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,
}
# 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():
if not config.production():
# 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))
# TODO: this doesn't do anything interesting now, but it could
page.window_destroy()
def keyboard(e):
@ -155,8 +109,20 @@ def main(page: ft.Page):
# page.on_view_pop = view_pop
# TODO: this may be too hacky but is useful for now/dev
if not config.production() and page.shared.get('user_uuid'):
page.go('/pos')
if not config.production():
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:
page.go('/login')

View file

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

View file

@ -132,10 +132,13 @@ class LoginView(WuttaView):
if user:
# handle success
self.page.shared.update({
'user_uuid': user.uuid,
'user_display': user_display,
})
self.page.session.set('user_uuid', user.uuid)
self.page.session.set('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')
else:

View file

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