From 73484192a59edd7e82b78e6c23bdc418d4e2a258 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Feb 2022 22:47:41 +1100 Subject: [PATCH 1/7] Add "batch code" and "serial numbers" serializer fields when receiving stock items against a purchase order --- InvenTree/order/models.py | 75 ++++++++++++++++++++++------------ InvenTree/order/serializers.py | 67 ++++++++++++++++++++++++++---- 2 files changed, 107 insertions(+), 35 deletions(-) diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index 7f6192dfcd..3b1cad2ff5 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -398,12 +398,22 @@ class PurchaseOrder(Order): return self.lines.count() > 0 and self.pending_line_items().count() == 0 @transaction.atomic - def receive_line_item(self, line, location, quantity, user, status=StockStatus.OK, purchase_price=None, **kwargs): - """ Receive a line item (or partial line item) against this PO + def receive_line_item(self, line, location, quantity, user, status=StockStatus.OK, **kwargs): + """ + Receive a line item (or partial line item) against this PO """ + # Extract optional batch code for the new stock item + batch_code = kwargs.get('batch_code', '') + + # Extract optional list of serial numbers + serials = kwargs.get('serials', None) + + # Extract optional notes field notes = kwargs.get('notes', '') - barcode = kwargs.get('barcode', '') + + # Extract optional barcode field + barcode = kwargs.get('barcode', None) # Prevent null values for barcode if barcode is None: @@ -427,33 +437,44 @@ class PurchaseOrder(Order): # Create a new stock item if line.part and quantity > 0: - stock = stock_models.StockItem( - part=line.part.part, - supplier_part=line.part, - location=location, - quantity=quantity, - purchase_order=self, - status=status, - purchase_price=line.purchase_price, - uid=barcode - ) - stock.save(add_note=False) + # Determine if we should individually serialize the items, or not + if type(serials) is list and len(serials) > 0: + quantity = 1 + else: + serials = [None] - tracking_info = { - 'status': status, - 'purchaseorder': self.pk, - } + for sn in serials: - stock.add_tracking_entry( - StockHistoryCode.RECEIVED_AGAINST_PURCHASE_ORDER, - user, - notes=notes, - deltas=tracking_info, - location=location, - purchaseorder=self, - quantity=quantity - ) + stock = stock_models.StockItem( + part=line.part.part, + supplier_part=line.part, + location=location, + quantity=quantity, + purchase_order=self, + status=status, + batch=batch_code, + serial=sn, + purchase_price=line.purchase_price, + uid=barcode + ) + + stock.save(add_note=False) + + tracking_info = { + 'status': status, + 'purchaseorder': self.pk, + } + + stock.add_tracking_entry( + StockHistoryCode.RECEIVED_AGAINST_PURCHASE_ORDER, + user, + notes=notes, + deltas=tracking_info, + location=location, + purchaseorder=self, + quantity=quantity + ) # Update the number of parts received against the particular line item line.received += quantity diff --git a/InvenTree/order/serializers.py b/InvenTree/order/serializers.py index 5e78b3e3a3..216bc63b11 100644 --- a/InvenTree/order/serializers.py +++ b/InvenTree/order/serializers.py @@ -5,6 +5,8 @@ JSON serializers for the Order API # -*- coding: utf-8 -*- from __future__ import unicode_literals +from decimal import Decimal + from django.utils.translation import ugettext_lazy as _ from django.core.exceptions import ValidationError as DjangoValidationError @@ -203,6 +205,17 @@ class POLineItemReceiveSerializer(serializers.Serializer): A serializer for receiving a single purchase order line item against a purchase order """ + class Meta: + fields = [ + 'barcode', + 'line_item', + 'location', + 'quantity', + 'status', + 'batch_code' + 'serial_numbers', + ] + line_item = serializers.PrimaryKeyRelatedField( queryset=order.models.PurchaseOrderLineItem.objects.all(), many=False, @@ -241,6 +254,22 @@ class POLineItemReceiveSerializer(serializers.Serializer): return quantity + batch_code = serializers.CharField( + label=_('Batch Code'), + help_text=_('Enter batch code for incoming stock items'), + required=False, + default='', + allow_blank=True, + ) + + serial_numbers = serializers.CharField( + label=_('Serial Numbers'), + help_text=_('Enter serial numbers for incoming stock items'), + required=False, + default='', + allow_blank=True, + ) + status = serializers.ChoiceField( choices=list(StockStatus.items()), default=StockStatus.OK, @@ -270,15 +299,35 @@ class POLineItemReceiveSerializer(serializers.Serializer): return barcode - class Meta: - fields = [ - 'barcode', - 'line_item', - 'location', - 'quantity', - 'status', - ] + def validate(self, data): + data = super().validate(data) + + line_item = data['line_item'] + quantity = data['quantity'] + serial_numbers = data.get('serial_numbers', '').strip() + + base_part = line_item.part.part + + # Does the quantity need to be "integer" (for trackable parts?) + if base_part.trackable: + + if Decimal(quantity) != int(quantity): + raise ValidationError({ + 'quantity': _('An integer quantity must be provided for trackable parts'), + }) + + # If serial numbers are provided + if serial_numbers: + try: + # Pass the serial numbers through to the parent serializer once validated + data['serials'] = extract_serial_numbers(serial_numbers, quantity, base_part.getLatestSerialNumberInt()) + except DjangoValidationError as e: + raise ValidationError({ + 'serial_numbers': e.messages, + }) + + return data class POReceiveSerializer(serializers.Serializer): """ @@ -366,6 +415,8 @@ class POReceiveSerializer(serializers.Serializer): request.user, status=item['status'], barcode=item.get('barcode', ''), + batch_code=item.get('batch_code', ''), + serials=item.get('serials', None), ) except (ValidationError, DjangoValidationError) as exc: # Catch model errors and re-throw as DRF errors From 9e0120599f21852f7da152614269f4ca87efff2d Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Feb 2022 22:48:15 +1100 Subject: [PATCH 2/7] Adds front-end javascript code to implement new serializer features --- InvenTree/templates/js/translated/forms.js | 17 ++--- InvenTree/templates/js/translated/order.js | 72 +++++++++++++++++++++- 2 files changed, 79 insertions(+), 10 deletions(-) diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index 9ee3dcace3..63598a8676 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -1976,7 +1976,7 @@ function constructField(name, parameters, options) { html += `
`; // Does this input deserve "extra" decorators? - var extra = parameters.prefix != null; + var extra = (parameters.icon != null) || (parameters.prefix != null) || (parameters.prefixRaw != null); // Some fields can have 'clear' inputs associated with them if (!parameters.required && !parameters.read_only) { @@ -1998,9 +1998,13 @@ function constructField(name, parameters, options) { if (extra) { html += `
`; - + if (parameters.prefix) { html += `${parameters.prefix}`; + } else if (parameters.prefixRaw) { + html += parameters.prefixRaw; + } else if (parameters.icon) { + html += ``; } } @@ -2147,6 +2151,10 @@ function constructInputOptions(name, classes, type, parameters, options={}) { opts.push(`type='${type}'`); + if (parameters.title || parameters.help_text) { + opts.push(`title='${parameters.title || parameters.help_text}'`); + } + // Read only? if (parameters.read_only) { opts.push(`readonly=''`); @@ -2192,11 +2200,6 @@ function constructInputOptions(name, classes, type, parameters, options={}) { opts.push(`required=''`); } - // Custom mouseover title? - if (parameters.title != null) { - opts.push(`title='${parameters.title}'`); - } - // Placeholder? if (parameters.placeholder != null) { opts.push(`placeholder='${parameters.placeholder}'`); diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index 008091bf15..0ec50022aa 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -476,6 +476,25 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) { quantity = 0; } + // Prepend toggles to the quantity input + var toggle_batch = ` + + + + `; + + var toggle_serials = ` + + + + `; + + var prefix_buttons = toggle_batch; + + if (line_item.part_detail.trackable) { + prefix_buttons += toggle_serials; + } + // Quantity to Receive var quantity_input = constructField( `items_quantity_${pk}`, @@ -485,12 +504,49 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) { value: quantity, title: '{% trans "Quantity to receive" %}', required: true, + prefixRaw: prefix_buttons, }, { hideLabels: true, } ); + // Add in options for "batch code" and "serial numbers" + var batch_input = constructField( + `items_batch_code_${pk}`, + { + type: 'string', + required: true, + label: '{% trans "Batch Code" %}', + help_text: '{% trans "Enter batch code for incoming stock items" %}', + prefixRaw: toggle_batch, + }, + { + hideLabels: true, + } + ); + + var sn_input = constructField( + `items_serial_numbers_${pk}`, + { + type: 'string', + required: true, + label: '{% trans "Serial Numbers" %}', + help_text: '{% trans "Enter serial numbers for incoming stock items" %}', + prefixRaw: toggle_serials, + }, + { + hideLabels: true + } + ); + + // Hidden inputs below the "quantity" field + var quantity_input_group = `${quantity_input}
${batch_input}
`; + + if (line_item.part_detail.trackable) { + quantity_input_group += `
${sn_input}
`; + } + // Construct list of StockItem status codes var choices = []; @@ -554,7 +610,7 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) { ${line_item.received} - ${quantity_input} + ${quantity_input_group} ${status_input} @@ -678,13 +734,23 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) { var location = getFormFieldValue(`items_location_${pk}`, {}, opts); if (quantity != null) { - data.items.push({ + + var line = { line_item: pk, quantity: quantity, status: status, location: location, - }); + }; + if (getFormFieldElement(`items_batch_code_${pk}`).exists()) { + line.batch_code = getFormFieldValue(`items_batch_code_${pk}`); + } + + if (getFormFieldElement(`items_serial_numbers_${pk}`).exists()) { + line.serial_numbers = getFormFieldValue(`items_serial_numbers_${pk}`); + } + + data.items.push(line); item_pk_values.push(pk); } From 421db61f21b89b04ea244d580d633fb4f3c07144 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Feb 2022 23:09:57 +1100 Subject: [PATCH 3/7] Adding unit testing for new features --- InvenTree/order/models.py | 5 +- InvenTree/order/serializers.py | 1 + InvenTree/order/test_api.py | 102 +++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/InvenTree/order/models.py b/InvenTree/order/models.py index 3b1cad2ff5..7f5c6b8164 100644 --- a/InvenTree/order/models.py +++ b/InvenTree/order/models.py @@ -440,8 +440,9 @@ class PurchaseOrder(Order): # Determine if we should individually serialize the items, or not if type(serials) is list and len(serials) > 0: - quantity = 1 + serialize = True else: + serialize = False serials = [None] for sn in serials: @@ -450,7 +451,7 @@ class PurchaseOrder(Order): part=line.part.part, supplier_part=line.part, location=location, - quantity=quantity, + quantity=1 if serialize else quantity, purchase_order=self, status=status, batch=batch_code, diff --git a/InvenTree/order/serializers.py b/InvenTree/order/serializers.py index 216bc63b11..cf8e701c68 100644 --- a/InvenTree/order/serializers.py +++ b/InvenTree/order/serializers.py @@ -329,6 +329,7 @@ class POLineItemReceiveSerializer(serializers.Serializer): return data + class POReceiveSerializer(serializers.Serializer): """ Serializer for receiving items against a purchase order diff --git a/InvenTree/order/test_api.py b/InvenTree/order/test_api.py index 2ea2086431..b6fee8a218 100644 --- a/InvenTree/order/test_api.py +++ b/InvenTree/order/test_api.py @@ -529,6 +529,108 @@ class PurchaseOrderReceiveTest(OrderTest): self.assertTrue(StockItem.objects.filter(uid='MY-UNIQUE-BARCODE-123').exists()) self.assertTrue(StockItem.objects.filter(uid='MY-UNIQUE-BARCODE-456').exists()) + def test_batch_code(self): + """ + Test that we can supply a 'batch code' when receiving items + """ + + line_1 = models.PurchaseOrderLineItem.objects.get(pk=1) + line_2 = models.PurchaseOrderLineItem.objects.get(pk=2) + + self.assertEqual(StockItem.objects.filter(supplier_part=line_1.part).count(), 0) + self.assertEqual(StockItem.objects.filter(supplier_part=line_2.part).count(), 0) + + data = { + 'items': [ + { + 'line_item': 1, + 'quantity': 10, + 'batch_code': 'abc-123', + }, + { + 'line_item': 2, + 'quantity': 10, + 'batch_code': 'xyz-789', + } + ], + 'location': 1, + } + + n = StockItem.objects.count() + + self.post( + self.url, + data, + expected_code=201, + ) + + # Check that two new stock items have been created! + self.assertEqual(n + 2, StockItem.objects.count()) + + item_1 = StockItem.objects.filter(supplier_part=line_1.part).first() + item_2 = StockItem.objects.filter(supplier_part=line_2.part).first() + + self.assertEqual(item_1.batch, 'abc-123') + self.assertEqual(item_2.batch, 'xyz-789') + + def test_serial_numbers(self): + """ + Test that we can supply a 'serial number' when receiving items + """ + + line_1 = models.PurchaseOrderLineItem.objects.get(pk=1) + line_2 = models.PurchaseOrderLineItem.objects.get(pk=2) + + self.assertEqual(StockItem.objects.filter(supplier_part=line_1.part).count(), 0) + self.assertEqual(StockItem.objects.filter(supplier_part=line_2.part).count(), 0) + + data = { + 'items': [ + { + 'line_item': 1, + 'quantity': 10, + 'batch_code': 'abc-123', + 'serial_numbers': '100+', + }, + { + 'line_item': 2, + 'quantity': 10, + 'batch_code': 'xyz-789', + } + ], + 'location': 1, + } + + n = StockItem.objects.count() + + self.post( + self.url, + data, + expected_code=201, + ) + + # Check that the expected number of stock items has been created + self.assertEqual(n + 11, StockItem.objects.count()) + + # 10 serialized stock items created for the first line item + self.assertEqual(StockItem.objects.filter(supplier_part=line_1.part).count(), 10) + + # Check that the correct serial numbers have been allocated + for i in range(100, 110): + item = StockItem.objects.get(serial_int=i) + self.assertEqual(item.serial, str(i)) + self.assertEqual(item.quantity, 1) + self.assertEqual(item.batch, 'abc-123') + + # A single stock item (quantity 10) created for the second line item + items = StockItem.objects.filter(supplier_part=line_2.part) + self.assertEqual(items.count(), 1) + + item = items.first() + + self.assertEqual(item.quantity, 10) + self.assertEqual(item.batch, 'xyz-789') + class SalesOrderTest(OrderTest): """ From fdc2cae6bac0daa7b1bfe640c487c8e9b5c48842 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Feb 2022 23:31:12 +1100 Subject: [PATCH 4/7] Fix some existing problems with the extract_serial_numbers helper function - Serial numbers don't really have to be "numbers" - Allow any text value once other higher-level checks have been performed --- InvenTree/InvenTree/helpers.py | 44 ++++++++++++++++------------------ 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index 2595f8b5c3..e260f71906 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -407,14 +407,16 @@ def DownloadFile(data, filename, content_type='application/text', inline=False): def extract_serial_numbers(serials, expected_quantity, next_number: int): - """ Attempt to extract serial numbers from an input string. - - Serial numbers must be integer values - - Serial numbers must be positive - - Serial numbers can be split by whitespace / newline / commma chars - - Serial numbers can be supplied as an inclusive range using hyphen char e.g. 10-20 - - Serial numbers can be defined as ~ for getting the next available serial number - - Serial numbers can be supplied as + for getting all expecteded numbers starting from - - Serial numbers can be supplied as + for getting numbers starting from + """ + Attempt to extract serial numbers from an input string: + + Requirements: + - Serial numbers can be either strings, or integers + - Serial numbers can be split by whitespace / newline / commma chars + - Serial numbers can be supplied as an inclusive range using hyphen char e.g. 10-20 + - Serial numbers can be defined as ~ for getting the next available serial number + - Serial numbers can be supplied as + for getting all expecteded numbers starting from + - Serial numbers can be supplied as + for getting numbers starting from Args: serials: input string with patterns @@ -428,17 +430,18 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int): if '~' in serials: serials = serials.replace('~', str(next_number)) + # Split input string by whitespace or comma (,) characters groups = re.split("[\s,]+", serials) numbers = [] errors = [] - # helpers - def number_add(n): - if n in numbers: - errors.append(_('Duplicate serial: {n}').format(n=n)) + # Helper function to check for duplicated numbers + def add_sn(sn): + if sn in numbers: + errors.append(_('Duplicate serial: {sn}').format(n=n)) else: - numbers.append(n) + numbers.append(sn) try: expected_quantity = int(expected_quantity) @@ -466,7 +469,7 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int): if a < b: for n in range(a, b + 1): - number_add(n) + add_sn(n) else: errors.append(_("Invalid group: {g}").format(g=group)) @@ -495,21 +498,16 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int): end = start + expected_quantity for n in range(start, end): - number_add(n) + add_sn(n) # no case else: errors.append(_("Invalid group: {g}").format(g=group)) - # Group should be a number + # At this point, we assume that the "group" is just a single serial value + # Note: At this point it is treated only as a string elif group: - # try conversion - try: - number = int(group) - except: - # seem like it is not a number - raise ValidationError(_(f"Invalid group {group}")) - number_add(number) + add_sn(group) # No valid input group detected else: From 615a954e094c748c4984d0c6d8429fc0ce2cf88e Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Feb 2022 23:31:23 +1100 Subject: [PATCH 5/7] Refactor UI for adding batch code and serial numbers --- InvenTree/templates/js/translated/forms.js | 2 +- InvenTree/templates/js/translated/helpers.js | 4 ++ InvenTree/templates/js/translated/order.js | 49 ++++++++++++-------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index 63598a8676..b3f54033e0 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -1884,7 +1884,7 @@ function getFieldName(name, options={}) { * - Field description (help text) * - Field errors */ -function constructField(name, parameters, options) { +function constructField(name, parameters, options={}) { var html = ''; diff --git a/InvenTree/templates/js/translated/helpers.js b/InvenTree/templates/js/translated/helpers.js index 2d35513e78..1925cb47ac 100644 --- a/InvenTree/templates/js/translated/helpers.js +++ b/InvenTree/templates/js/translated/helpers.js @@ -116,6 +116,10 @@ function makeIconButton(icon, cls, pk, title, options={}) { extraProps += `disabled='true' `; } + if (options.collapseTarget) { + extraProps += `data-bs-toggle='collapse' href='#${options.collapseTarget}'`; + } + html += ``; diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index 0ec50022aa..ab437df426 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -489,12 +489,6 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) { `; - var prefix_buttons = toggle_batch; - - if (line_item.part_detail.trackable) { - prefix_buttons += toggle_serials; - } - // Quantity to Receive var quantity_input = constructField( `items_quantity_${pk}`, @@ -504,7 +498,6 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) { value: quantity, title: '{% trans "Quantity to receive" %}', required: true, - prefixRaw: prefix_buttons, }, { hideLabels: true, @@ -516,13 +509,10 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) { `items_batch_code_${pk}`, { type: 'string', - required: true, + required: false, label: '{% trans "Batch Code" %}', help_text: '{% trans "Enter batch code for incoming stock items" %}', prefixRaw: toggle_batch, - }, - { - hideLabels: true, } ); @@ -530,13 +520,10 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) { `items_serial_numbers_${pk}`, { type: 'string', - required: true, + required: false, label: '{% trans "Serial Numbers" %}', help_text: '{% trans "Enter serial numbers for incoming stock items" %}', prefixRaw: toggle_serials, - }, - { - hideLabels: true } ); @@ -584,16 +571,38 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) { ); // Button to remove the row - var delete_button = `
`; + var buttons = `
`; - delete_button += makeIconButton( + buttons += makeIconButton( + 'fa-layer-group', + 'button-row-add-batch', + pk, + '{% trans "Add batch code" %}', + { + collapseTarget: `div-batch-${pk}` + } + ); + + if (line_item.part_detail.trackable) { + buttons += makeIconButton( + 'fa-hashtag', + 'button-row-add-serials', + pk, + '{% trans "Add serial numbers" %}', + { + collapseTarget: `div-serials-${pk}`, + } + ); + } + + buttons += makeIconButton( 'fa-times icon-red', 'button-row-remove', pk, '{% trans "Remove row" %}', ); - delete_button += '
'; + buttons += '
'; var html = ` @@ -619,7 +628,7 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) { ${destination_input} - ${delete_button} + ${buttons} `; @@ -643,7 +652,7 @@ function receivePurchaseOrderItems(order_id, line_items, options={}) { {% trans "Order Code" %} {% trans "Ordered" %} {% trans "Received" %} - {% trans "Receive" %} + {% trans "Quantity to Receive" %} {% trans "Status" %} {% trans "Destination" %} From 7ad9f8852e300e329d59771aec4bbe10139e372f Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Feb 2022 23:31:48 +1100 Subject: [PATCH 6/7] PEP fix --- InvenTree/InvenTree/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index e260f71906..d71f7d87c7 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -409,7 +409,7 @@ def DownloadFile(data, filename, content_type='application/text', inline=False): def extract_serial_numbers(serials, expected_quantity, next_number: int): """ Attempt to extract serial numbers from an input string: - + Requirements: - Serial numbers can be either strings, or integers - Serial numbers can be split by whitespace / newline / commma chars From 63d052e1ca4e7931072688f1fa4cc39f9b3117bd Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 1 Mar 2022 00:37:27 +1100 Subject: [PATCH 7/7] Fixes for unit tests - Prioritize "integer" values when extracting serial numbers --- InvenTree/InvenTree/helpers.py | 10 +++++++--- InvenTree/InvenTree/status.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index d71f7d87c7..b5cecf764d 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -439,7 +439,7 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int): # Helper function to check for duplicated numbers def add_sn(sn): if sn in numbers: - errors.append(_('Duplicate serial: {sn}').format(n=n)) + errors.append(_('Duplicate serial: {sn}').format(sn=sn)) else: numbers.append(sn) @@ -504,10 +504,14 @@ def extract_serial_numbers(serials, expected_quantity, next_number: int): errors.append(_("Invalid group: {g}").format(g=group)) # At this point, we assume that the "group" is just a single serial value - # Note: At this point it is treated only as a string elif group: - add_sn(group) + try: + # First attempt to add as an integer value + add_sn(int(group)) + except (ValueError): + # As a backup, add as a string value + add_sn(group) # No valid input group detected else: diff --git a/InvenTree/InvenTree/status.py b/InvenTree/InvenTree/status.py index cc9df701c6..181f431574 100644 --- a/InvenTree/InvenTree/status.py +++ b/InvenTree/InvenTree/status.py @@ -14,6 +14,8 @@ from django_q.monitor import Stat from django.conf import settings +import InvenTree.ready + logger = logging.getLogger("inventree") @@ -56,6 +58,12 @@ def is_email_configured(): configured = True + if InvenTree.ready.isInTestMode(): + return False + + if InvenTree.ready.isImportingData(): + return False + if not settings.EMAIL_HOST: configured = False @@ -89,6 +97,14 @@ def check_system_health(**kwargs): result = True + if InvenTree.ready.isInTestMode(): + # Do not perform further checks if we are running unit tests + return False + + if InvenTree.ready.isImportingData(): + # Do not perform further checks if we are importing data + return False + if not is_worker_running(**kwargs): # pragma: no cover result = False logger.warning(_("Background worker check failed"))