# # Copyright (c) Daniel Sheffield 2021 # # All rights reserved # # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY import urwid from app.db_utils import NON_IDENTIFIER_COLUMNS from ..widgets import ( AutoCompleteEdit, AutoCompletePopUp, ) from collections import OrderedDict class NewProduct(urwid.Overlay): def __init__(self, query_manager, under, name, data, autocomplete_cb, change_cb, apply_cb, esc_cb, apply_choice_cb): self.esc_cb = esc_cb self.under = under self._data = data self.query_manager = query_manager self.name = name title = urwid.Text('Enter Product Info', align='center') self.fields = OrderedDict() for f in ('product', 'category', 'group'): w = AutoCompleteEdit(('bg', f)) self.fields[f] = w w.set_edit_text(data[f]) urwid.connect_signal(w, 'change', change_cb(f)) ok = urwid.Button('Done', on_press=lambda w: apply_cb(**self.data)) body = urwid.AttrMap(urwid.ListBox(urwid.SimpleListWalker([ urwid.Pile([ urwid.AttrMap(title, 'banner'), urwid.Divider(), *[ urwid.AttrMap( urwid.LineBox(urwid.AttrMap( AutoCompletePopUp( v, apply_change_func=lambda name, pop_up_cb: autocomplete_cb(name, pop_up_cb), apply_choice_cb=apply_choice_cb, ),'streak'), title=k.title(), title_align='left'), 'banner' ) for k,v in self.fields.items() ], urwid.AttrMap(ok, 'banner'), ], focus_item=2) ]) ), 'banner') super().__init__(urwid.AttrMap(body, 'bg'), under, align='center', width=('relative', 40), valign='middle', height=('relative', 40), min_width=20, min_height=12) @property def data(self): return dict([(k,v) for k,v in self._data.items() if k in ('product', 'category', 'group')]) def _apply_choice(self, name, value): self._data.update({ name: value }) for k,v in self.data.items(): if k == name or v: continue options = self.query_manager.unique_suggestions(k, exclude=[self.name, *NON_IDENTIFIER_COLUMNS], **self.data) if len(options) == 1 and k != 'ts': self._data.update({ k: list(options)[0] }) def apply_choice(self, name): return lambda w,x: self._apply_choice(name, x) def update(self): for k, v in self.data.items(): self.fields[k].set_edit_text(v) return self def keypress(self, size, key): if key == 'esc': self.esc_cb() return if key == 'tab': return return super().keypress(size, key)