mirror of
https://github.com/inventree/InvenTree.git
synced 2026-02-19 13:18:03 +00:00
[Plugin] Enhancements for serial number validation (#8311)
* Error handling for plugin.increment_serial_number * Improve error handling for plugin validation functions * Add Part.get_next_serial_number method - Simplify call for incrementing * ValidationMixin: add "part" to increment_serial_number func * Pass part information through to serial number functions * Fix circular imports * Allow "get_latest_serial_number" to use plugin system * Better working example for plugin sample * Update SampleValidatorPlugin * Fix indent * Add code comment * Cleanup code logic * Revert previous commit * Update unit tests
This commit is contained in:
@@ -135,6 +135,34 @@ class SampleValidatorPlugin(SettingsMixin, ValidationMixin, InvenTreePlugin):
|
||||
if serial[0] != part.name[0]:
|
||||
self.raise_error('Serial number must start with same letter as part')
|
||||
|
||||
# Prevent serial numbers which are a multiple of 5
|
||||
try:
|
||||
sn = int(serial)
|
||||
if sn % 5 == 0:
|
||||
self.raise_error('Serial number cannot be a multiple of 5')
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def increment_serial_number(self, serial: str, part=None, **kwargs):
|
||||
"""Increment a serial number.
|
||||
|
||||
These examples are silly, but serve to demonstrate how the feature could be used
|
||||
"""
|
||||
try:
|
||||
sn = int(serial)
|
||||
sn += 1
|
||||
|
||||
# Skip any serial number which is a multiple of 5
|
||||
if sn % 5 == 0:
|
||||
sn += 1
|
||||
|
||||
return str(sn)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# Return "None" to defer to the next plugin or builtin functionality
|
||||
return None
|
||||
|
||||
def validate_batch_code(self, batch_code: str, item):
|
||||
"""Ensure that a particular batch code meets specification.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user