From 9b00e829b8ef01bd3a8e3247225c69426bde5047 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 15 Mar 2020 13:01:52 -0500 Subject: [PATCH] Prevent deletion of department which still has products --- tailbone/views/departments.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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