1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Remove custom validate_unique() methods

This commit is contained in:
jeremystretch
2022-09-14 09:16:25 -04:00
parent 6016e1b15d
commit ec6457bcd3
6 changed files with 153 additions and 89 deletions

View File

@@ -651,10 +651,25 @@ class Device(NetBoxModel, ConfigContextModel):
class Meta:
ordering = ('_name', 'pk') # Name may be null
unique_together = (
('site', 'tenant', 'name'), # See validate_unique below
('rack', 'position', 'face'),
('virtual_chassis', 'vc_position'),
constraints = (
models.UniqueConstraint(
name='dcim_device_unique_name_site_tenant',
fields=('name', 'site', 'tenant')
),
models.UniqueConstraint(
name='dcim_device_unique_name_site',
fields=('name', 'site'),
condition=Q(tenant__isnull=True),
violation_error_message="Device name must be unique per site."
),
models.UniqueConstraint(
name='dcim_device_unique_rack_position_face',
fields=('rack', 'position', 'face')
),
models.UniqueConstraint(
name='dcim_device_unique_virtual_chassis_vc_position',
fields=('virtual_chassis', 'vc_position')
),
)
def __str__(self):
@@ -679,23 +694,6 @@ class Device(NetBoxModel, ConfigContextModel):
def get_absolute_url(self):
return reverse('dcim:device', args=[self.pk])
def validate_unique(self, exclude=None):
# Check for a duplicate name on a device assigned to the same Site and no Tenant. This is necessary
# because Django does not consider two NULL fields to be equal, and thus will not trigger a violation
# of the uniqueness constraint without manual intervention.
if self.name and hasattr(self, 'site') and self.tenant is None:
if Device.objects.exclude(pk=self.pk).filter(
name=self.name,
site=self.site,
tenant__isnull=True
):
raise ValidationError({
'name': 'A device with this name already exists.'
})
super().validate_unique(exclude)
def clean(self):
super().clean()

View File

@@ -67,7 +67,8 @@ class Region(NestedGroupModel):
models.UniqueConstraint(
fields=('name',),
name='dcim_region_name',
condition=Q(parent=None)
condition=Q(parent__isnull=True),
violation_error_message="A top-level region with this name already exists."
),
models.UniqueConstraint(
fields=('parent', 'slug'),
@@ -76,24 +77,11 @@ class Region(NestedGroupModel):
models.UniqueConstraint(
fields=('slug',),
name='dcim_region_slug',
condition=Q(parent=None)
condition=Q(parent__isnull=True),
violation_error_message="A top-level region with this slug already exists."
),
)
def validate_unique(self, exclude=None):
if self.parent is None:
regions = Region.objects.exclude(pk=self.pk)
if regions.filter(name=self.name, parent__isnull=True).exists():
raise ValidationError({
'name': 'A region with this name already exists.'
})
if regions.filter(slug=self.slug, parent__isnull=True).exists():
raise ValidationError({
'name': 'A region with this slug already exists.'
})
super().validate_unique(exclude=exclude)
def get_absolute_url(self):
return reverse('dcim:region', args=[self.pk])
@@ -153,7 +141,8 @@ class SiteGroup(NestedGroupModel):
models.UniqueConstraint(
fields=('name',),
name='dcim_sitegroup_name',
condition=Q(parent=None)
condition=Q(parent__isnull=True),
violation_error_message="A top-level site group with this name already exists."
),
models.UniqueConstraint(
fields=('parent', 'slug'),
@@ -162,24 +151,11 @@ class SiteGroup(NestedGroupModel):
models.UniqueConstraint(
fields=('slug',),
name='dcim_sitegroup_slug',
condition=Q(parent=None)
condition=Q(parent__isnull=True),
violation_error_message="A top-level site group with this slug already exists."
),
)
def validate_unique(self, exclude=None):
if self.parent is None:
site_groups = SiteGroup.objects.exclude(pk=self.pk)
if site_groups.filter(name=self.name, parent__isnull=True).exists():
raise ValidationError({
'name': 'A site group with this name already exists.'
})
if site_groups.filter(slug=self.slug, parent__isnull=True).exists():
raise ValidationError({
'name': 'A site group with this slug already exists.'
})
super().validate_unique(exclude=exclude)
def get_absolute_url(self):
return reverse('dcim:sitegroup', args=[self.pk])
@@ -384,7 +360,8 @@ class Location(NestedGroupModel):
models.UniqueConstraint(
fields=('site', 'name'),
name='dcim_location_name',
condition=Q(parent=None)
condition=Q(parent__isnull=True),
violation_error_message="A location with this name already exists within the specified site."
),
models.UniqueConstraint(
fields=('site', 'parent', 'slug'),
@@ -393,24 +370,11 @@ class Location(NestedGroupModel):
models.UniqueConstraint(
fields=('site', 'slug'),
name='dcim_location_slug',
condition=Q(parent=None)
condition=Q(parent__isnull=True),
violation_error_message="A location with this slug already exists within the specified site."
),
)
def validate_unique(self, exclude=None):
if self.parent is None:
locations = Location.objects.exclude(pk=self.pk)
if locations.filter(name=self.name, site=self.site, parent__isnull=True).exists():
raise ValidationError({
"name": f"A location with this name in site {self.site} already exists."
})
if locations.filter(slug=self.slug, site=self.site, parent__isnull=True).exists():
raise ValidationError({
"name": f"A location with this slug in site {self.site} already exists."
})
super().validate_unique(exclude=exclude)
@classmethod
def get_prerequisite_models(cls):
return [Site, ]