2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-18 04:55:44 +00:00

Parameter validation via plugin (#4958)

* Expose part parameter validation to plugins

- Allow ValidationMixin plugins to validate part parameter values

* Update sample plugin

* Catch and re-throw error

* Update docs

* Improve plugin docs

* Simplify validation sample

* Calculate numeric value first
This commit is contained in:
Oliver
2023-06-03 21:27:31 +10:00
committed by GitHub
parent 1d85b70313
commit b0338e181e
10 changed files with 103 additions and 26 deletions

View File

@ -572,7 +572,7 @@ class Part(InvenTreeBarcodeMixin, InvenTreeNotesMixin, MetadataMixin, MPTTModel)
"""Ensure that the IPN (internal part number) is valid for this Part"
- Validation is handled by custom plugins
- By default, no validation checks are perfomed
- By default, no validation checks are performed
"""
from plugin.registry import registry
@ -3505,6 +3505,24 @@ class PartParameter(MetadataMixin, models.Model):
'data': _('Invalid choice for parameter value')
})
self.calculate_numeric_value()
# Run custom validation checks (via plugins)
from plugin.registry import registry
for plugin in registry.with_mixin('validation'):
# Note: The validate_part_parameter function may raise a ValidationError
try:
result = plugin.validate_part_parameter(self, self.data)
if result:
break
except ValidationError as exc:
# Re-throw the ValidationError against the 'data' field
raise ValidationError({
'data': exc.message
})
def calculate_numeric_value(self):
"""Calculate a numeric value for the parameter data.