mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Remove code duplication for part data export
This commit is contained in:
parent
2bc97764c7
commit
2d17f957f1
@ -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())
|
||||
|
||||
|
@ -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')
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user