Skip to content

Commit

Permalink
Raises validation error if file path and root are not unique (#14232)
Browse files Browse the repository at this point in the history
* raises validation error if file path and root are not unique #14187

* review changes #14187
  • Loading branch information
abhi1693 authored Nov 29, 2023
1 parent ff021a8 commit 290aae5
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions netbox/core/models/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 290aae5

Please sign in to comment.