From 073bb7c48823a9b32ba6ac035c2d21a0a05790ba Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 3 Aug 2021 10:03:05 +1000 Subject: [PATCH] Merge pull request #1894 from SchrodingersGat/non-int-serial-fix Fix for non-integer serial numbers (cherry picked from commit 529742b5203005d5d71921c1ee32cd1bc540af4d) --- .../stock/templates/stock/item_base.html | 2 +- InvenTree/stock/views.py | 44 +++++++++++++------ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index b16d9b0b1a..91c3209013 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -253,7 +253,7 @@ {{ previous.serial }} ‹ {% endif %} - {{ item.serial }} + {{ item.serial }} {% if next %} {{ next.serial }} diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index 80968e5aa9..7b6fbc527e 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -92,21 +92,39 @@ class StockItemDetail(InvenTreeRoleMixin, DetailView): data = super().get_context_data(**kwargs) if self.object.serialized: - serial_elem = {int(a.serial): a for a in self.object.part.stock_items.all() if a.serialized} - serials = serial_elem.keys() - current = int(self.object.serial) - # previous - for nbr in range(current - 1, -1, -1): - if nbr in serials: - data['previous'] = serial_elem.get(nbr, None) - break + serial_elem = {} - # next - for nbr in range(current + 1, max(serials) + 1): - if nbr in serials: - data['next'] = serial_elem.get(nbr, None) - break + try: + current = int(self.object.serial) + + for item in self.object.part.stock_items.all(): + + if item.serialized: + try: + sn = int(item.serial) + serial_elem[sn] = item + except ValueError: + # We only support integer serial number progression + pass + + serials = serial_elem.keys() + + # previous + for nbr in range(current - 1, min(serials), -1): + if nbr in serials: + data['previous'] = serial_elem.get(nbr, None) + break + + # next + for nbr in range(current + 1, max(serials) + 1): + if nbr in serials: + data['next'] = serial_elem.get(nbr, None) + break + + except ValueError: + # We only support integer serial number progression + pass return data