Skip to content

Commit

Permalink
raises validation error if file path and root are not unique #14187
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi1693 committed Nov 9, 2023
1 parent e5c38e0 commit 8e9e812
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions netbox/extras/models/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
Expand Down Expand Up @@ -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)

0 comments on commit 8e9e812

Please sign in to comment.