Closes #6414: Enable assigning prefixes to various object types (#17692)

* Replace site FK on Prefix with scope GFK

* Add denormalized relations

* Update prefix filters

* Add generic relations for Prefix

* Update GraphQL type for Prefix model

* Fix tests; misc cleanup

* Remove prefix_count from SiteSerializer

* Remove site field from PrefixBulkEditForm

* Restore scope filters for prefixes

* Fix scope population on PrefixForm init

* Show scope type

* Assign scope during bulk import of prefixes

* Correct handling of GenericForeignKey in PrefixForm

* Add prefix counts to all scoped objects

* Fix migration; linter fix

* Add limit_choices_to on scope_type

* Clean up cache_related_objects()

* Enable bulk editing prefix scope
This commit is contained in:
Jeremy Stretch
2024-10-18 15:45:22 -04:00
committed by GitHub
parent c78da79ce6
commit 75270c1aef
21 changed files with 457 additions and 153 deletions
+8 -4
View File
@@ -21,12 +21,13 @@ __all__ = (
class RegionSerializer(NestedGroupModelSerializer):
parent = NestedRegionSerializer(required=False, allow_null=True, default=None)
site_count = serializers.IntegerField(read_only=True, default=0)
prefix_count = RelatedObjectCountField('_prefixes')
class Meta:
model = Region
fields = [
'id', 'url', 'display_url', 'display', 'name', 'slug', 'parent', 'description', 'tags', 'custom_fields',
'created', 'last_updated', 'site_count', '_depth',
'created', 'last_updated', 'site_count', 'prefix_count', '_depth',
]
brief_fields = ('id', 'url', 'display', 'name', 'slug', 'description', 'site_count', '_depth')
@@ -34,12 +35,13 @@ class RegionSerializer(NestedGroupModelSerializer):
class SiteGroupSerializer(NestedGroupModelSerializer):
parent = NestedSiteGroupSerializer(required=False, allow_null=True, default=None)
site_count = serializers.IntegerField(read_only=True, default=0)
prefix_count = RelatedObjectCountField('_prefixes')
class Meta:
model = SiteGroup
fields = [
'id', 'url', 'display_url', 'display', 'name', 'slug', 'parent', 'description', 'tags', 'custom_fields',
'created', 'last_updated', 'site_count', '_depth',
'created', 'last_updated', 'site_count', 'prefix_count', '_depth',
]
brief_fields = ('id', 'url', 'display', 'name', 'slug', 'description', 'site_count', '_depth')
@@ -61,7 +63,7 @@ class SiteSerializer(NetBoxModelSerializer):
# Related object counts
circuit_count = RelatedObjectCountField('circuit_terminations')
device_count = RelatedObjectCountField('devices')
prefix_count = RelatedObjectCountField('prefixes')
prefix_count = RelatedObjectCountField('_prefixes')
rack_count = RelatedObjectCountField('racks')
vlan_count = RelatedObjectCountField('vlans')
virtualmachine_count = RelatedObjectCountField('virtual_machines')
@@ -84,11 +86,13 @@ class LocationSerializer(NestedGroupModelSerializer):
tenant = TenantSerializer(nested=True, required=False, allow_null=True)
rack_count = serializers.IntegerField(read_only=True, default=0)
device_count = serializers.IntegerField(read_only=True, default=0)
prefix_count = RelatedObjectCountField('_prefixes')
class Meta:
model = Location
fields = [
'id', 'url', 'display_url', 'display', 'name', 'slug', 'site', 'parent', 'status', 'tenant', 'facility',
'description', 'tags', 'custom_fields', 'created', 'last_updated', 'rack_count', 'device_count', '_depth',
'description', 'tags', 'custom_fields', 'created', 'last_updated', 'rack_count', 'device_count',
'prefix_count', '_depth',
]
brief_fields = ('id', 'url', 'display', 'name', 'slug', 'description', 'rack_count', '_depth')
+24
View File
@@ -28,6 +28,12 @@ class Region(ContactsMixin, NestedGroupModel):
states, and/or cities. Regions are recursively nested into a hierarchy: all sites belonging to a child region are
also considered to be members of its parent and ancestor region(s).
"""
prefixes = GenericRelation(
to='ipam.Prefix',
content_type_field='scope_type',
object_id_field='scope_id',
related_query_name='region'
)
vlan_groups = GenericRelation(
to='ipam.VLANGroup',
content_type_field='scope_type',
@@ -78,6 +84,12 @@ class SiteGroup(ContactsMixin, NestedGroupModel):
within corporate sites you might distinguish between offices and data centers. Like regions, site groups can be
nested recursively to form a hierarchy.
"""
prefixes = GenericRelation(
to='ipam.Prefix',
content_type_field='scope_type',
object_id_field='scope_id',
related_query_name='site_group'
)
vlan_groups = GenericRelation(
to='ipam.VLANGroup',
content_type_field='scope_type',
@@ -214,6 +226,12 @@ class Site(ContactsMixin, ImageAttachmentsMixin, PrimaryModel):
)
# Generic relations
prefixes = GenericRelation(
to='ipam.Prefix',
content_type_field='scope_type',
object_id_field='scope_id',
related_query_name='site'
)
vlan_groups = GenericRelation(
to='ipam.VLANGroup',
content_type_field='scope_type',
@@ -273,6 +291,12 @@ class Location(ContactsMixin, ImageAttachmentsMixin, NestedGroupModel):
)
# Generic relations
prefixes = GenericRelation(
to='ipam.Prefix',
content_type_field='scope_type',
object_id_field='scope_id',
related_query_name='location'
)
vlan_groups = GenericRelation(
to='ipam.VLANGroup',
content_type_field='scope_type',