mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Make serial number suggestion DB independent, handle mixed types more cleanly and test
This commit is contained in:
parent
471ece136e
commit
d5a374f1fd
@ -330,14 +330,21 @@ class Part(MPTTModel):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
parts = Part.objects.filter(tree_id=self.tree_id)
|
parts = Part.objects.filter(tree_id=self.tree_id)
|
||||||
stock = StockModels.StockItem.objects.filter(part__in=parts).exclude(serial=None).annotate(
|
stock = StockModels.StockItem.objects.filter(part__in=parts).exclude(serial=None)
|
||||||
serial_as_int=Cast('serial', output_field=IntegerField())).order_by('-serial_as_int')
|
|
||||||
|
|
||||||
if stock.count() > 0:
|
try:
|
||||||
return stock.first().serial
|
ordered = sorted(stock.all(), reverse=True, key=lambda n: int(n.serial))
|
||||||
|
|
||||||
|
if len(ordered) > 0:
|
||||||
|
return ordered[0].serial
|
||||||
|
|
||||||
|
# Non-numeric serials, so don't suggest one.
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
|
||||||
# No serial numbers found
|
# No serial numbers found
|
||||||
return None
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def getNextSerialNumber(self):
|
def getNextSerialNumber(self):
|
||||||
"""
|
"""
|
||||||
@ -347,12 +354,9 @@ class Part(MPTTModel):
|
|||||||
n = self.getHighestSerialNumber()
|
n = self.getHighestSerialNumber()
|
||||||
|
|
||||||
if n is None:
|
if n is None:
|
||||||
return 1
|
return None
|
||||||
else:
|
else:
|
||||||
try:
|
return int(n) + 1
|
||||||
return int(n) + 1
|
|
||||||
except ValueError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def getSerialNumberString(self, quantity):
|
def getSerialNumberString(self, quantity):
|
||||||
|
@ -391,6 +391,11 @@ class VariantTest(StockTest):
|
|||||||
with self.assertRaises(ValidationError):
|
with self.assertRaises(ValidationError):
|
||||||
item.save()
|
item.save()
|
||||||
|
|
||||||
|
# Verify items with a non-numeric serial don't offer a next serial.
|
||||||
|
item.serial="string"
|
||||||
|
item.save()
|
||||||
|
self.assertEqual(variant.getNextSerialNumber(), None)
|
||||||
|
|
||||||
# This should pass, although not strictly an int field now.
|
# This should pass, although not strictly an int field now.
|
||||||
item.serial = int(n) + 1
|
item.serial = int(n) + 1
|
||||||
item.save()
|
item.save()
|
||||||
|
Loading…
Reference in New Issue
Block a user