diff --git a/tailbone/views/departments.py b/tailbone/views/departments.py index c3d48058..4d827018 100644 --- a/tailbone/views/departments.py +++ b/tailbone/views/departments.py @@ -2,7 +2,7 @@ ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2018 Lance Edgar +# Copyright © 2010-2020 Lance Edgar # # This file is part of Rattail. # @@ -82,6 +82,19 @@ class DepartmentsView(MasterView): kwargs['employees'] = None return kwargs + def before_delete(self, department): + """ + Check to see if there are any products which belong to the department; + if there are then we do not allow delete and redirect the user. + """ + count = self.Session.query(model.Product)\ + .filter(model.Product.department == department)\ + .count() + if count: + self.request.session.flash("Will not delete department which still has {} products: {}".format( + count, department), 'error') + raise self.redirect(self.get_action_url('view', department)) + def list_by_vendor(self): """ View list of departments by vendor