Only expose "product" departments within product view dropdowns

This commit is contained in:
Lance Edgar 2022-01-24 15:32:24 -06:00
parent ae27c110ab
commit db3cd4ec6e

View file

@ -211,6 +211,18 @@ class ProductView(MasterView):
return query return query
def get_departments(self):
"""
Returns the list of departments to be exposed in a drop-down.
"""
model = self.model
return self.Session.query(model.Department)\
.filter(sa.or_(
model.Department.product == True,
model.Department.product == None))\
.order_by(model.Department.name)\
.all()
def configure_grid(self, g): def configure_grid(self, g):
super(ProductView, self).configure_grid(g) super(ProductView, self).configure_grid(g)
app = self.get_rattail_app() app = self.get_rattail_app()
@ -247,12 +259,9 @@ class ProductView(MasterView):
# department # department
g.set_joiner('department', lambda q: q.outerjoin(model.Department)) g.set_joiner('department', lambda q: q.outerjoin(model.Department))
g.set_sorter('department', model.Department.name) g.set_sorter('department', model.Department.name)
department_choices = app.cache_model(self.Session(), model.Department, departments = self.get_departments()
order_by=model.Department.name,
normalizer=lambda d: d.name)
department_choices = OrderedDict([('', "(any)")] department_choices = OrderedDict([('', "(any)")]
+ sorted(six.iteritems(department_choices), + [(d.uuid, d.name) for d in departments])
key=lambda itm: itm[1]))
if not use_buefy: if not use_buefy:
department_choices = [tags.Option(name, uuid) department_choices = [tags.Option(name, uuid)
for uuid, name in six.iteritems(department_choices)] for uuid, name in six.iteritems(department_choices)]
@ -824,8 +833,7 @@ class ProductView(MasterView):
if self.creating or self.editing: if self.creating or self.editing:
if 'department' in f.fields: if 'department' in f.fields:
f.replace('department', 'department_uuid') f.replace('department', 'department_uuid')
departments = self.Session.query(model.Department)\ departments = self.get_departments()
.order_by(model.Department.number)
dept_values = [(d.uuid, "{} {}".format(d.number, d.name)) dept_values = [(d.uuid, "{} {}".format(d.number, d.name))
for d in departments] for d in departments]
require_department = False require_department = False