Add support for multiple DB engines, and HouseCoupon views

This commit is contained in:
Lance Edgar 2019-07-09 22:35:11 -05:00
parent 8b5863c18a
commit 2e238ed723
4 changed files with 139 additions and 2 deletions

View file

@ -37,3 +37,6 @@ register(CoreOfficeSession)
CoreTransSession = scoped_session(sessionmaker())
register(CoreTransSession)
# empty dict for now, this must populated on app startup
ExtraCoreOfficeSessions = {}

View file

@ -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')

View file

@ -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 <http://www.gnu.org/licenses/>.
#
################################################################################
"""
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)

View file

@ -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)