Optimize the CSV download for CORE products table

This commit is contained in:
Lance Edgar 2020-08-21 18:29:02 -05:00
parent 9c0c7cc08c
commit 6270ec7d46
2 changed files with 33 additions and 0 deletions

View file

@ -66,6 +66,10 @@ class CoreOfficeMasterView(MasterView):
return CoreOfficeSession return CoreOfficeSession
def results_csv_session(self):
from corepos.db.office_op import Session as CoreSession
return CoreSession()
def results_xlsx_session(self): def results_xlsx_session(self):
from corepos.db.office_op import Session as CoreSession from corepos.db.office_op import Session as CoreSession
return CoreSession() return CoreSession()

View file

@ -40,6 +40,7 @@ class ProductView(CoreOfficeMasterView):
model_title = "CORE-POS Product" model_title = "CORE-POS Product"
url_prefix = '/core-pos/products' url_prefix = '/core-pos/products'
route_prefix = 'corepos.products' route_prefix = 'corepos.products'
results_downloadable_csv = True
results_downloadable_xlsx = True results_downloadable_xlsx = True
labels = { labels = {
@ -141,6 +142,34 @@ class ProductView(CoreOfficeMasterView):
return '{}/item/ItemEditorPage.php?searchupc={}'.format( return '{}/item/ItemEditorPage.php?searchupc={}'.format(
office_url, product.upc) office_url, product.upc)
def get_csv_fields(self):
fields = super(ProductView, self).get_csv_fields()
# add our extra field
fields.append('superdepartment_number')
# but also, go ahead and cache the superdept mapping, for performance
mapping = {}
super_departments = self.Session.query(corepos.SuperDepartment).all()
for superdept in super_departments:
if superdept.child_id in mapping:
if superdept.parent_id < mapping[superdept.child_id]:
mapping[superdept.child_id] = superdept.parent_id
else:
mapping[superdept.child_id] = superdept.parent_id
self.supermap = mapping
return fields
def get_csv_row(self, product, fields):
row = super(ProductView, self).get_csv_row(product, fields)
row['superdepartment_number'] = None
if product.department_number in self.supermap:
row['superdepartment_number'] = self.supermap[product.department_number]
return row
def get_xlsx_fields(self): def get_xlsx_fields(self):
fields = super(ProductView, self).get_xlsx_fields() fields = super(ProductView, self).get_xlsx_fields()