Add generic "bulk delete" support to MasterView

This commit is contained in:
Lance Edgar 2017-02-17 19:40:21 -06:00
parent 9712868406
commit 6ed752d477
3 changed files with 59 additions and 5 deletions

View file

@ -34,6 +34,11 @@
padding: 0 0.5em 0 0;
}
.newgrid-wrapper .grid-header td.tools form {
display: inline-block;
}
/******************************
* filters
******************************/

View file

@ -43,6 +43,25 @@
% endif
% if master.bulk_deletable and request.has_perm('{}.bulk_delete'.format(permission_prefix)):
$('form[name="bulk-delete"] button').click(function() {
var count = 0;
var match = /showing \d+ thru \d+ of (\S+)/.exec($('.pager .showing').text());
if (match) {
count = match[1];
} else {
alert("There don't seem to be any results to delete!");
return;
}
if (! confirm("You are about to delete " + count + " ${model_title_plural}.\n\nAre you sure?")) {
return
}
$(this).button('disable').button('option', 'label', "Deleting Results...");
$('form[name="bulk-delete"]').submit();
});
% endif
});
</script>
</%def>
@ -61,6 +80,12 @@
<button type="submit">Merge 2 ${model_title_plural}</button>
${h.end_form()}
% endif
% if master.bulk_deletable and request.has_perm('{}.bulk_delete'.format(permission_prefix)):
${h.form(url('{}.bulk_delete'.format(route_prefix)), name='bulk-delete')}
${h.csrf_token(request)}
<button type="button">Delete Results</button>
${h.end_form()}
% endif
</%def>
${grid.render_complete(tools=capture(self.grid_tools).strip(), context_menu=capture(self.context_menu_items).strip())|n}

View file

@ -54,6 +54,7 @@ class MasterView(View):
viewable = True
editable = True
deletable = True
bulk_deletable = False
mergeable = False
listing = False
@ -408,6 +409,19 @@ class MasterView(View):
'instance_title': instance_title,
'form': form})
def bulk_delete(self):
"""
Delete all records matching the current grid query
"""
if self.request.method == 'POST':
query = self.get_effective_query(sortable=False)
count = query.count()
query.delete(synchronize_session=False)
self.request.session.flash("Deleted {:,d} {}".format(count, self.get_model_title_plural()))
else:
self.request.session.flash("Sorry, you must POST to do a bulk delete operation")
return self.redirect(self.get_index_url())
def get_merge_fields(self):
if hasattr(self, 'merge_fields'):
return self.merge_fields
@ -895,7 +909,7 @@ class MasterView(View):
"""
return session.query(self.get_model_class())
def get_effective_data(self, session=None):
def get_effective_data(self, session=None, **kwargs):
"""
Convenience method which returns the "effective" data for the master
grid, filtered and sorted to match what would show on the UI, but not
@ -903,12 +917,14 @@ class MasterView(View):
"""
if session is None:
session = self.Session()
grid = self.make_grid(session=session, pageable=False,
main_actions=[], more_actions=[])
kwargs.setdefault('pageable', False)
kwargs.setdefault('main_actions', [])
kwargs.setdefault('more_actions', [])
grid = self.make_grid(session=session, **kwargs)
return grid._fa_grid.rows
def get_effective_query(self, session):
return self.get_effective_data(session)
def get_effective_query(self, session=None, **kwargs):
return self.get_effective_data(session=session, **kwargs)
def checkbox(self, instance):
"""
@ -1295,6 +1311,14 @@ class MasterView(View):
config.add_tailbone_permission(permission_prefix, '{0}.create'.format(permission_prefix),
"Create new {0}".format(model_title))
# bulk delete
if cls.bulk_deletable:
config.add_route('{}.bulk_delete'.format(route_prefix), '{}/bulk-delete'.format(url_prefix))
config.add_view(cls, attr='bulk_delete', route_name='{}.bulk_delete'.format(route_prefix),
permission='{}.bulk_delete'.format(permission_prefix))
config.add_tailbone_permission(permission_prefix, '{}.bulk_delete'.format(permission_prefix),
"Bulk delete {}".format(model_title_plural))
# merge
if cls.mergeable:
config.add_route('{}.merge'.format(route_prefix), '{}/merge'.format(url_prefix))