Prevent deletion of department which still has products

This commit is contained in:
Lance Edgar 2020-03-15 13:01:52 -05:00
parent 964671fcbf
commit 9b00e829b8

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2018 Lance Edgar # Copyright © 2010-2020 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -82,6 +82,19 @@ class DepartmentsView(MasterView):
kwargs['employees'] = None kwargs['employees'] = None
return kwargs 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): def list_by_vendor(self):
""" """
View list of departments by vendor View list of departments by vendor