2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-12-16 17:28:11 +00:00

Run plugin validation against parameter

This commit is contained in:
Oliver Walters
2025-11-25 01:46:20 +00:00
parent 009aba81dd
commit 5a4e84c83a
4 changed files with 28 additions and 17 deletions

View File

@@ -547,7 +547,7 @@ class InvenTreeParameterMixin(InvenTreePermissionCheckMixin, models.Model):
Before deleting the model instance, delete any associated parameters. Before deleting the model instance, delete any associated parameters.
""" """
self.parameters.all().delete() self.parameters_list.all().delete()
super().delete(*args, **kwargs) super().delete(*args, **kwargs)

View File

@@ -50,6 +50,7 @@ from taggit.managers import TaggableManager
import common.validators import common.validators
import InvenTree.conversion import InvenTree.conversion
import InvenTree.exceptions
import InvenTree.fields import InvenTree.fields
import InvenTree.helpers import InvenTree.helpers
import InvenTree.models import InvenTree.models
@@ -2585,6 +2586,15 @@ class Parameter(
"""Validate the Parameter before saving to the database.""" """Validate the Parameter before saving to the database."""
super().clean() super().clean()
# Validate the parameter data against the template choices
if choices := self.template.get_choices():
if self.data not in choices:
raise ValidationError({'data': _('Invalid choice for parameter value')})
self.calculate_numeric_value()
# TODO: Check that the model_type for this parameter matches the template
# Validate the parameter data against the template units # Validate the parameter data against the template units
if ( if (
get_global_setting( get_global_setting(
@@ -2599,18 +2609,21 @@ class Parameter(
except ValidationError as e: except ValidationError as e:
raise ValidationError({'data': e.message}) raise ValidationError({'data': e.message})
# Validate the parameter data against the template choices # Finally, run custom validation checks (via plugins)
if choices := self.template.get_choices(): from plugin import PluginMixinEnum, registry
if self.data not in choices:
raise ValidationError({'data': _('Invalid choice for parameter value')})
self.calculate_numeric_value() for plugin in registry.with_mixin(PluginMixinEnum.VALIDATION):
# Note: The validate_part_parameter function may raise a ValidationError
# TODO: Check that the model_type for this parameter matches the template try:
if hasattr(plugin, 'validate_parameter'):
# TODO: Validation of units (check global setting) result = plugin.validate_parameter(self, self.data)
if result:
# TODO: Validate against plugins break
except ValidationError as exc:
# Re-throw the ValidationError against the 'data' field
raise ValidationError({'data': exc.message})
except Exception:
InvenTree.exceptions.log_error('validate_parameter', plugin=plugin.slug)
def calculate_numeric_value(self): def calculate_numeric_value(self):
"""Calculate a numeric value for the parameter data. """Calculate a numeric value for the parameter data.

View File

@@ -227,9 +227,7 @@ class ValidationMixin:
""" """
return None return None
def validate_part_parameter( def validate_parameter(self, parameter, data: str) -> Optional[bool]:
self, parameter: part.models.PartParameter, data: str
) -> Optional[bool]:
"""Validate a parameter value. """Validate a parameter value.
Arguments: Arguments:

View File

@@ -111,8 +111,8 @@ class SampleValidatorPlugin(SettingsMixin, ValidationMixin, InvenTreePlugin):
if self.get_setting('IPN_MUST_CONTAIN_Q') and 'Q' not in ipn: if self.get_setting('IPN_MUST_CONTAIN_Q') and 'Q' not in ipn:
self.raise_error("IPN must contain 'Q'") self.raise_error("IPN must contain 'Q'")
def validate_part_parameter(self, parameter, data): def validate_parameter(self, parameter, data):
"""Validate part parameter data. """Validate parameter data.
These examples are silly, but serve to demonstrate how the feature could be used These examples are silly, but serve to demonstrate how the feature could be used
""" """