Add support for "bulk-delete" of Person table

This commit is contained in:
Lance Edgar 2020-03-16 17:47:06 -05:00
parent 7994c7d770
commit 907a356bea
2 changed files with 27 additions and 3 deletions

View file

@ -1855,7 +1855,7 @@ class MasterView(View):
def bulk_delete_objects(self, session, objects, progress=None): def bulk_delete_objects(self, session, objects, progress=None):
def delete(obj, i): def delete(obj, i):
session.delete(obj) self.delete_instance(obj)
if i % 1000 == 0: if i % 1000 == 0:
session.flush() session.flush()
@ -3142,10 +3142,14 @@ class MasterView(View):
""" """
Delete the instance, or mark it as deleted, or whatever you need to do. Delete the instance, or mark it as deleted, or whatever you need to do.
""" """
# note, we don't use self.Session here, in case we're being called from
# a separate (bulk-delete) thread
session = orm.object_session(instance)
session.delete(instance)
# Flush immediately to force any pending integrity errors etc.; that # Flush immediately to force any pending integrity errors etc.; that
# way we don't set flash message until we know we have success. # way we don't set flash message until we know we have success.
self.Session.delete(instance) session.flush()
self.Session.flush()
def get_after_delete_url(self, instance): def get_after_delete_url(self, instance):
""" """

View file

@ -51,6 +51,7 @@ class PeopleView(MasterView):
touchable = True touchable = True
has_versions = True has_versions = True
supports_mobile = True supports_mobile = True
bulk_deletable = True
manage_notes_from_profile_view = False manage_notes_from_profile_view = False
grid_columns = [ grid_columns = [
@ -146,6 +147,25 @@ class PeopleView(MasterView):
return not bool(person.user and person.user.username == 'chuck') return not bool(person.user and person.user.username == 'chuck')
return True return True
def delete_instance(self, person):
"""
Supplements the default logic as follows:
Any customer associations are first deleted for the person. Once that
is complete, deletion continues as per usual.
"""
session = orm.object_session(person)
# must explicitly remove all CustomerPerson records
for cp in list(person._customers):
customer = cp.customer
session.delete(cp)
# session.flush()
customer._people.reorder()
# continue with normal logic
super(PeopleView, self).delete_instance(person)
def touch_instance(self, person): def touch_instance(self, person):
""" """
Supplements the default logic as follows: Supplements the default logic as follows: