diff --git a/tailbone_corepos/views/corepos/master.py b/tailbone_corepos/views/corepos/master.py index e59d1b6..3d47ceb 100644 --- a/tailbone_corepos/views/corepos/master.py +++ b/tailbone_corepos/views/corepos/master.py @@ -66,6 +66,10 @@ class CoreOfficeMasterView(MasterView): return CoreOfficeSession + def results_xlsx_session(self): + from corepos.db.office_op import Session as CoreSession + return CoreSession() + def render_local_date(self, obj, field): value = getattr(obj, field) if not value: diff --git a/tailbone_corepos/views/corepos/products.py b/tailbone_corepos/views/corepos/products.py index 6c53e21..2cbee3d 100644 --- a/tailbone_corepos/views/corepos/products.py +++ b/tailbone_corepos/views/corepos/products.py @@ -143,16 +143,29 @@ class ProductView(CoreOfficeMasterView): def get_xlsx_fields(self): fields = super(ProductView, self).get_xlsx_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_xlsx_row(self, product, fields): row = super(ProductView, self).get_xlsx_row(product, fields) row['superdepartment_number'] = None - dept = product.department - if dept and dept._super_parents: - row['superdepartment_number'] = dept._super_parents[0].parent_id + if product.department_number in self.supermap: + row['superdepartment_number'] = self.supermap[product.department_number] return row