Move logic for "bulk-delete row objects" into MasterView

i guess so far it has only been needed for batch, but some day surely
it will be needed for something else..?

some of the template logic is still batch only i think..
This commit is contained in:
Lance Edgar 2022-08-31 20:52:17 -05:00
parent b5a519d132
commit c43a4edec7
2 changed files with 47 additions and 18 deletions

View file

@ -1264,22 +1264,19 @@ class BatchMasterView(MasterView):
""" """
self.handler.do_remove_row(row) self.handler.do_remove_row(row)
def bulk_delete_rows(self): def delete_row_objects(self, rows):
""" deleted = super(BatchMasterView, self).delete_row_objects(rows)
"Delete" all rows matching the current row grid view query. This sets
the ``removed`` flag on the rows but does not truly delete them.
"""
batch = self.get_instance() batch = self.get_instance()
query = self.get_effective_row_data(sort=False)
# TODO: this should surely be handled by the handler... # decrement rowcount for batch
if batch.rowcount is not None: if batch.rowcount is not None:
batch.rowcount -= query.count() batch.rowcount -= deleted
query.update({'removed': True}, synchronize_session=False)
# refresh batch status
self.Session.refresh(batch) self.Session.refresh(batch)
self.handler.refresh_batch_status(batch) self.handler.refresh_batch_status(batch)
return self.redirect(self.get_action_url('view', batch)) return deleted
def execute(self): def execute(self):
""" """
@ -1505,14 +1502,6 @@ class BatchMasterView(MasterView):
config.add_tailbone_permission(permission_prefix, '{}.refresh'.format(permission_prefix), config.add_tailbone_permission(permission_prefix, '{}.refresh'.format(permission_prefix),
"Refresh data for {}".format(model_title)) "Refresh data for {}".format(model_title))
# bulk delete rows
if cls.rows_bulk_deletable:
config.add_route('{}.delete_rows'.format(route_prefix), '{}/{{uuid}}/rows/delete'.format(url_prefix))
config.add_view(cls, attr='bulk_delete_rows', route_name='{}.delete_rows'.format(route_prefix),
permission='{}.delete_rows'.format(permission_prefix))
config.add_tailbone_permission(permission_prefix, '{}.delete_rows'.format(permission_prefix),
"Bulk-delete data rows from {}".format(model_title))
# toggle complete # toggle complete
config.add_route('{}.toggle_complete'.format(route_prefix), '{}/{{{}}}/toggle-complete'.format(url_prefix, model_key)) config.add_route('{}.toggle_complete'.format(route_prefix), '{}/{{{}}}/toggle-complete'.format(url_prefix, model_key))
config.add_view(cls, attr='toggle_complete', route_name='{}.toggle_complete'.format(route_prefix), config.add_view(cls, attr='toggle_complete', route_name='{}.toggle_complete'.format(route_prefix),

View file

@ -4182,6 +4182,30 @@ class MasterView(View):
self.delete_row_object(row) self.delete_row_object(row)
return self.redirect(self.get_action_url('view', self.get_parent(row))) return self.redirect(self.get_action_url('view', self.get_parent(row)))
def bulk_delete_rows(self):
"""
Delete all row objects matching the current row grid query.
"""
obj = self.get_instance()
rows = self.get_effective_row_data(sort=False).all()
# TODO: this should use a separate thread with progress
self.delete_row_objects(rows)
self.Session.refresh(obj)
return self.redirect(self.get_action_url('view', obj))
def delete_row_objects(self, rows):
"""
Perform the actual deletion of given row objects.
"""
deleted = 0
for row in rows:
if self.row_deletable(row):
self.delete_row_object(row)
deleted += 1
return deleted
def get_parent(self, row): def get_parent(self, row):
raise NotImplementedError raise NotImplementedError
@ -4940,6 +4964,22 @@ class MasterView(View):
config.add_view(cls, attr='create_row', route_name='{}.create_row'.format(route_prefix), config.add_view(cls, attr='create_row', route_name='{}.create_row'.format(route_prefix),
permission='{}.create_row'.format(permission_prefix)) permission='{}.create_row'.format(permission_prefix))
# bulk-delete rows
# nb. must be defined before view_row b/c of url similarity
if cls.rows_bulk_deletable:
config.add_tailbone_permission(permission_prefix,
'{}.delete_rows'.format(permission_prefix),
"Bulk-delete {} from {}".format(
row_model_title_plural, model_title))
config.add_route('{}.delete_rows'.format(route_prefix),
'{}/rows/delete'.format(instance_url_prefix),
# TODO: should enforce this
# request_method='POST'
)
config.add_view(cls, attr='bulk_delete_rows',
route_name='{}.delete_rows'.format(route_prefix),
permission='{}.delete_rows'.format(permission_prefix))
# view row # view row
if cls.has_rows: if cls.has_rows:
if cls.rows_viewable: if cls.rows_viewable: