Make serial number suggestion DB independent, handle mixed types more cleanly and test

This commit is contained in:
Ben Charlton 2020-08-24 19:49:32 +01:00
parent 471ece136e
commit d5a374f1fd
2 changed files with 20 additions and 11 deletions

View File

@ -330,14 +330,21 @@ class Part(MPTTModel):
"""
parts = Part.objects.filter(tree_id=self.tree_id)
stock = StockModels.StockItem.objects.filter(part__in=parts).exclude(serial=None).annotate(
serial_as_int=Cast('serial', output_field=IntegerField())).order_by('-serial_as_int')
if stock.count() > 0:
return stock.first().serial
stock = StockModels.StockItem.objects.filter(part__in=parts).exclude(serial=None)
try:
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
return None
return 0
def getNextSerialNumber(self):
"""
@ -347,12 +354,9 @@ class Part(MPTTModel):
n = self.getHighestSerialNumber()
if n is None:
return 1
return None
else:
try:
return int(n) + 1
except ValueError:
return None
return int(n) + 1
def getSerialNumberString(self, quantity):

View File

@ -391,6 +391,11 @@ class VariantTest(StockTest):
with self.assertRaises(ValidationError):
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.
item.serial = int(n) + 1
item.save()