mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Initial work on #6087
This commit is contained in:
@@ -1,27 +1,32 @@
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db.models import Q
|
||||
from django.db.models.expressions import RawSQL
|
||||
|
||||
from utilities.querysets import RestrictedQuerySet
|
||||
|
||||
|
||||
class PrefixQuerySet(RestrictedQuerySet):
|
||||
|
||||
def annotate_tree(self):
|
||||
def annotate_hierarchy(self):
|
||||
"""
|
||||
Annotate the number of parent and child prefixes for each Prefix. Raw SQL is needed for these subqueries
|
||||
because we need to cast NULL VRF values to integers for comparison. (NULL != NULL).
|
||||
Annotate the depth and number of child prefixes for each Prefix. Cast null VRF values to zero for
|
||||
comparison. (NULL != NULL).
|
||||
"""
|
||||
return self.extra(
|
||||
select={
|
||||
'parents': 'SELECT COUNT(U0."prefix") AS "c" '
|
||||
'FROM "ipam_prefix" U0 '
|
||||
'WHERE (U0."prefix" >> "ipam_prefix"."prefix" '
|
||||
'AND COALESCE(U0."vrf_id", 0) = COALESCE("ipam_prefix"."vrf_id", 0))',
|
||||
'children': 'SELECT COUNT(U1."prefix") AS "c" '
|
||||
'FROM "ipam_prefix" U1 '
|
||||
'WHERE (U1."prefix" << "ipam_prefix"."prefix" '
|
||||
'AND COALESCE(U1."vrf_id", 0) = COALESCE("ipam_prefix"."vrf_id", 0))',
|
||||
}
|
||||
return self.annotate(
|
||||
hierarchy_depth=RawSQL(
|
||||
'SELECT COUNT(DISTINCT U0."prefix") AS "c" '
|
||||
'FROM "ipam_prefix" U0 '
|
||||
'WHERE (U0."prefix" >> "ipam_prefix"."prefix" '
|
||||
'AND COALESCE(U0."vrf_id", 0) = COALESCE("ipam_prefix"."vrf_id", 0))',
|
||||
()
|
||||
),
|
||||
hierarchy_children=RawSQL(
|
||||
'SELECT COUNT(U1."prefix") AS "c" '
|
||||
'FROM "ipam_prefix" U1 '
|
||||
'WHERE (U1."prefix" << "ipam_prefix"."prefix" '
|
||||
'AND COALESCE(U1."vrf_id", 0) = COALESCE("ipam_prefix"."vrf_id", 0))',
|
||||
()
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user