From ef7a9b5152096d693871820083a7e31481d716d9 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 27 Nov 2021 00:11:18 +1100 Subject: [PATCH] Adds a simple endpoint for accessing serial number information for a Part instance - This is not included by default in the "part detail" endpoint as it must be calculated! --- InvenTree/part/api.py | 38 ++++++++++++++++++- .../stock/templates/stock/item_base.html | 1 + InvenTree/templates/js/translated/api.js | 1 + InvenTree/templates/js/translated/stock.js | 14 +++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 00b5dfa7de..403934d3d9 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -42,7 +42,7 @@ from build.models import Build from . import serializers as part_serializers -from InvenTree.helpers import str2bool, isNull +from InvenTree.helpers import str2bool, isNull, increment from InvenTree.api import AttachmentMixin from InvenTree.status_codes import BuildStatus @@ -410,6 +410,33 @@ class PartThumbsUpdate(generics.RetrieveUpdateAPIView): ] +class PartSerialNumberDetail(generics.RetrieveAPIView): + """ + API endpoint for returning extra serial number information about a particular part + """ + + queryset = Part.objects.all() + + def retrieve(self, request, *args, **kwargs): + + part = self.get_object() + + # Calculate the "latest" serial number + latest = part.getLatestSerialNumber() + + data = { + 'latest': latest, + } + + if latest is not None: + next = increment(latest) + + if next != increment: + data['next'] = next + + return Response(data) + + class PartDetail(generics.RetrieveUpdateDestroyAPIView): """ API endpoint for detail view of a single Part object """ @@ -1532,7 +1559,14 @@ part_api_urls = [ url(r'^(?P\d+)/?', PartThumbsUpdate.as_view(), name='api-part-thumbs-update'), ])), - url(r'^(?P\d+)/', PartDetail.as_view(), name='api-part-detail'), + url(r'^(?P\d+)/', include([ + + # Endpoint for extra serial number information + url(r'^serial-numbers/', PartSerialNumberDetail.as_view(), name='api-part-serial-number-detail'), + + # Part detail endpoint + url(r'^.*$', PartDetail.as_view(), name='api-part-detail'), + ])), url(r'^.*$', PartList.as_view(), name='api-part-list'), ] diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index 0f8d81203a..111007cd71 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -433,6 +433,7 @@ $("#stock-serialize").click(function() { serializeStockItem({{ item.pk }}, { + part: {{ item.part.pk }}, reload: true, data: { quantity: {{ item.quantity }}, diff --git a/InvenTree/templates/js/translated/api.js b/InvenTree/templates/js/translated/api.js index 735ce0a676..d9c23f035f 100644 --- a/InvenTree/templates/js/translated/api.js +++ b/InvenTree/templates/js/translated/api.js @@ -54,6 +54,7 @@ function inventreeGet(url, filters={}, options={}) { data: filters, dataType: 'json', contentType: 'application/json', + async: (options.async == false) ? false : true, success: function(response) { if (options.success) { options.success(response); diff --git a/InvenTree/templates/js/translated/stock.js b/InvenTree/templates/js/translated/stock.js index c624278c93..e5a59d7767 100644 --- a/InvenTree/templates/js/translated/stock.js +++ b/InvenTree/templates/js/translated/stock.js @@ -80,6 +80,20 @@ function serializeStockItem(pk, options={}) { notes: {}, }; + if (options.part) { + // Work out the next available serial number + inventreeGet(`/api/part/${options.part}/serial-numbers/`, {}, { + success: function(data) { + if (data.next) { + options.fields.serial_numbers.placeholder = `{% trans "Next available serial number" %}: ${data.next}`; + } else if (data.latest) { + options.fields.serial_numbers.placeholder = `{% trans "Latest serial number" %}: ${data.latest}`; + } + }, + async: false, + }); + } + constructForm(url, options); }