Remove code duplication for part data export

This commit is contained in:
Oliver Walters 2019-09-15 19:52:28 +10:00
parent 2bc97764c7
commit 2d17f957f1
2 changed files with 6 additions and 101 deletions

View File

@ -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())

View File

@ -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')