From dde6195f38d214ea353fe58c2833fb9ba08d8cc2 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 21 Jan 2021 17:39:16 -0600 Subject: [PATCH] Add master view for Units of Measure mapping table w/ support for "collect from wild" tool --- .../templates/units-of-measure/index.mako | 66 +++++++++ tailbone/views/uoms.py | 126 ++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 tailbone/templates/units-of-measure/index.mako create mode 100644 tailbone/views/uoms.py diff --git a/tailbone/templates/units-of-measure/index.mako b/tailbone/templates/units-of-measure/index.mako new file mode 100644 index 00000000..b29bad66 --- /dev/null +++ b/tailbone/templates/units-of-measure/index.mako @@ -0,0 +1,66 @@ +## -*- coding: utf-8; -*- +<%inherit file="/master/index.mako" /> + +<%def name="grid_tools()"> + ${parent.grid_tools()} + + + Collect from the Wild + + + ${h.form(url('{}.collect_wild_uoms'.format(route_prefix)), ref='collect-wild-uoms-form')} + ${h.csrf_token(request)} + ${h.end_form()} + + + + + + +<%def name="modify_this_page_vars()"> + ${parent.modify_this_page_vars()} + + + + +${parent.body()} diff --git a/tailbone/views/uoms.py b/tailbone/views/uoms.py new file mode 100644 index 00000000..11f80779 --- /dev/null +++ b/tailbone/views/uoms.py @@ -0,0 +1,126 @@ +# -*- coding: utf-8; -*- +################################################################################ +# +# Rattail -- Retail Software Framework +# Copyright © 2010-2021 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 . +# +################################################################################ +""" +UOM Views +""" + +from __future__ import unicode_literals, absolute_import + +from rattail.db import model + +from tailbone.views import MasterView + + +class UnitOfMeasureView(MasterView): + """ + Master view for the UOM mappings. + """ + model_class = model.UnitOfMeasure + route_prefix = 'uoms' + url_prefix = '/units-of-measure' + bulk_deletable = True + has_versions = True + + labels = { + 'sil_code': "SIL Code", + } + + grid_columns = [ + 'abbreviation', + 'sil_code', + 'description', + 'notes', + ] + + form_fields = [ + 'abbreviation', + 'sil_code', + 'description', + 'notes', + ] + + def configure_grid(self, g): + super(UnitOfMeasureView, self).configure_grid(g) + + g.set_renderer('description', self.render_description) + + g.set_sort_defaults('abbreviation') + + g.set_link('abbreviation') + g.set_link('description') + + def configure_form(self, f): + super(UnitOfMeasureView, self).configure_form(f) + + f.set_renderer('description', self.render_description) + f.set_type('notes', 'text') + + if not self.creating: + f.set_readonly('abbreviation') + + if self.creating or self.editing: + f.remove('description') + f.set_enum('sil_code', self.enum.UNIT_OF_MEASURE) + + def redirect_after_create(self, uom, **kwargs): + return self.redirect(self.get_index_url()) + + def redirect_after_edit(self, uom, **kwargs): + return self.redirect(self.get_index_url()) + + def render_description(self, uom, field): + code = uom.sil_code + if code: + if code in self.enum.UNIT_OF_MEASURE: + return self.enum.UNIT_OF_MEASURE[code] + return "(unknown code)" + + def collect_wild_uoms(self): + app = self.get_rattail_app() + handler = app.get_products_handler() + uoms = handler.collect_wild_uoms() + self.request.session.flash("All abbreviations from the wild have been added. Now please map each to a SIL code.") + return self.redirect(self.get_index_url()) + + @classmethod + def defaults(cls, config): + cls._uom_defaults(config) + cls._defaults(config) + + @classmethod + def _uom_defaults(cls, config): + route_prefix = cls.get_route_prefix() + url_prefix = cls.get_url_prefix() + permission_prefix = cls.get_permission_prefix() + + # collect wild uoms + config.add_tailbone_permission(permission_prefix, '{}.collect_wild_uoms'.format(permission_prefix), + "Collect UoM abbreviations from the wild") + config.add_route('{}.collect_wild_uoms'.format(route_prefix), '{}/collect-wild-uoms'.format(url_prefix), + request_method='POST') + config.add_view(cls, attr='collect_wild_uoms', route_name='{}.collect_wild_uoms'.format(route_prefix), + permission='{}.collect_wild_uoms'.format(permission_prefix)) + + +def includeme(config): + UnitOfMeasureView.defaults(config)