Use Page.session and .client_storage instead of my first hack

no shortage of hacky yet though i'm sure
This commit is contained in:
Lance Edgar 2023-09-24 20:56:02 -05:00
parent 51c1094e1c
commit 943f723ae6
4 changed files with 59 additions and 93 deletions

View file

@ -65,54 +65,8 @@ def main(page: ft.Page):
'default_button_height_dlg': 80, 'default_button_height_dlg': 80,
} }
# nb. track current user, txn etc.
page.shared = {}
# TODO: this may be too hacky but is useful for now/dev
if not config.production():
path = os.path.join(config.appdir(), '.wuttapos.cache')
if os.path.exists(path):
with open(path, 'rt') as f:
page.shared = json.loads(f.read())
page.shared.pop('txn_display', None) # TODO
page.shared.pop('cust_uuid', None) # TODO
page.shared.pop('cust_display', None) # TODO
if page.shared and page.shared.get('user_uuid'):
handler = get_pos_batch_handler(config)
session = app.make_session()
user = session.get(model.User, page.shared['user_uuid'])
if user:
# TODO: should also filter this by terminal
batch = handler.get_current_batch(user, create=False)
if batch:
page.shared['txn_display'] = batch.id_str
page.shared['cust_uuid'] = batch.customer_uuid
if batch.customer:
key = app.get_customer_key_field()
value = getattr(batch.customer, key)
page.shared['cust_display'] = str(value or '') or None
else:
page.shared['cust_display'] = None
else:
page.shared['txn_display'] = None
page.shared['cust_uuid'] = None
page.shared['cust_display'] = None
else:
page.shared['user_uuid'] = None
page.shared['user_display'] = None
page.shared['txn_display'] = None
page.shared['cust_uuid'] = None
page.shared['cust_display'] = None
session.close()
else:
page.shared['user_display'] = None
def clean_exit(): def clean_exit():
if not config.production(): # TODO: this doesn't do anything interesting now, but it could
# TODO: this may be too hacky but is useful for now/dev
path = os.path.join(config.appdir(), '.wuttapos.cache')
with open(path, 'wt') as f:
f.write(json.dumps(page.shared))
page.window_destroy() page.window_destroy()
def keyboard(e): def keyboard(e):
@ -155,8 +109,20 @@ def main(page: ft.Page):
# page.on_view_pop = view_pop # page.on_view_pop = view_pop
# TODO: this may be too hacky but is useful for now/dev # TODO: this may be too hacky but is useful for now/dev
if not config.production() and page.shared.get('user_uuid'): if not config.production():
page.go('/pos') user = None
uuid = page.client_storage.get('user_uuid')
if uuid:
session = app.make_session()
user = session.get(model.User, uuid)
if user:
page.session.set('user_uuid', uuid)
page.session.set('user_display', str(user))
session.close()
if user:
page.go('/pos')
else:
page.go('/login')
else: else:
page.go('/login') page.go('/login')

View file

@ -62,38 +62,35 @@ class WuttaHeader(WuttaControl):
self.update() self.update()
def update_txn_display(self): def update_txn_display(self):
txn_display = "N" txn_display = None
if self.page:
if self.page and self.page.shared and self.page.shared.get('txn_display'): txn_display = self.page.session.get('txn_display')
txn_display = self.page.shared['txn_display'] self.txn_display.value = f"Txn: {txn_display or 'N'}"
self.txn_display.value = f"Txn: {txn_display}"
def update_cust_display(self): def update_cust_display(self):
cust_display = "N" cust_display = None
if self.page:
if self.page and self.page.shared and self.page.shared.get('cust_display'): cust_display = self.page.session.get('cust_display')
cust_display = self.page.shared['cust_display'] self.cust_display.value = f"Cust: {cust_display or 'N'}"
self.cust_display.value = f"Cust: {cust_display}"
def update_user_display(self): def update_user_display(self):
user_display = "N" user_display = None
if self.page:
user_display = self.page.session.get('user_display')
self.user_display.value = f"User: {user_display or 'N'}"
if self.page and self.page.shared and self.page.shared.get('user_display'): if self.page and self.page.session.get('user_uuid'):
user_display = self.page.shared['user_display']
self.user_display.value = f"User: {user_display}"
if self.page and self.page.shared.get('user_uuid'):
self.logout_button.visible = True self.logout_button.visible = True
self.logout_divider.visible = True self.logout_divider.visible = True
def logout_click(self, e): def logout_click(self, e):
self.page.shared.update({ # TODO: hacky but works for now
'user_uuid': None, if not self.config.production():
'user_display': None, self.page.client_storage.set('user_uuid', '')
'txn_display': None,
'cust_display': None, self.page.session.set('user_uuid', None)
}) self.page.session.set('user_display', None)
self.page.session.set('txn_display', None)
self.page.session.set('cust_display', None)
self.page.go('/login') self.page.go('/login')

View file

@ -132,10 +132,13 @@ class LoginView(WuttaView):
if user: if user:
# handle success # handle success
self.page.shared.update({ self.page.session.set('user_uuid', user.uuid)
'user_uuid': user.uuid, self.page.session.set('user_display', user_display)
'user_display': user_display,
}) # TODO: hacky but works for now
if not self.config.production():
self.page.client_storage.set('user_uuid', user.uuid)
self.page.go('/pos') self.page.go('/pos')
else: else:

View file

@ -62,8 +62,8 @@ class POSView(WuttaView):
key = self.app.get_customer_key_field() key = self.app.get_customer_key_field()
value = getattr(customer, key) value = getattr(customer, key)
self.page.shared['cust_uuid'] = customer.uuid self.page.session.set('cust_uuid', customer.uuid)
self.page.shared['cust_display'] = str(value or '') self.page.session.set('cust_display', str(value or ''))
self.header.update_cust_display() self.header.update_cust_display()
self.header.update() self.header.update()
# TODO: can we assume caller will do this? # TODO: can we assume caller will do this?
@ -292,7 +292,7 @@ class POSView(WuttaView):
else: else:
different = False different = False
customer = session.get(self.model.Customer, self.page.shared['cust_uuid']) customer = session.get(self.model.Customer, self.page.session.get('cust_uuid'))
assert customer assert customer
info = clientele.get_customer_info_markdown(customer) info = clientele.get_customer_info_markdown(customer)
@ -439,7 +439,7 @@ class POSView(WuttaView):
def customer_click(self, e): def customer_click(self, e):
# prompt user to replace customer if already set # prompt user to replace customer if already set
if self.page.shared['cust_uuid']: if self.page.session.get('cust_uuid'):
self.customer_prompt() self.customer_prompt()
else: else:
@ -484,7 +484,7 @@ class POSView(WuttaView):
def send_feedback(e): def send_feedback(e):
self.app.send_email('pos_feedback', data={ self.app.send_email('pos_feedback', data={
'user_name': self.page.shared['user_display'], 'user_name': self.page.session.get('user_display'),
'message': message.value, 'message': message.value,
}) })
@ -831,14 +831,14 @@ class POSView(WuttaView):
def get_current_batch(self, session, user=None, create=True): def get_current_batch(self, session, user=None, create=True):
if not user: if not user:
user = session.get(self.model.User, self.page.shared['user_uuid']) user = session.get(self.model.User, self.page.session.get('user_uuid'))
handler = self.get_batch_handler() handler = self.get_batch_handler()
return handler.get_current_batch(user, create=create) return handler.get_current_batch(user, create=create)
def did_mount(self): def did_mount(self):
session = self.app.make_session() session = self.app.make_session()
batch = self.get_current_batch(session) batch = self.get_current_batch(session)
self.page.shared['txn_display'] = batch.id_str self.page.session.set('txn_display', batch.id_str)
self.items.controls.clear() self.items.controls.clear()
for row in batch.active_rows(): for row in batch.active_rows():
@ -894,7 +894,7 @@ class POSView(WuttaView):
model = self.model model = self.model
session = self.app.make_session() session = self.app.make_session()
handler = self.get_batch_handler() handler = self.get_batch_handler()
user = session.get(model.User, self.page.shared['user_uuid']) user = session.get(model.User, self.page.session.get('user_uuid'))
batch = handler.get_current_batch(user, create=True) batch = handler.get_current_batch(user, create=True)
# void current batch # void current batch
@ -904,7 +904,7 @@ class POSView(WuttaView):
# make new batch # make new batch
batch = handler.get_current_batch(user, create=True) batch = handler.get_current_batch(user, create=True)
self.page.shared['txn_display'] = batch.id_str self.page.session.set('txn_display', batch.id_str)
session.commit() session.commit()
session.close() session.close()
@ -951,7 +951,7 @@ class POSView(WuttaView):
model = self.model model = self.model
session = self.app.make_session() session = self.app.make_session()
handler = self.get_batch_handler() handler = self.get_batch_handler()
user = session.get(model.User, self.page.shared['user_uuid']) user = session.get(model.User, self.page.session.get('user_uuid'))
batch = handler.get_current_batch(user) batch = handler.get_current_batch(user)
# tender / execute batch # tender / execute batch
@ -961,7 +961,7 @@ class POSView(WuttaView):
# make new batch # make new batch
batch = handler.get_current_batch(user, create=True) batch = handler.get_current_batch(user, create=True)
self.page.shared['txn_display'] = batch.id_str self.page.session.set('txn_display', batch.id_str)
self.header.update_txn_display() self.header.update_txn_display()
self.header.update() self.header.update()
@ -974,9 +974,9 @@ class POSView(WuttaView):
def clear_all(self): def clear_all(self):
self.items.controls.clear() self.items.controls.clear()
self.txn_total.value = None self.txn_total.value = None
self.page.shared['txn_display'] = None self.page.session.set('txn_display', None)
self.page.shared['cust_uuid'] = None self.page.session.set('cust_uuid', None)
self.page.shared['cust_display'] = None self.page.session.set('cust_display', None)
self.header.update_txn_display() self.header.update_txn_display()
self.header.update_cust_display() self.header.update_cust_display()
# TODO: not clear why must call update() for header too? # TODO: not clear why must call update() for header too?
@ -994,7 +994,7 @@ class POSView(WuttaView):
session = self.app.make_session() session = self.app.make_session()
model = self.model model = self.model
user = session.get(model.User, self.page.shared['user_uuid']) user = session.get(model.User, self.page.session.get('user_uuid'))
batch = handler.get_current_batch(user) batch = handler.get_current_batch(user)
kwargs = {} kwargs = {}