2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-01 03:00:54 +00:00

Extend functionality of custom validation plugins (#4391)

* Pass "Part" instance to plugins when calling validate_serial_number

* Pass part instance through when validating IPN

* Improve custom part name validation

- Pass the Part instance through to the plugins
- Validation is performed at the model instance level
- Updates to sample plugin code

* Pass StockItem through when validating batch code

* Pass Part instance through when calling validate_serial_number

* Bug fix

* Update unit tests

* Unit test fixes

* Fixes for unit tests

* More unit test fixes

* More unit tests

* Furrther unit test fixes

* Simplify custom batch code validation

* Further improvements to unit tests

* Further unit test
This commit is contained in:
Oliver
2023-03-07 22:43:12 +11:00
committed by GitHub
parent edae82caa5
commit abeb85cbb3
22 changed files with 193 additions and 137 deletions

View File

@ -81,7 +81,7 @@
pk: 102
fields:
part: 25
batch: 'ABCDE'
batch: 'BCDE'
location: 7
quantity: 0
level: 0
@ -109,7 +109,7 @@
part: 10001
location: 7
quantity: 5
batch: "AAA"
batch: "BBAAA"
level: 0
tree_id: 0
lft: 0

View File

@ -529,7 +529,7 @@ class StockItem(InvenTreeBarcodeMixin, MetadataMixin, common.models.MetaMixin, M
for plugin in registry.with_mixin('validation'):
try:
plugin.validate_batch_code(self.batch)
plugin.validate_batch_code(self.batch, self)
except ValidationError as exc:
raise ValidationError({
'batch': exc.message
@ -560,6 +560,7 @@ class StockItem(InvenTreeBarcodeMixin, MetadataMixin, common.models.MetaMixin, M
if type(self.batch) is str:
self.batch = self.batch.strip()
# Custom validation of batch code
self.validate_batch_code()
try:

View File

@ -161,7 +161,7 @@ class StockLocationTest(StockAPITestCase):
# Create stock items in the location to be deleted
for jj in range(3):
stock_items.append(StockItem.objects.create(
batch=f"Stock Item xyz {jj}",
batch=f"Batch xyz {jj}",
location=stock_location_to_delete,
part=part
))
@ -180,7 +180,7 @@ class StockLocationTest(StockAPITestCase):
# Create stock items in the sub locations
for jj in range(3):
child_stock_locations_items.append(StockItem.objects.create(
batch=f"Stock item in sub location xyz {jj}",
batch=f"B xyz {jj}",
part=part,
location=child
))
@ -272,7 +272,7 @@ class StockLocationTest(StockAPITestCase):
# Create the test stock item located to a non-structural category
item = StockItem.objects.create(
batch="Item which will be tried to relocated to a structural location",
batch="BBB",
location=non_structural_location,
part=part
)
@ -951,7 +951,7 @@ class StockItemTest(StockAPITestCase):
# First, construct a set of template / variant parts
master_part = part.models.Part.objects.create(
name='Master', description='Master part',
name='Master', description='Master part which has variants',
category=category,
is_template=True,
)

View File

@ -181,8 +181,8 @@ class StockTest(StockTestBase):
# Ensure that 'global uniqueness' setting is enabled
InvenTreeSetting.set_setting('SERIAL_NUMBER_GLOBALLY_UNIQUE', True, self.user)
part_a = Part.objects.create(name='A', description='A', trackable=True)
part_b = Part.objects.create(name='B', description='B', trackable=True)
part_a = Part.objects.create(name='A', description='A part with a description', trackable=True)
part_b = Part.objects.create(name='B', description='B part with a description', trackable=True)
# Create a StockItem for part_a
StockItem.objects.create(
@ -577,10 +577,13 @@ class StockTest(StockTestBase):
"""Tests for stock serialization."""
p = Part.objects.create(
name='trackable part',
description='trackable part',
description='A trackable part which can be tracked',
trackable=True,
)
# Ensure we do not have unique serials enabled
InvenTreeSetting.set_setting('SERIAL_NUMBER_GLOBALLY_UNIQUE', False, None)
item = StockItem.objects.create(
part=p,
quantity=1,
@ -608,7 +611,7 @@ class StockTest(StockTestBase):
"""Unit tests for "large" serial numbers which exceed integer encoding."""
p = Part.objects.create(
name='trackable part',
description='trackable part',
description='A trackable part with really big serial numbers',
trackable=True,
)
@ -721,6 +724,9 @@ class StockTest(StockTestBase):
self.assertEqual(item.quantity, 10)
# Ensure we do not have unique serials enabled
InvenTreeSetting.set_setting('SERIAL_NUMBER_GLOBALLY_UNIQUE', False, None)
item.serializeStock(3, [1, 2, 3], self.user)
self.assertEqual(item.quantity, 7)
@ -1087,8 +1093,14 @@ class TestResultTest(StockTestBase):
item.pk = None
item.serial = None
item.quantity = 50
item.batch = "B344"
# Try with an invalid batch code (according to sample validatoin plugin)
item.batch = "X234"
with self.assertRaises(ValidationError):
item.save()
item.batch = "B123"
item.save()
# Do some tests!