Simplify code for exporting PurchaseOrder

- New resource for managing import/export of POLineItem model
This commit is contained in:
Oliver Walters 2019-09-15 22:04:52 +10:00
parent 204cd967aa
commit ed20e9d4a1
4 changed files with 28 additions and 60 deletions

View File

@ -2,8 +2,12 @@
from __future__ import unicode_literals
from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from import_export.resources import ModelResource
from import_export.fields import Field
from .models import PurchaseOrder, PurchaseOrderLineItem
@ -18,8 +22,28 @@ class PurchaseOrderAdmin(ImportExportModelAdmin):
)
class POLineItemResource(ModelResource):
""" Class for managing import / export of POLineItem data """
part_name = Field(attribute='part__part__name', readonly=True)
manufacturer = Field(attribute='part__manufacturer', readonly=True)
MPN = Field(attribute='part__MPN', readonly=True)
SKU = Field(attribute='part__SKU', readonly=True)
class Meta:
model = PurchaseOrderLineItem
skip_unchanged = True
report_skipped = False
clean_model_instances = True
class PurchaseOrderLineItemAdmin(ImportExportModelAdmin):
resource_class = POLineItemResource
list_display = (
'order',
'part',

View File

@ -12,7 +12,6 @@ from django.contrib.auth.models import User
from django.urls import reverse
from django.utils.translation import ugettext as _
import tablib
from datetime import datetime
from stock.models import StockItem
@ -126,54 +125,6 @@ class PurchaseOrder(Order):
related_name='+'
)
def export_to_file(self, **kwargs):
""" Export order information to external file """
file_format = kwargs.get('format', 'csv').lower()
data = tablib.Dataset(headers=[
'Line',
'Part',
'Description',
'Manufacturer',
'MPN',
'Order Code',
'Quantity',
'Received',
'Reference',
'Notes',
])
idx = 0
for item in self.lines.all():
line = []
line.append(idx)
if item.part:
line.append(item.part.part.name)
line.append(item.part.part.description)
line.append(item.part.manufacturer)
line.append(item.part.MPN)
line.append(item.part.SKU)
else:
line += [[] * 5]
line.append(item.quantity)
line.append(item.received)
line.append(item.reference)
line.append(item.notes)
idx += 1
data.append(line)
return data.export(file_format)
def get_absolute_url(self):
return reverse('purchase-order-detail', kwargs={'pk': self.id})

View File

@ -139,13 +139,3 @@ class OrderTest(TestCase):
order.receive_line_item(line, loc, line.quantity, user=None)
self.assertEqual(order.status, OrderStatus.COMPLETE)
def test_export(self):
""" Test order exporting """
order = PurchaseOrder.objects.get(pk=1)
output = order.export_to_file(format='csv')
self.assertIn('M2x4 LPHS', output)
self.assertIn('Line,Part,Description', output)

View File

@ -14,6 +14,7 @@ from django.forms import HiddenInput
import logging
from .models import PurchaseOrder, PurchaseOrderLineItem
from .admin import POLineItemResource
from build.models import Build
from company.models import Company, SupplierPart
from stock.models import StockItem, StockLocation
@ -165,7 +166,9 @@ class PurchaseOrderExport(AjaxView):
fmt=export_format
)
filedata = order.export_to_file(format=export_format)
dataset = POLineItemResource().export(queryset=order.lines.all())
filedata = dataset.export(format=export_format)
return DownloadFile(filedata, filename)