diff --git a/rattail/pyramid/views/customergroups.py b/rattail/pyramid/views/customergroups.py index af3a3bf6..fd7d5f79 100644 --- a/rattail/pyramid/views/customergroups.py +++ b/rattail/pyramid/views/customergroups.py @@ -29,7 +29,8 @@ from edbob.pyramid.views import SearchableAlchemyGridView from rattail.pyramid.views import CrudView -from rattail.db.model import CustomerGroup +from rattail.pyramid import Session +from rattail.db.model import CustomerGroup, CustomerGroupAssignment class CustomerGroupsGrid(SearchableAlchemyGridView): @@ -84,6 +85,13 @@ class CustomerGroupCrud(CrudView): ]) return fs + def pre_delete(self, group): + # First remove customer associations. + q = Session.query(CustomerGroupAssignment)\ + .filter(CustomerGroupAssignment.group == group) + for assignment in q: + Session.delete(assignment) + def add_routes(config): config.add_route('customer_groups', '/customer-groups')