Allow bulk-delete, merge for Brands table

This commit is contained in:
Lance Edgar 2020-03-27 18:15:33 -05:00
parent aaabde5c5a
commit 65f41480eb
2 changed files with 31 additions and 1 deletions

View file

@ -147,7 +147,7 @@
% if master.mergeable and request.has_perm('{}.merge'.format(permission_prefix)): % if master.mergeable and request.has_perm('{}.merge'.format(permission_prefix)):
% if use_buefy: % if use_buefy:
${h.form(url('{}.merge'.format(route_prefix)), **{'@submit': 'submitMergeForm'})} ${h.form(url('{}.merge'.format(route_prefix)), class_='control', **{'@submit': 'submitMergeForm'})}
% else: % else:
${h.form(url('{}.merge'.format(route_prefix)), name='merge-things')} ${h.form(url('{}.merge'.format(route_prefix)), name='merge-things')}
% endif % endif

View file

@ -37,6 +37,16 @@ class BrandsView(MasterView):
""" """
model_class = model.Brand model_class = model.Brand
has_versions = True has_versions = True
bulk_deletable = True
mergeable = True
merge_additive_fields = [
'product_count',
]
merge_fields = merge_additive_fields + [
'uuid',
'name',
]
grid_columns = [ grid_columns = [
'name', 'name',
@ -60,6 +70,26 @@ class BrandsView(MasterView):
# confirmed # confirmed
g.set_type('confirmed', 'boolean') g.set_type('confirmed', 'boolean')
def get_merge_data(self, brand):
product_count = self.Session.query(model.Product)\
.filter(model.Product.brand == brand)\
.count()
return {
'uuid': brand.uuid,
'name': brand.name,
'product_count': product_count,
}
def merge_objects(self, removing, keeping):
products = self.Session.query(model.Product)\
.filter(model.Product.brand == removing)\
.all()
for product in products:
product.brand = keeping
self.Session.flush()
self.Session.delete(removing)
class BrandsAutocomplete(AutocompleteView): class BrandsAutocomplete(AutocompleteView):