Skip to content

Commit

Permalink
Merge pull request #2549 from johannaengland/related-names-8
Browse files Browse the repository at this point in the history
Add related names to django models (manage - Router/topology)
  • Loading branch information
johannaengland authored Mar 3, 2023
2 parents 921bf5d + 97a7e34 commit ecc47c0
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 50 deletions.
2 changes: 1 addition & 1 deletion python/nav/ipdevpoll/neighbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def _netbox_from_ip(self, ip):
if ip in self._invalid_neighbor_ips:
return
return self._netbox_query(Q(ip=ip)) or self._netbox_query(
Q(interfaces__gwportprefix__gw_ip=ip)
Q(interfaces__gwport_prefixes__gw_ip=ip)
)

ID_PATTERN = re.compile(r'(.*\()?(?P<sysname>[^\)]+)\)?')
Expand Down
2 changes: 1 addition & 1 deletion python/nav/ipdevpoll/plugins/lldp.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,4 @@ def _interfaces_from_ip(self, ip):
assert ip
if ip in self._invalid_neighbor_ips:
return
return self._interface_query(Q(gwportprefix__gw_ip=ip))
return self._interface_query(Q(gwport_prefixes__gw_ip=ip))
2 changes: 1 addition & 1 deletion python/nav/ipdevpoll/shadows/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ def _get_router_count_for_prefix(net_address, include_netboxid=None):
"""
address_filter = Q(
interfaces__gwportprefix__prefix__net_address=str(net_address)
interfaces__gwport_prefixes__prefix__net_address=str(net_address)
)
if include_netboxid:
address_filter = address_filter | Q(id=include_netboxid)
Expand Down
68 changes: 53 additions & 15 deletions python/nav/models/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def get_gwport_count(self):
def get_gwports(self):
"""Returns all interfaces that have IP addresses."""
return Interface.objects.filter(
netbox=self, gwportprefix__isnull=False
netbox=self, gwport_prefixes__isnull=False
).distinct()

def get_gwports_sorted(self):
Expand Down Expand Up @@ -992,7 +992,7 @@ def get_absolute_url(self):
def get_gwports(self):
"""Returns all interfaces that have IP addresses."""
return Interface.objects.filter(
module=self, gwportprefix__isnull=False
module=self, gwport_prefixes__isnull=False
).distinct()

def get_gwports_sorted(self):
Expand Down Expand Up @@ -1380,9 +1380,17 @@ class GwPortPrefix(models.Model):
"""

interface = models.ForeignKey(
'Interface', on_delete=models.CASCADE, db_column='interfaceid'
'Interface',
on_delete=models.CASCADE,
db_column='interfaceid',
related_name="gwport_prefixes",
)
prefix = models.ForeignKey(
'Prefix',
on_delete=models.CASCADE,
db_column='prefixid',
related_name="gwport_prefixes",
)
prefix = models.ForeignKey('Prefix', on_delete=models.CASCADE, db_column='prefixid')
gw_ip = CIDRField(db_column='gwip', primary_key=True)
virtual = models.BooleanField(default=False)

Expand Down Expand Up @@ -1438,9 +1446,17 @@ class Prefix(models.Model):

id = models.AutoField(db_column='prefixid', primary_key=True)
net_address = CIDRField(db_column='netaddr', unique=True)
vlan = models.ForeignKey('Vlan', on_delete=models.CASCADE, db_column='vlanid')
vlan = models.ForeignKey(
'Vlan',
on_delete=models.CASCADE,
db_column='vlanid',
related_name="prefixes",
)
usages = models.ManyToManyField(
'Usage', through='PrefixUsage', through_fields=('prefix', 'usage')
'Usage',
through='PrefixUsage',
through_fields=('prefix', 'usage'),
related_name="prefixes",
)

class Meta(object):
Expand All @@ -1464,7 +1480,7 @@ def get_prefix_size(self):
def get_router_ports(self):
"""Returns a ordered list of GwPortPrefix objects on this prefix"""
return (
self.gwportprefix_set.filter(
self.gwport_prefixes.filter(
interface__netbox__category__id__in=('GSW', 'GW')
)
.select_related('interface', 'interface__netbox')
Expand Down Expand Up @@ -1495,22 +1511,36 @@ class Vlan(models.Model):
id = models.AutoField(db_column='vlanid', primary_key=True)
vlan = models.IntegerField(null=True, blank=True)
net_type = models.ForeignKey(
'NetType', on_delete=models.CASCADE, db_column='nettype'
'NetType',
on_delete=models.CASCADE,
db_column='nettype',
related_name="vlans",
)
organization = models.ForeignKey(
'Organization',
on_delete=models.CASCADE,
db_column='orgid',
null=True,
blank=True,
related_name="vlans",
)
usage = models.ForeignKey(
'Usage', on_delete=models.CASCADE, db_column='usageid', null=True, blank=True
'Usage',
on_delete=models.CASCADE,
db_column='usageid',
null=True,
blank=True,
related_name="vlans",
)
net_ident = VarcharField(db_column='netident', null=True, blank=True)
description = VarcharField(null=True, blank=True)
netbox = models.ForeignKey(
'NetBox', on_delete=models.SET_NULL, db_column='netboxid', null=True, blank=True
'NetBox',
on_delete=models.SET_NULL,
db_column='netboxid',
null=True,
blank=True,
related_name="vlans",
)

class Meta(object):
Expand Down Expand Up @@ -1540,7 +1570,7 @@ def get_graph_urls(self):
def get_graph_url(self, family=4):
"""Creates a graph url for the given family with all prefixes stacked"""
assert family in [4, 6]
prefixes = self.prefix_set.extra(where=["family(netaddr)=%s" % family])
prefixes = self.prefixes.extra(where=["family(netaddr)=%s" % family])
# Put metainformation in the alias so that Rickshaw can pick it up and
# know how to draw the series.
series = [
Expand Down Expand Up @@ -1622,10 +1652,18 @@ class Arp(models.Model):

id = models.AutoField(db_column='arpid', primary_key=True)
netbox = models.ForeignKey(
'Netbox', on_delete=models.CASCADE, db_column='netboxid', null=True
'Netbox',
on_delete=models.CASCADE,
db_column='netboxid',
null=True,
related_name="arp_set",
)
prefix = models.ForeignKey(
'Prefix', on_delete=models.CASCADE, db_column='prefixid', null=True
'Prefix',
on_delete=models.CASCADE,
db_column='prefixid',
null=True,
related_name="arp_set",
)
sysname = VarcharField()
ip = models.GenericIPAddressField()
Expand Down Expand Up @@ -2143,7 +2181,7 @@ def is_gwport(self):
other hosts.
"""
return self.gwportprefix_set.count() > 0
return self.gwport_prefixes.count() > 0

def is_physical_port(self):
"""Returns true if this interface has a physical connector present"""
Expand Down Expand Up @@ -2312,7 +2350,7 @@ def get_peer_as_netbox(self):
:rtype: Netbox
"""
expr = Q(ip=self.peer) | Q(interfaces__gwportprefix__gw_ip=self.peer)
expr = Q(ip=self.peer) | Q(interfaces__gwport_prefixes__gw_ip=self.peer)
netboxes = Netbox.objects.filter(expr)
if netboxes:
return netboxes[0]
Expand Down
6 changes: 3 additions & 3 deletions python/nav/models/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ class MatchField(models.Model):
# formating is not PEP8, but it wouldn't be very readable otherwise)
# Since we need to know how things are connected this has been done manualy
FOREIGN_MAP = {
ARP: 'netbox__arp',
ARP: 'netbox__arp_set',
CAM: 'netbox__cam_set',
CATEGORY: 'netbox__category',
NETBOXGROUP: 'netbox__netboxcategory__category',
Expand All @@ -1203,9 +1203,9 @@ class MatchField(models.Model):
SERVICE: 'netbox__service',
INTERFACE: 'netbox__connected_to_interface',
TYPE: 'netbox__type',
USAGE: 'netbox__organization__vlan__usage',
USAGE: 'netbox__organization__vlans__usage',
VENDOR: 'netbox__type__vendor',
VLAN: 'netbox__organization__vlan',
VLAN: 'netbox__organization__vlans',
ALERT: '', # Checks alert object itself
ALERTTYPE: 'alert_type',
}
Expand Down
4 changes: 2 additions & 2 deletions python/nav/topology/detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def do_vlan_detection(vlans):
@with_exception_logging
def delete_unused_vlans():
"""Deletes vlans unassociated with prefixes or switch ports"""
unused = Vlan.objects.filter(prefix__isnull=True, swport_vlans__isnull=True)
unused = Vlan.objects.filter(prefixes__isnull=True, swport_vlans__isnull=True)
if unused:
_logger.info("deleting unused vlans: %r", unused)
unused.delete()
Expand All @@ -156,7 +156,7 @@ def delete_unused_prefixes():
manually entered into NAV.
"""
holy_vlans = Q(vlan__net_type__in=('scope', 'reserved', 'static'))
unused_prefixes = Prefix.objects.filter(gwportprefix__isnull=True).exclude(
unused_prefixes = Prefix.objects.filter(gwport_prefixes__isnull=True).exclude(
holy_vlans
)

Expand Down
2 changes: 1 addition & 1 deletion python/nav/topology/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _build_unrouted_vlan_seed_dict(self):
return {
x
for x in Vlan.objects.filter(
prefix__isnull=True, netbox__isnull=False
prefixes__isnull=True, netbox__isnull=False
).iterator()
}

Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/api/v1/filter_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def filter_queryset(self, request, queryset, view):
"""
filters = {
'swport': Q(baseport__isnull=False),
'gwport': Q(gwportprefix__isnull=False),
'gwport': Q(gwport_prefixes__isnull=False),
'physicalport': Q(ifconnectorpresent=True),
'trunk': Q(trunk=True),
}
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ class RoutedPrefixList(NAVAPIMixin, ListAPIView):

def get_queryset(self):
prefixes = manage.Prefix.objects.filter(
gwportprefix__interface__netbox__category__in=self._router_categories
gwport_prefixes__interface__netbox__category__in=self._router_categories
)
if self.request.GET.get('family'):
prefixes = prefixes.extra(
Expand Down
8 changes: 4 additions & 4 deletions python/nav/web/info/vlan/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def process_searchform(form):
def vlan_details(request, vlanid):
"""Render details for a vlan"""
vlan = get_object_or_404(Vlan, pk=vlanid)
prefixes = sorted(vlan.prefix_set.all(), key=methodcaller('get_prefix_size'))
prefixes = sorted(vlan.prefixes.all(), key=methodcaller('get_prefix_size'))

has_v6 = False
has_v4 = False
Expand Down Expand Up @@ -184,7 +184,7 @@ def get_vlan_graph_url(vlanid, family=4, timeframe="day"):

extra = {'where': ['family(netaddr) = %s' % family]}
prefixes = sorted(
vlan.prefix_set.all().extra(**extra),
vlan.prefixes.all().extra(**extra),
key=methodcaller('get_prefix_size'),
reverse=True,
)
Expand Down Expand Up @@ -235,9 +235,9 @@ def _vlan_metrics_from_prefixes(prefixes, ip_version):
def find_gwportprefixes(vlan):
"""Find routers that defines this vlan"""
gwportprefixes = []
for prefix in vlan.prefix_set.all():
for prefix in vlan.prefixes.all():
gwportprefixes.extend(
prefix.gwportprefix_set.filter(
prefix.gwport_prefixes.filter(
interface__netbox__category__id__in=['GSW', 'GW']
)
)
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/ipam/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def matrix(request):
def generate_context():
scopes = Prefix.objects.filter(vlan__net_type='scope')
ctx = {
"net_types": NetType.objects.exclude(vlan__net_type__in=["scope", "reserved"]),
"net_types": NetType.objects.exclude(vlans__net_type__in=["scope", "reserved"]),
"organizations": Organization.objects.all,
"usages": Usage.objects.all,
"has_registered_scopes": scopes.count() > 0,
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/l2trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def get_vlan_gateway(vlan):
'prefix__net_address'
)
gateways = Netbox.objects.filter(
category__in=('GW', 'GSW'), interfaces__gwportprefix__in=gwport_prefixes
category__in=('GW', 'GSW'), interfaces__gwport_prefixes__in=gwport_prefixes
).distinct()
if gateways:
return gateways[0]
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/networkexplorer/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class ExpandGWPortMixin(object):

def get_context_data(self, **kwargs):
gwport = kwargs.pop('object')
prefixes = gwport.gwportprefix_set.select_related(
prefixes = gwport.gwport_prefixes.select_related(
'prefix__vlan',
).exclude(prefix__vlan__net_type='static')

Expand Down
16 changes: 8 additions & 8 deletions python/nav/web/networkexplorer/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def search_expand_swport(swportid=None, swport=None, scanned=[]):
for swportvlan in swport.swport_vlans.exclude(
vlan__net_type='static'
).select_related():
for prefix in swportvlan.vlan.prefix_set.all():
for gwportprefix in prefix.gwportprefix_set.all():
for prefix in swportvlan.vlan.prefixes.all():
for gwportprefix in prefix.gwport_prefixes.all():
found_gwports.append(gwportprefix.interface)

for port in (
Expand All @@ -108,17 +108,17 @@ def search_expand_swport(swportid=None, swport=None, scanned=[]):
found_swports.extend(recurs_found[1])

for port in Interface.objects.filter(
to_interface__in=found_swports, gwportprefix__isnull=False
to_interface__in=found_swports, gwport_prefixes__isnull=False
):
found_gwports.append(port)

for port in Interface.objects.filter(
to_netbox=swport.netbox, gwportprefix__isnull=False
to_netbox=swport.netbox, gwport_prefixes__isnull=False
):
found_gwports.append(port)

for port in Interface.objects.filter(
to_interface=swport, gwportprefix__isnull=False
to_interface=swport, gwport_prefixes__isnull=False
):
found_gwports.append(port)

Expand All @@ -142,14 +142,14 @@ def search_expand_netbox(netboxid=None, netbox=None):
for result in netbox.get_uplinks():
if (
result['other'].__class__ == Interface
and result['other'].gwportprefix_set.count()
and result['other'].gwport_prefixes.count()
):
found_gwports.append(result['other'])
else:
found_swports.append(result['other'])

ports = Interface.objects.filter(to_netbox=netbox)
gwports = ports.filter(gwportprefix__isnull=False)
gwports = ports.filter(gwport_prefixes__isnull=False)
swports = ports.filter(baseport__isnull=False)

found_gwports.extend(gwports)
Expand Down Expand Up @@ -230,7 +230,7 @@ def sysname_search(sysname, exact=False):

interfaces = Interface.objects.filter(module__netbox__in=netboxes)

for gwport in interfaces.filter(gwportprefix__isnull=False):
for gwport in interfaces.filter(gwport_prefixes__isnull=False):
gwport_matches.add(gwport)

for swport in interfaces.filter(baseport__isnull=False):
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/portadmin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def find_vlans_in_org(org):
:returns: list of FantasyVlans
:rtype: list
"""
vlans = list(org.vlan_set.all())
vlans = list(org.vlans.all())
for child_org in org.child_organizations.all():
vlans.extend(find_vlans_in_org(child_org))
return [FantasyVlan(x.vlan, x.net_ident) for x in list(set(vlans)) if x.vlan]
Expand Down
4 changes: 2 additions & 2 deletions python/nav/web/seeddb/page/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def vlan_list(request):
query = (
Vlan.objects.extra(
select={
'prefixes': (
'prefixes_list': (
"array_to_string("
"ARRAY(SELECT netaddr FROM prefix "
"WHERE vlanid=vlan.vlanid), ', ')"
Expand All @@ -101,7 +101,7 @@ def vlan_list(request):
'usage',
'net_ident',
'description',
'prefixes',
'prefixes_list',
)
return render_list(
request,
Expand Down
Loading

0 comments on commit ecc47c0

Please sign in to comment.