From 5a9fedbb9a789ce01aa4c5e890187d38fb0bf91a Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 17 Oct 2018 18:29:49 -0500 Subject: [PATCH] Add basic Excel download support for Products table --- tailbone/views/products.py | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tailbone/views/products.py b/tailbone/views/products.py index ff816be4..e99ca2fb 100644 --- a/tailbone/views/products.py +++ b/tailbone/views/products.py @@ -78,6 +78,7 @@ class ProductsView(MasterView): model_class = model.Product supports_mobile = True has_versions = True + results_downloadable_xlsx = True labels = { 'upc': "UPC", @@ -471,6 +472,67 @@ class ProductsView(MasterView): if classes: return ' '.join(classes) + def get_xlsx_fields(self): + fields = super(ProductsView, self).get_xlsx_fields() + + i = fields.index('department_uuid') + fields.insert(i + 1, 'department_number') + + i = fields.index('subdepartment_uuid') + fields.insert(i + 1, 'subdepartment_number') + + i = fields.index('category_uuid') + fields.insert(i + 1, 'category_code') + + i = fields.index('family_uuid') + fields.insert(i + 1, 'family_code') + + i = fields.index('report_code_uuid') + fields.insert(i + 1, 'report_code') + + i = fields.index('deposit_link_uuid') + fields.insert(i + 1, 'deposit_link_code') + + i = fields.index('tax_uuid') + fields.insert(i + 1, 'tax_code') + + i = fields.index('brand_uuid') + fields.insert(i + 1, 'brand_name') + + return fields + + def get_xlsx_row(self, product, fields): + row = super(ProductsView, self).get_xlsx_row(product, fields) + + if 'upc' in fields and isinstance(row['upc'], GPC): + row['upc'] = row['upc'].pretty() + + if 'department_number' in fields: + row['department_number'] = product.department.number if product.department else None + + if 'subdepartment_number' in fields: + row['subdepartment_number'] = product.subdepartment.number if product.subdepartment else None + + if 'category_code' in fields: + row['category_code'] = product.category.code if product.category else None + + if 'family_code' in fields: + row['family_code'] = product.family.code if product.family else None + + if 'report_code' in fields: + row['report_code'] = product.report_code.code if product.report_code else None + + if 'deposit_link_code' in fields: + row['deposit_link_code'] = product.deposit_link.code if product.deposit_link else None + + if 'tax_code' in fields: + row['tax_code'] = product.tax.code if product.tax else None + + if 'brand_name' in fields: + row['brand_name'] = product.brand.name if product.brand else None + + return row + def get_instance(self): key = self.request.matchdict['uuid'] product = Session.query(model.Product).get(key)