From 8e9e81243a377d4597bd89a0d6d2a35f2f2540ee Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Thu, 9 Nov 2023 21:36:10 +0530 Subject: [PATCH] raises validation error if file path and root are not unique #14187 --- netbox/extras/models/scripts.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/netbox/extras/models/scripts.py b/netbox/extras/models/scripts.py index 122f56f20..443dabdb3 100644 --- a/netbox/extras/models/scripts.py +++ b/netbox/extras/models/scripts.py @@ -2,6 +2,7 @@ import inspect import logging from functools import cached_property +from django.core.exceptions import ValidationError from django.db import models from django.urls import reverse from django.utils.translation import gettext_lazy as _ @@ -76,6 +77,14 @@ class ScriptModule(PythonModuleMixin, JobsMixin, ManagedFile): return scripts + def clean(self): + super().clean() + + # Ensure that the file root and path make a unique pair + if self.file_root and self.file_path: + if ScriptModule.objects.filter(file_root=self.file_root, file_path=self.file_path).exclude(pk=self.pk).exists(): + raise ValidationError(f"A script module with this file path already exists ({self.file_root}/{self.file_path}).") + def save(self, *args, **kwargs): self.file_root = ManagedFileRootPathChoices.SCRIPTS return super().save(*args, **kwargs)