From 8e9e81243a377d4597bd89a0d6d2a35f2f2540ee Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Thu, 9 Nov 2023 21:36:10 +0530 Subject: [PATCH 1/2] 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 122f56f2065..443dabdb339 100644 --- a/netbox/extras/models/scripts.py +++ b/netbox/extras/models/scripts.py @@ -2,6 +2,7 @@ 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 @@ def _get_name(cls): 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) From 5a9739f5639fae575476d475eb834d5172ff5992 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Fri, 10 Nov 2023 02:21:01 +0530 Subject: [PATCH 2/2] review changes #14187 --- netbox/core/models/files.py | 9 +++++++++ netbox/extras/models/scripts.py | 9 --------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/netbox/core/models/files.py b/netbox/core/models/files.py index 38d82463eca..a9e0e7f0069 100644 --- a/netbox/core/models/files.py +++ b/netbox/core/models/files.py @@ -2,6 +2,7 @@ import os from django.conf import settings +from django.core.exceptions import ValidationError from django.db import models from django.urls import reverse from django.utils.translation import gettext as _ @@ -84,6 +85,14 @@ def sync_data(self): self.file_path = os.path.basename(self.data_path) self.data_file.write_to_disk(self.full_path, overwrite=True) + def clean(self): + super().clean() + + # Ensure that the file root and path make a unique pair + if self._meta.model.objects.filter(file_root=self.file_root, file_path=self.file_path).exclude(pk=self.pk).exists(): + raise ValidationError( + f"A {self._meta.verbose_name.lower()} with this file path already exists ({self.file_root}/{self.file_path}).") + def delete(self, *args, **kwargs): # Delete file from disk try: diff --git a/netbox/extras/models/scripts.py b/netbox/extras/models/scripts.py index 443dabdb339..122f56f2065 100644 --- a/netbox/extras/models/scripts.py +++ b/netbox/extras/models/scripts.py @@ -2,7 +2,6 @@ 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 _ @@ -77,14 +76,6 @@ def _get_name(cls): 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)