From 2d17f957f16598103173882a52b155acabd61003 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 15 Sep 2019 19:52:28 +1000 Subject: [PATCH] Remove code duplication for part data export --- InvenTree/part/admin.py | 2 + InvenTree/part/views.py | 105 ++-------------------------------------- 2 files changed, 6 insertions(+), 101 deletions(-) diff --git a/InvenTree/part/admin.py b/InvenTree/part/admin.py index 7a9def473f..331a3f943b 100644 --- a/InvenTree/part/admin.py +++ b/InvenTree/part/admin.py @@ -31,6 +31,8 @@ class PartResource(ModelResource): variant_of = Field(attribute='variant_of', widget=widgets.ForeignKeyWidget(Part)) + suppliers = Field(attribute='supplier_count', readonly=True) + # Extra calculated meta-data (readonly) in_stock = Field(attribute='total_stock', readonly=True, widget=widgets.IntegerWidget()) diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 7f66cd7718..9e1b5491e8 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -30,6 +30,8 @@ from company.models import SupplierPart from . import forms as part_forms from .bom import MakeBomTemplate, BomUploadManager +from .admin import PartResource + from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDeleteView from InvenTree.views import QRCodeView @@ -1216,108 +1218,9 @@ class PartExport(AjaxView): parts = self.get_parts(request) - headers = [ - 'ID', - 'Name', - 'Description', - 'Category', - 'Category ID', - 'IPN', - 'Revision', - 'URL', - 'Keywords', - 'Notes', - 'Assembly', - 'Component', - 'Template', - 'Trackable', - 'Purchaseable', - 'Salable', - 'Active', - 'Virtual', + dataset = PartResource().export(queryset=parts) - # Part meta-data - 'Used In', - - # Stock Information - 'Stock Info', - 'In Stock', - 'Allocated', - 'Building', - 'On Order', - ] - - # Construct list of suppliers for each part - supplier_names = set() - - for part in parts: - supplier_parts = part.supplier_parts.all() - part.suppliers = {} - - for sp in supplier_parts: - name = sp.supplier.name - supplier_names.add(name) - part.suppliers[name] = sp - - if len(supplier_names) > 0: - headers.append('Suppliers') - for name in supplier_names: - headers.append(name) - - data = tablib.Dataset(headers=headers) - - for part in parts: - line = [] - - line.append(part.pk) - line.append(part.name) - line.append(part.description) - - if part.category: - line.append(str(part.category)) - line.append(part.category.pk) - else: - line.append('') - line.append('') - - line.append(part.IPN) - line.append(part.revision) - line.append(part.URL) - line.append(part.keywords) - line.append(part.notes) - line.append(part.assembly) - line.append(part.component) - line.append(part.is_template) - line.append(part.trackable) - line.append(part.purchaseable) - line.append(part.salable) - line.append(part.active) - line.append(part.virtual) - - # Part meta-data - line.append(part.used_in_count) - - # Stock information - line.append('') - line.append(part.total_stock) - line.append(part.allocation_count) - line.append(part.quantity_being_built) - line.append(part.on_order) - - # Supplier Information - if len(supplier_names) > 0: - line.append('') - - for name in supplier_names: - sp = part.suppliers.get(name, None) - if sp: - line.append(sp.SKU) - else: - line.append('') - - data.append(line) - - csv = data.export('csv') + csv = dataset.export('csv') return DownloadFile(csv, 'InvenTree_Parts.csv')