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

Change VLANGroup site to scope (GFK)

This commit is contained in:
Jeremy Stretch
2021-03-09 14:13:50 -05:00
parent c6641ec1de
commit 4dae781be0
7 changed files with 169 additions and 59 deletions

View File

@@ -1,3 +1,5 @@
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
@@ -31,13 +33,24 @@ class VLANGroup(OrganizationalModel):
slug = models.SlugField(
max_length=100
)
site = models.ForeignKey(
to='dcim.Site',
on_delete=models.PROTECT,
related_name='vlan_groups',
scope_type = models.ForeignKey(
to=ContentType,
on_delete=models.CASCADE,
limit_choices_to=Q(
app_label='dcim',
model__in=['region', 'sitegroup', 'site', 'location', 'rack']
),
blank=True,
null=True
)
scope_id = models.PositiveBigIntegerField(
blank=True,
null=True
)
scope = GenericForeignKey(
ct_field='scope_type',
fk_field='scope_id'
)
description = models.CharField(
max_length=200,
blank=True
@@ -45,13 +58,13 @@ class VLANGroup(OrganizationalModel):
objects = RestrictedQuerySet.as_manager()
csv_headers = ['name', 'slug', 'site', 'description']
csv_headers = ['name', 'slug', 'scope_type', 'scope_id', 'description']
class Meta:
ordering = ('site', 'name', 'pk') # (site, name) may be non-unique
ordering = ('name', 'pk') # Name may be non-unique
unique_together = [
['site', 'name'],
['site', 'slug'],
['scope_type', 'scope_id', 'name'],
['scope_type', 'scope_id', 'slug'],
]
verbose_name = 'VLAN group'
verbose_name_plural = 'VLAN groups'
@@ -66,7 +79,8 @@ class VLANGroup(OrganizationalModel):
return (
self.name,
self.slug,
self.site.name if self.site else None,
f'{self.scope_type.app_label}.{self.scope_type.model}',
self.scope_id,
self.description,
)