Centralize logic for default POS batch handler

This commit is contained in:
Lance Edgar 2023-09-24 18:00:06 -05:00
parent a0915f7d5c
commit 6448fc139c
3 changed files with 45 additions and 6 deletions

View file

@ -32,6 +32,7 @@ from rattail.config import make_config
import flet as ft import flet as ft
import wuttapos import wuttapos
from wuttapos.util import get_pos_batch_handler
def main(page: ft.Page): def main(page: ft.Page):
@ -62,7 +63,7 @@ def main(page: ft.Page):
page.shared.pop('cust_uuid', None) # TODO page.shared.pop('cust_uuid', None) # TODO
page.shared.pop('cust_display', None) # TODO page.shared.pop('cust_display', None) # TODO
if page.shared and page.shared.get('user_uuid'): if page.shared and page.shared.get('user_uuid'):
handler = app.get_batch_handler('pos') handler = get_pos_batch_handler(config)
session = app.make_session() session = app.make_session()
user = session.get(model.User, page.shared['user_uuid']) user = session.get(model.User, page.shared['user_uuid'])
if user: if user:

36
wuttapos/util.py Normal file
View file

@ -0,0 +1,36 @@
# -*- coding: utf-8; -*-
################################################################################
#
# WuttaPOS -- Pythonic Point of Sale System
# Copyright © 2023 Lance Edgar
#
# This file is part of WuttaPOS.
#
# WuttaPOS is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# WuttaPOS is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# WuttaPOS. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
WuttaPOS utilities
"""
def get_pos_batch_handler(config):
"""
Official way of obtaining the POS batch handler.
Code should use this where possible to make later refactoring
easier, should it be needed.
"""
app = config.get_app()
return app.get_batch_handler('pos', default='rattail.batch.pos:POSBatchHandler')

View file

@ -32,6 +32,8 @@ import flet as ft
from .base import WuttaView from .base import WuttaView
from wuttapos.controls.keyboard import WuttaKeyboard from wuttapos.controls.keyboard import WuttaKeyboard
from wuttapos.util import get_pos_batch_handler
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -48,7 +50,7 @@ class POSView(WuttaView):
disabled_bgcolor = '#aaaaaa' disabled_bgcolor = '#aaaaaa'
def get_batch_handler(self): def get_batch_handler(self):
return self.app.get_batch_handler('pos') return get_pos_batch_handler(self.config)
def set_customer(self, customer, batch=None): def set_customer(self, customer, batch=None):
session = self.app.get_session(customer) session = self.app.get_session(customer)
@ -744,7 +746,7 @@ 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.shared['user_uuid'])
handler = self.app.get_batch_handler('pos') 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):
@ -805,7 +807,7 @@ class POSView(WuttaView):
model = self.model model = self.model
session = self.app.make_session() session = self.app.make_session()
handler = self.app.get_batch_handler('pos') handler = self.get_batch_handler()
user = session.get(model.User, self.page.shared['user_uuid']) user = session.get(model.User, self.page.shared['user_uuid'])
batch = handler.get_current_batch(user, create=True) batch = handler.get_current_batch(user, create=True)
@ -862,7 +864,7 @@ class POSView(WuttaView):
def tender_click(self, e): def tender_click(self, e):
model = self.model model = self.model
session = self.app.make_session() session = self.app.make_session()
handler = self.app.get_batch_handler('pos') handler = self.get_batch_handler()
user = session.get(model.User, self.page.shared['user_uuid']) user = session.get(model.User, self.page.shared['user_uuid'])
batch = handler.get_current_batch(user) batch = handler.get_current_batch(user)
@ -902,7 +904,7 @@ class POSView(WuttaView):
self.page.update() self.page.update()
return return
handler = self.app.get_batch_handler('pos') handler = self.get_batch_handler()
session = self.app.make_session() session = self.app.make_session()
model = self.model model = self.model