Consolidate stock export code

- Now defined in stock.admin as StockItemResource
- Much more control over format of exported data
- Exported data can be re-imported!
This commit is contained in:
Oliver Walters 2019-09-15 19:29:18 +10:00
parent 66e439a836
commit 9c84e9076f
3 changed files with 9 additions and 68 deletions

View File

@ -103,6 +103,7 @@ def GetExportFormats():
'xls', 'xls',
'xlsx', 'xlsx',
'json', 'json',
'yaml',
] ]

View File

@ -61,6 +61,10 @@ class StockItemResource(ModelResource):
supplier_part = Field(attribute='supplier_part', widget=widgets.ForeignKeyWidget(SupplierPart)) supplier_part = Field(attribute='supplier_part', widget=widgets.ForeignKeyWidget(SupplierPart))
supplier = Field(attribute='supplier_part__supplier__id', readonly=True)
supplier_name = Field(attribute='supplier_part__supplier__name', readonly=True)
location = Field(attribute='location', widget=widgets.ForeignKeyWidget(StockLocation)) location = Field(attribute='location', widget=widgets.ForeignKeyWidget(StockLocation))
location_name = Field(attribute='location__name', readonly=True) location_name = Field(attribute='location__name', readonly=True)

View File

@ -29,6 +29,8 @@ from company.models import Company
from part.models import Part from part.models import Part
from .models import StockItem, StockLocation, StockItemTracking from .models import StockItem, StockLocation, StockItemTracking
from .admin import StockItemResource
from .forms import EditStockLocationForm from .forms import EditStockLocationForm
from .forms import CreateStockItemForm from .forms import CreateStockItemForm
from .forms import EditStockItemForm from .forms import EditStockItemForm
@ -226,75 +228,9 @@ class StockExport(AjaxView):
# Pre-fetch related fields to reduce DB queries # Pre-fetch related fields to reduce DB queries
stock_items = stock_items.prefetch_related('part', 'supplier_part__supplier', 'location', 'purchase_order', 'build') stock_items = stock_items.prefetch_related('part', 'supplier_part__supplier', 'location', 'purchase_order', 'build')
# Column headers dataset = StockItemResource().export(queryset=stock_items)
headers = [
_('Stock ID'),
_('Part ID'),
_('Part'),
_('Supplier Part ID'),
_('Supplier ID'),
_('Supplier'),
_('Location ID'),
_('Location'),
_('Quantity'),
_('Batch'),
_('Serial'),
_('Status'),
_('Notes'),
_('Review Needed'),
_('Last Updated'),
_('Last Stocktake'),
_('Purchase Order ID'),
_('Build ID'),
]
data = tablib.Dataset(headers=headers) filedata = dataset.export(export_format)
for item in stock_items:
line = []
line.append(item.pk)
line.append(item.part.pk)
line.append(item.part.full_name)
if item.supplier_part:
line.append(item.supplier_part.pk)
line.append(item.supplier_part.supplier.pk)
line.append(item.supplier_part.supplier.name)
else:
line.append('')
line.append('')
line.append('')
if item.location:
line.append(item.location.pk)
line.append(item.location.name)
else:
line.append('')
line.append('')
line.append(item.quantity)
line.append(item.batch)
line.append(item.serial)
line.append(StockStatus.label(item.status))
line.append(item.notes)
line.append(item.review_needed)
line.append(item.updated)
line.append(item.stocktake_date)
if item.purchase_order:
line.append(item.purchase_order.pk)
else:
line.append('')
if item.build:
line.append(item.build.pk)
else:
line.append('')
data.append(line)
filedata = data.export(export_format)
return DownloadFile(filedata, filename) return DownloadFile(filedata, filename)