Add 'used_in' count to part export

This commit is contained in:
Oliver Walters 2019-09-09 19:37:59 +10:00
parent 8723a6c219
commit 776fc8b1e5

View File

@ -1162,15 +1162,53 @@ class BomUpload(FormView):
class PartExport(AjaxView):
""" Export a CSV file containing information on multiple parts """
def get(self, request, *args, **kwargs):
part = request.GET.get('parts', '')
parts = []
def get_parts(self, request):
""" Extract part list from the POST parameters.
Parts can be supplied as:
for pk in part.split(','):
- List of part PK values
- Part category
"""
part_list = Part.objects.all()
part_ids = request.GET.get('parts', '')
parts = []
for pk in part_ids.split(','):
try:
parts.append(Part.objects.get(pk=int(pk)))
except (Part.DoesNotExist, ValueError):
continue
parts.append(int(pk))
except ValueError:
pass
# Filter by list of Part IDs
if len(parts) > 0:
part_list = part_list.filter(pk__in=parts)
# Filter by part category
cat_id = request.GET.get('category', None)
if cat_id is not None:
try:
category = PartCategory.objects.get(cat_id)
part_list = part_list.filter(category=category)
except (ValueError, PartCategory.DoesNotExist):
pass
# Prefetch related fields to reduce DB hits
part_list = part_list.prefetch_related(
'category',
'used_in',
'builds',
'supplier_parts__purchase_order_line_items',
'stock_items__allocations',
)
return part_list
def get(self, request, *args, **kwargs):
parts = self.get_parts(request)
headers = [
'ID',
@ -1191,7 +1229,12 @@ class PartExport(AjaxView):
'Salable',
'Active',
'Virtual',
'Stock Info', # Spacer between part data and stock data
# Part meta-data
'Used In',
# Stock Information
'Stock Info',
'In Stock',
'Allocated',
'Building',
@ -1223,8 +1266,14 @@ class PartExport(AjaxView):
line.append(part.pk)
line.append(part.name)
line.append(part.description)
line.append(str(part.category))
line.append(part.category.pk)
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)
@ -1239,6 +1288,9 @@ class PartExport(AjaxView):
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)
@ -1246,6 +1298,7 @@ class PartExport(AjaxView):
line.append(part.quantity_being_built)
line.append(part.on_order)
# Supplier Information
if len(supplier_names) > 0:
line.append('')