From 2e238ed7234b03c4f92d8ed9b59ef3a902eb6d63 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 9 Jul 2019 22:35:11 -0500 Subject: [PATCH] Add support for multiple DB engines, and HouseCoupon views --- tailbone_corepos/db.py | 3 + tailbone_corepos/views/corepos/__init__.py | 1 + tailbone_corepos/views/corepos/coupons.py | 99 ++++++++++++++++++++++ tailbone_corepos/views/corepos/master.py | 38 ++++++++- 4 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 tailbone_corepos/views/corepos/coupons.py diff --git a/tailbone_corepos/db.py b/tailbone_corepos/db.py index a99e930..b11b65d 100644 --- a/tailbone_corepos/db.py +++ b/tailbone_corepos/db.py @@ -37,3 +37,6 @@ register(CoreOfficeSession) CoreTransSession = scoped_session(sessionmaker()) register(CoreTransSession) + +# empty dict for now, this must populated on app startup +ExtraCoreOfficeSessions = {} diff --git a/tailbone_corepos/views/corepos/__init__.py b/tailbone_corepos/views/corepos/__init__.py index 0aa9de6..0b8a910 100644 --- a/tailbone_corepos/views/corepos/__init__.py +++ b/tailbone_corepos/views/corepos/__init__.py @@ -35,4 +35,5 @@ def includeme(config): config.include('tailbone_corepos.views.corepos.members') config.include('tailbone_corepos.views.corepos.customers') config.include('tailbone_corepos.views.corepos.employees') + config.include('tailbone_corepos.views.corepos.coupons') config.include('tailbone_corepos.views.corepos.transactions') diff --git a/tailbone_corepos/views/corepos/coupons.py b/tailbone_corepos/views/corepos/coupons.py new file mode 100644 index 0000000..2a6ce67 --- /dev/null +++ b/tailbone_corepos/views/corepos/coupons.py @@ -0,0 +1,99 @@ +# -*- coding: utf-8; -*- +################################################################################ +# +# Rattail -- Retail Software Framework +# Copyright © 2010-2019 Lance Edgar +# +# This file is part of Rattail. +# +# Rattail 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. +# +# Rattail 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 +# Rattail. If not, see . +# +################################################################################ +""" +CORE POS coupon views +""" + +from corepos.db import model as corepos +from corepos import enum as corepos_enum + +from .master import CoreOfficeMasterView + + +class HouseCouponView(CoreOfficeMasterView): + """ + Master view for house (store) coupons + """ + model_class = corepos.HouseCoupon + model_title = "CORE-POS House Coupon" + url_prefix = '/core-pos/house-coupons' + route_prefix = 'corepos.house_coupons' + editable = True + + labels = { + 'coupon_id': "Coupon ID", + 'department_id': "Department Number", # filter + } + + grid_columns = [ + 'coupon_id', + 'description', + 'department', + 'discount_type', + 'discount_value', + 'start_date', + 'end_date', + ] + + form_fields = [ + 'coupon_id', + 'description', + 'limit', + 'start_date', + 'end_date', + 'member_only', + 'auto', + 'department', + 'min_type', + 'min_value', + 'discount_type', + 'discount_value', + 'virtual_only', + ] + + def configure_grid(self, g): + super(HouseCouponView, self).configure_grid(g) + + g.set_enum('discount_type', corepos_enum.HOUSE_COUPON_DISCOUNT_TYPE) + + g.set_joiner('department', lambda q: q.outerjoin(corepos.Department)) + g.set_filter('department', corepos.Department.dept_name, label="Department Name") + g.set_sorter('department', corepos.Department.dept_name) + + g.set_sort_defaults('coupon_id', 'desc') + + def configure_form(self, f): + super(HouseCouponView, self).configure_form(f) + + f.set_enum('member_only', corepos_enum.HOUSE_COUPON_MEMBER_ONLY) + + f.set_renderer('department', self.render_corepos_department) + + f.set_enum('discount_type', corepos_enum.HOUSE_COUPON_DISCOUNT_TYPE) + + if self.creating or self.editing: + f.set_readonly('department') # TODO: show dropdown for this + + +def includeme(config): + HouseCouponView.defaults(config) diff --git a/tailbone_corepos/views/corepos/master.py b/tailbone_corepos/views/corepos/master.py index e72d337..81dd4c8 100644 --- a/tailbone_corepos/views/corepos/master.py +++ b/tailbone_corepos/views/corepos/master.py @@ -30,22 +30,27 @@ CORE POS master view # from rattail.time import localtime # from rattail.util import NOTSET +from rattail.util import OrderedDict + +from webhelpers2.html import HTML, tags from tailbone.views import MasterView # from tailbone.util import raw_datetime -from tailbone_corepos.db import CoreOfficeSession +from tailbone_corepos.db import CoreOfficeSession, ExtraCoreOfficeSessions class CoreOfficeMasterView(MasterView): """ Master base class for Catapult views """ - Session = CoreOfficeSession # model_key = 'pk' creatable = False editable = False deletable = False + supports_multiple_engines = True + engine_type_key = 'corepos' + # # TODO: would be nice to find a way around this somehow # # must encode all search values as utf-8 # use_byte_string_filters = True @@ -61,3 +66,32 @@ class CoreOfficeMasterView(MasterView): # value = catapult_time(value) # value = localtime(self.rattail_config, value) # return raw_datetime(self.rattail_config, value) + + def get_db_engines(self): + engines = OrderedDict() + if self.rattail_config.corepos_engine: + engines['default'] = self.rattail_config.corepos_engine + for dbkey in sorted(self.rattail_config.corepos_engines): + if dbkey != 'default': + engines[dbkey] = self.rattail_config.corepos_engines[dbkey] + return engines + + @property + def Session(self): + """ + Which session we return will depend on user's "current" engine. + """ + dbkey = self.get_current_engine_dbkey() + + if dbkey != 'default' and default in ExtraCoreOfficeSessions: + return ExtraCoreOfficeSessions[dbkey] + + return CoreOfficeSession + + def render_corepos_department(self, obj, field): + department = getattr(obj, field) + if not department: + return "" + text = "({}) {}".format(department.dept_no, department.dept_name) + url = self.request.route_url('corepos.departments.view', dept_no=department.dept_no) + return tags.link_to(text, url)