diff --git a/docs/docs/extend/plugins/validation.md b/docs/docs/extend/plugins/validation.md index 46b0b89474..20e4291a20 100644 --- a/docs/docs/extend/plugins/validation.md +++ b/docs/docs/extend/plugins/validation.md @@ -14,29 +14,35 @@ Any of the methods described below can be implemented in a custom plugin to prov !!! info "Multi Plugin Support" It is possible to have multiple plugins loaded simultaneously which support validation methods. For example when validating a field, if one plugin returns a null value (`None`) then the *next* plugin (if available) will be queried. +## Model Deletion + +Any model which inherits the `PluginValidationMixin` class is exposed to the plugin system for custom deletion validation. Before the model is deleted from the database, it is first passed to the plugin ecosystem to check if it really should be deleted. + +A custom plugin may implement the `validate_model_deletion` method to perform custom validation on the model instance before it is deleted. + +::: plugin.base.integration.ValidationMixin.ValidationMixin.validate_model_deletion + options: + show_bases: False + show_root_heading: False + show_root_toc_entry: False + show_sources: True + summary: False + members: [] + ## Model Validation Any model which inherits the `PluginValidationMixin` mixin class is exposed to the plugin system for custom validation. Before the model is saved to the database (either when created, or updated), it is first passed to the plugin ecosystem for validation. Any plugin which inherits the `ValidationMixin` can implement the `validate_model_instance` method, and run a custom validation routine. -The `validate_model_instance` method is passed the following arguments: - -| Argument | Description | -| --- | --- | -| `instance` | The model instance to be validated | -| `deltas` | A dict of field deltas (if the instance is being updated) | - -```python -def validate_model_instance(self, instance, deltas=None): - """Validate the supplied model instance. - - Arguments: - instance: The model instance to be validated - deltas: A dict of field deltas (if the instance is being updated) - """ - ... -``` +::: plugin.base.integration.ValidationMixin.ValidationMixin.validate_model_instance + options: + show_bases: False + show_root_heading: False + show_root_toc_entry: False + show_sources: True + summary: False + members: [] ### Error Messages diff --git a/src/backend/InvenTree/plugin/base/integration/ValidationMixin.py b/src/backend/InvenTree/plugin/base/integration/ValidationMixin.py index 2dcb6b03e5..d00c6753f6 100644 --- a/src/backend/InvenTree/plugin/base/integration/ValidationMixin.py +++ b/src/backend/InvenTree/plugin/base/integration/ValidationMixin.py @@ -1,6 +1,7 @@ """Validation mixin class definition.""" from django.core.exceptions import ValidationError +from django.db.models import Model import part.models import stock.models @@ -49,7 +50,7 @@ class ValidationMixin: """Raise a ValidationError with the given message.""" raise ValidationError(message) - def validate_model_deletion(self, instance): + def validate_model_deletion(self, instance: Model) -> None: """Run custom validation when a model instance is being deleted. This method is called when a model instance is being deleted. @@ -59,14 +60,14 @@ class ValidationMixin: instance: The model instance to validate Returns: - None or True (refer to class docstring) + None: or True (refer to class docstring) Raises: - ValidationError if the instance cannot be deleted + ValidationError: if the instance cannot be deleted """ return None - def validate_model_instance(self, instance, deltas=None): + def validate_model_instance(self, instance: Model, deltas: dict = None) -> None: """Run custom validation on a database model instance. This method is called when a model instance is being validated. @@ -77,10 +78,10 @@ class ValidationMixin: deltas: A dictionary of field names and updated values (if the instance is being updated) Returns: - None or True (refer to class docstring) + None: or True (refer to class docstring) Raises: - ValidationError if the instance is invalid + ValidationError: if the instance is invalid """ return None