def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) # Instead of using the DRF serializer to LIST, # we will serialize the objects manually. # This is significantly faster data = queryset.values( 'pk', 'quantity', 'serial', 'batch', 'status', 'notes', 'location', 'location__name', 'location__description', 'part', 'part__IPN', 'part__name', 'part__revision', 'part__description', 'part__image', 'part__category', 'part__category__name', 'part__category__description', ) # Reduce the number of lookups we need to do for categories # Cache location lookups for this query locations = {} for item in data: item['part__image'] = os.path.join(settings.MEDIA_URL, item['part__image']) loc_id = item['location'] if loc_id: if loc_id not in locations: locations[loc_id] = StockLocation.objects.get( pk=loc_id).pathstring item['location__path'] = locations[loc_id] else: item['location__path'] = None item['status_text'] = StockStatus.label(item['status']) return Response(data)
def status_label(self): return StockStatus.label(self.status)
def get(self, request, *args, **kwargs): export_format = request.GET.get('format', 'csv').lower() # Check if a particular location was specified loc_id = request.GET.get('location', None) location = None if loc_id: try: location = StockLocation.objects.get(pk=loc_id) except (ValueError, StockLocation.DoesNotExist): pass # Check if a particular supplier was specified sup_id = request.GET.get('supplier', None) supplier = None if sup_id: try: supplier = Company.objects.get(pk=sup_id) except (ValueError, Company.DoesNotExist): pass # Check if a particular part was specified part_id = request.GET.get('part', None) part = None if part_id: try: part = Part.objects.get(pk=part_id) except (ValueError, Part.DoesNotExist): pass if export_format not in GetExportFormats(): export_format = 'csv' filename = 'InvenTree_Stocktake_{date}.{fmt}'.format( date=datetime.now().strftime("%d-%b-%Y"), fmt=export_format) if location: # CHeck if locations should be cascading cascade = str2bool(request.GET.get('cascade', True)) stock_items = location.get_stock_items(cascade) else: cascade = True stock_items = StockItem.objects.all() if part: stock_items = stock_items.filter(part=part) if supplier: stock_items = stock_items.filter(supplier_part__supplier=supplier) # Filter out stock items that are not 'in stock' stock_items = stock_items.filter(customer=None) stock_items = stock_items.filter(belongs_to=None) # Pre-fetch related fields to reduce DB queries stock_items = stock_items.prefetch_related('part', 'supplier_part__supplier', 'location', 'purchase_order', 'build') # Column headers 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) 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)