mirror of
https://github.com/inventree/InvenTree.git
synced 2026-03-15 16:40:56 +00:00
Reimplement checks for locked parts
This commit is contained in:
@@ -627,6 +627,20 @@ class InvenTreeParameterMixin(InvenTreePermissionCheckMixin, models.Model):
|
||||
|
||||
return params
|
||||
|
||||
def check_parameter_delete(self, parameter):
|
||||
"""Run a check to determine if the provided parameter can be deleted.
|
||||
|
||||
The default implementation always returns True, but this can be overridden in the implementing class.
|
||||
"""
|
||||
return True
|
||||
|
||||
def check_parameter_save(self, parameter):
|
||||
"""Run a check to determine if the provided parameter can be saved.
|
||||
|
||||
The default implementation always returns True, but this can be overridden in the implementing class.
|
||||
"""
|
||||
return True
|
||||
|
||||
|
||||
class InvenTreeAttachmentMixin(InvenTreePermissionCheckMixin):
|
||||
"""Provides an abstracted class for managing file attachments.
|
||||
|
||||
@@ -2584,8 +2584,6 @@ class Parameter(
|
||||
|
||||
- Update the numeric data field (if applicable)
|
||||
"""
|
||||
# TODO: Custom call against the model type this is linked to...
|
||||
|
||||
self.calculate_numeric_value()
|
||||
|
||||
# Convert 'boolean' values to 'True' / 'False'
|
||||
@@ -2593,11 +2591,12 @@ class Parameter(
|
||||
self.data = InvenTree.helpers.str2bool(self.data)
|
||||
self.data_numeric = 1 if self.data else 0
|
||||
|
||||
self.check_save()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def delete(self):
|
||||
"""Perform custom delete checks before deleting a Parameter instance."""
|
||||
# TODO: Custom delete checks against the model type this is linked to...
|
||||
self.check_delete()
|
||||
super().delete()
|
||||
|
||||
def clean(self):
|
||||
@@ -2681,6 +2680,34 @@ class Parameter(
|
||||
|
||||
return model_class.check_related_permission(permission, user)
|
||||
|
||||
def check_save(self):
|
||||
"""Check if this parameter can be saved.
|
||||
|
||||
The linked content_object can implement custom checks by overriding
|
||||
the 'check_parameter_edit' method.
|
||||
"""
|
||||
from InvenTree.models import InvenTreeParameterMixin
|
||||
|
||||
try:
|
||||
instance = self.content_object
|
||||
except InvenTree.models.InvenTreeModel.DoesNotExist:
|
||||
return
|
||||
|
||||
if instance and isinstance(instance, InvenTreeParameterMixin):
|
||||
instance.check_parameter_save(self)
|
||||
|
||||
def check_delete(self):
|
||||
"""Check if this parameter can be deleted."""
|
||||
from InvenTree.models import InvenTreeParameterMixin
|
||||
|
||||
try:
|
||||
instance = self.content_object
|
||||
except InvenTree.models.InvenTreeModel.DoesNotExist:
|
||||
return
|
||||
|
||||
if instance and isinstance(instance, InvenTreeParameterMixin):
|
||||
instance.check_parameter_delete(self)
|
||||
|
||||
# TODO: Reintroduce validator for model_type
|
||||
model_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
||||
|
||||
|
||||
@@ -591,6 +591,16 @@ class Part(
|
||||
'test_templates': self.getTestTemplateMap(),
|
||||
}
|
||||
|
||||
def check_parameter_delete(self, parameter):
|
||||
"""Custom delete check for Paramteter instances associated with this Part."""
|
||||
if self.locked:
|
||||
raise ValidationError(_('Cannot delete parameters of a locked part'))
|
||||
|
||||
def check_parameter_save(self, parameter):
|
||||
"""Custom save check for Parameter instances associated with this Part."""
|
||||
if self.locked:
|
||||
raise ValidationError(_('Cannot modify parameters of a locked part'))
|
||||
|
||||
def delete(self, **kwargs):
|
||||
"""Custom delete method for the Part model.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user