File tree Expand file tree Collapse file tree 1 file changed +26
-1
lines changed Expand file tree Collapse file tree 1 file changed +26
-1
lines changed Original file line number Diff line number Diff line change 1
1
from django .db .models import QuerySet
2
-
2
+ from netaddr import IPNetwork
3
+ from .constants import PREFIX_STATUS_CONTAINER
3
4
4
5
class PrefixQuerySet (QuerySet ):
5
6
7
+ def build_hierachy (self , limit = None ):
8
+ """
9
+ Iterate through a Queryset of Prefixes and build the parent-child relationships.
10
+ """
11
+ queryset = self
12
+ for p in queryset :
13
+ p .parent = None
14
+ p .depth = 0
15
+
16
+ for p in queryset :
17
+ p .children = []
18
+ for c in queryset :
19
+ if (p .prefix != c .prefix and p .family == c .family and ((p .vrf is None and
20
+ p .status == PREFIX_STATUS_CONTAINER ) or p .vrf == c .vrf ) and c .prefix .cidr in p .prefix .cidr ):
21
+ parent = c .parent
22
+ if parent is None or parent .prefix in p .prefix :
23
+ c .parent = p
24
+ c .depth = p .depth + 1
25
+ p .children .append (c )
26
+
27
+ return list (filter (lambda p : p .parent == None , self ))
28
+
29
+
30
+
6
31
def annotate_depth (self , limit = None ):
7
32
"""
8
33
Iterate through a QuerySet of Prefixes and annotate the hierarchical level of each. While it would be preferable
You can’t perform that action at this time.
0 commit comments