Skip to content

Commit 290aae5

Browse files
authored
Raises validation error if file path and root are not unique (#14232)
* raises validation error if file path and root are not unique #14187 * review changes #14187
1 parent ff021a8 commit 290aae5

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

netbox/core/models/files.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33

44
from django.conf import settings
5+
from django.core.exceptions import ValidationError
56
from django.db import models
67
from django.urls import reverse
78
from django.utils.translation import gettext as _
@@ -84,6 +85,14 @@ def sync_data(self):
8485
self.file_path = os.path.basename(self.data_path)
8586
self.data_file.write_to_disk(self.full_path, overwrite=True)
8687

88+
def clean(self):
89+
super().clean()
90+
91+
# Ensure that the file root and path make a unique pair
92+
if self._meta.model.objects.filter(file_root=self.file_root, file_path=self.file_path).exclude(pk=self.pk).exists():
93+
raise ValidationError(
94+
f"A {self._meta.verbose_name.lower()} with this file path already exists ({self.file_root}/{self.file_path}).")
95+
8796
def delete(self, *args, **kwargs):
8897
# Delete file from disk
8998
try:

0 commit comments

Comments
 (0)