From 4989e8634969a0c55674f8d48e84a3adb217c846 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 25 Jun 2024 11:48:00 +1000 Subject: [PATCH] Error checking for attachment model type (#7506) --- src/backend/InvenTree/common/models.py | 2 +- src/backend/InvenTree/common/validators.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/backend/InvenTree/common/models.py b/src/backend/InvenTree/common/models.py index 3a5a7ad7c2..645abd28e6 100644 --- a/src/backend/InvenTree/common/models.py +++ b/src/backend/InvenTree/common/models.py @@ -3241,6 +3241,6 @@ class Attachment(InvenTree.models.MetadataMixin, InvenTree.models.InvenTreeModel ) if not issubclass(model_class, InvenTreeAttachmentMixin): - raise ValueError(_('Invalid model type specified for attachment')) + raise ValidationError(_('Invalid model type specified for attachment')) return model_class.check_attachment_permission(permission, user) diff --git a/src/backend/InvenTree/common/validators.py b/src/backend/InvenTree/common/validators.py index 5edd4a0f9a..a806e6cc0d 100644 --- a/src/backend/InvenTree/common/validators.py +++ b/src/backend/InvenTree/common/validators.py @@ -29,11 +29,14 @@ def attachment_model_options(): def attachment_model_class_from_label(label: str): """Return the model class for the given label.""" + if not label: + raise ValidationError(_('No attachment model type provided')) + for model in attachment_model_types(): if model.__name__.lower() == label.lower(): return model - raise ValueError(f'Invalid attachment model label: {label}') + raise ValidationError(_('Invalid attachment model type') + f": '{label}'") def validate_attachment_model_type(value):