Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add related names to django models (manage - Switch/topology) #2550

Merged
merged 10 commits into from Mar 2, 2023
2 changes: 1 addition & 1 deletion python/nav/eventengine/plugins/linkstate.py
Expand Up @@ -140,7 +140,7 @@ def _get_target_vlans(self):

"""
ifc = self.get_target()
vlans = ifc.swportvlan_set.values('vlan__vlan')
vlans = ifc.swport_vlans.values('vlan__vlan')
vlans = {row['vlan__vlan'] for row in vlans}
return vlans

Expand Down
62 changes: 47 additions & 15 deletions python/nav/models/manage.py
Expand Up @@ -465,7 +465,7 @@ def get_uplinks(self):
result = []

for iface in self.connected_to_interface.all():
if iface.swportvlan_set.filter(direction=SwPortVlan.DIRECTION_DOWN).count():
if iface.swport_vlans.filter(direction=SwPortVlan.DIRECTION_DOWN).count():
result.append(
{
'other': iface,
Expand Down Expand Up @@ -1603,9 +1603,17 @@ class SwPortVlan(models.Model):

id = models.AutoField(db_column='swportvlanid', primary_key=True)
interface = models.ForeignKey(
'Interface', on_delete=models.CASCADE, db_column='interfaceid'
'Interface',
on_delete=models.CASCADE,
db_column='interfaceid',
related_name="swport_vlans",
)
vlan = models.ForeignKey(
'Vlan',
on_delete=models.CASCADE,
db_column='vlanid',
related_name="swport_vlans",
)
vlan = models.ForeignKey('Vlan', on_delete=models.CASCADE, db_column='vlanid')
direction = models.CharField(
max_length=1, choices=DIRECTION_CHOICES, default=DIRECTION_UNDEFINED
)
Expand All @@ -1625,7 +1633,11 @@ class SwPortAllowedVlan(models.Model):
"""

interface = models.OneToOneField(
'Interface', on_delete=models.CASCADE, db_column='interfaceid', primary_key=True
'Interface',
on_delete=models.CASCADE,
db_column='interfaceid',
primary_key=True,
related_name="swport_allowed_vlan",
)
hex_string = VarcharField(db_column='hexstring')
_cached_hex_string = ''
Expand Down Expand Up @@ -1680,7 +1692,10 @@ class SwPortBlocked(models.Model):

id = models.AutoField(db_column='swportblockedid', primary_key=True)
interface = models.ForeignKey(
'Interface', on_delete=models.CASCADE, db_column='interfaceid'
'Interface',
on_delete=models.CASCADE,
db_column='interfaceid',
related_name="blocked_swports",
)
vlan = models.IntegerField()

Expand All @@ -1702,22 +1717,30 @@ class AdjacencyCandidate(models.Model):
"""

id = models.AutoField(db_column='adjacency_candidateid', primary_key=True)
netbox = models.ForeignKey('Netbox', on_delete=models.CASCADE, db_column='netboxid')
netbox = models.ForeignKey(
'Netbox',
on_delete=models.CASCADE,
db_column='netboxid',
related_name="from_adjancency_candidates",
)
interface = models.ForeignKey(
'Interface', on_delete=models.CASCADE, db_column='interfaceid'
'Interface',
on_delete=models.CASCADE,
db_column='interfaceid',
related_name="from_adjancency_candidates",
)
to_netbox = models.ForeignKey(
'Netbox',
on_delete=models.CASCADE,
db_column='to_netboxid',
related_name='to_adjacencycandidate_set',
related_name='to_adjacency_candidates',
)
to_interface = models.ForeignKey(
'Interface',
on_delete=models.CASCADE,
db_column='to_interfaceid',
null=True,
related_name='to_adjacencycandidate_set',
related_name='to_adjacency_candidates',
)
source = VarcharField()
miss_count = models.IntegerField(db_column='misscnt', default=0)
Expand Down Expand Up @@ -1746,7 +1769,12 @@ class NetboxVtpVlan(models.Model):
information."""

id = models.AutoField(primary_key=True) # Serial for faking a primary key
netbox = models.ForeignKey('Netbox', on_delete=models.CASCADE, db_column='netboxid')
netbox = models.ForeignKey(
'Netbox',
on_delete=models.CASCADE,
db_column='netboxid',
related_name="netbox_vtp_vlans",
)
vtp_vlan = models.IntegerField(db_column='vtpvlan')

class Meta(object):
Expand All @@ -1763,7 +1791,11 @@ class Cam(models.Model):

id = models.AutoField(db_column='camid', 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="cam_set",
)
sysname = VarcharField()
ifindex = models.IntegerField()
Expand Down Expand Up @@ -1915,7 +1947,7 @@ def get_vlan_numbers(self):
# XXX: This causes a DB query per port
vlans = [
swpv.vlan.vlan
for swpv in self.swportvlan_set.select_related('vlan', 'interface')
for swpv in self.swport_vlans.select_related('vlan', 'interface')
]
if self.vlan is not None and self.vlan not in vlans:
vlans.append(self.vlan)
Expand All @@ -1928,7 +1960,7 @@ def get_allowed_vlan_ranges(self):
:rtype: nav.util.NumberRange
"""
try:
allowed = self.swportallowedvlan.get_allowed_vlans()
allowed = self.swport_allowed_vlan.get_allowed_vlans()
except SwPortAllowedVlan.DoesNotExist:
pass
else:
Expand Down Expand Up @@ -2024,7 +2056,7 @@ def as_range(iterable):
return ",".join(
as_range(y)
for x, y in groupby(
sorted(self.swportallowedvlan.get_allowed_vlans()),
sorted(self.swport_allowed_vlan.get_allowed_vlans()),
lambda n, c=count(): n - next(c),
)
)
Expand Down Expand Up @@ -2092,7 +2124,7 @@ def is_degraded(self):

def get_sorted_vlans(self):
"""Returns a queryset of sorted swportvlans"""
return self.swportvlan_set.select_related('vlan').order_by('vlan__vlan')
return self.swport_vlans.select_related('vlan').order_by('vlan__vlan')

def is_on_maintenace(self):
"""Returns True if the owning Netbox is on maintenance"""
Expand Down
2 changes: 1 addition & 1 deletion python/nav/models/profiles.py
Expand Up @@ -1130,7 +1130,7 @@ class MatchField(models.Model):
# Since we need to know how things are connected this has been done manualy
FOREIGN_MAP = {
ARP: 'netbox__arp',
CAM: 'netbox__cam',
CAM: 'netbox__cam_set',
hmpf marked this conversation as resolved.
Show resolved Hide resolved
CATEGORY: 'netbox__category',
NETBOXGROUP: 'netbox__netboxcategory__category',
DEVICE: 'netbox__device',
Expand Down
4 changes: 2 additions & 2 deletions python/nav/portadmin/napalm/juniper.py
Expand Up @@ -205,7 +205,7 @@ def _get_untagged_vlans(self):

def get_netbox_vlans(self) -> List[FantasyVlan]:
vlan_objects = manage.Vlan.objects.filter(
swportvlan__interface__netbox=self.netbox
swport_vlans__interface__netbox=self.netbox
).distinct()

def _make_vlan(vlan):
Expand Down Expand Up @@ -289,7 +289,7 @@ def _save_access_interface(interface: manage.Interface, access_vlan: int):
interface.trunk = False
interface.vlan = access_vlan
try:
allowedvlans = interface.swportallowedvlan
allowedvlans = interface.swport_allowed_vlan
allowedvlans.save()
except manage.SwPortAllowedVlan.DoesNotExist:
pass
Expand Down
4 changes: 2 additions & 2 deletions python/nav/portadmin/snmp/base.py
Expand Up @@ -399,7 +399,7 @@ def _get_all_interfaces_oper_status(self):
def get_netbox_vlans(self):
numerical_vlans = self.get_netbox_vlan_tags()
vlan_objects = Vlan.objects.filter(
swportvlan__interface__netbox=self.netbox
swport_vlans__interface__netbox=self.netbox
).distinct()
vlans = []
for numerical_vlan in numerical_vlans:
Expand Down Expand Up @@ -548,7 +548,7 @@ def _save_trunk_interface(self, interface, native_vlan, trunk_vlans):
@staticmethod
def _set_interface_hex(interface, trunk_vlans):
try:
allowedvlan = interface.swportallowedvlan
allowedvlan = interface.swport_allowed_vlan
except SwPortAllowedVlan.DoesNotExist:
allowedvlan = SwPortAllowedVlan(interface=interface)

Expand Down
2 changes: 1 addition & 1 deletion python/nav/topology/detector.py
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, swportvlan__isnull=True)
unused = Vlan.objects.filter(prefix__isnull=True, swport_vlans__isnull=True)
if unused:
_logger.info("deleting unused vlans: %r", unused)
unused.delete()
Expand Down
4 changes: 2 additions & 2 deletions python/nav/topology/vlan.py
Expand Up @@ -288,8 +288,8 @@ def _ifc_has_vlan(self, ifc):
def _vlan_allowed_on_trunk(self, ifc):
return (
ifc.trunk
and getattr(ifc, 'swportallowedvlan', None)
and self.vlan.vlan in ifc.swportallowedvlan
and getattr(ifc, 'swport_allowed_vlan', None)
and self.vlan.vlan in ifc.swport_allowed_vlan
)

def _is_blocked_on_any_end(self, edge):
Expand Down
8 changes: 4 additions & 4 deletions python/nav/web/ipdevinfo/utils.py
Expand Up @@ -306,7 +306,7 @@ def find_children(netbox, netboxes=None):
netboxes = [netbox]

interfaces = netbox.interface_set.filter(
to_netbox__isnull=False, swportvlan__direction=SwPortVlan.DIRECTION_DOWN
to_netbox__isnull=False, swport_vlans__direction=SwPortVlan.DIRECTION_DOWN
)
for interface in interfaces:
if interface.to_netbox not in netboxes:
Expand Down Expand Up @@ -334,12 +334,12 @@ def find_vlan_organizations(netboxes):
for netbox in netboxes:
interfaces = netbox.interface_set.filter(
to_netbox__isnull=False,
swportvlan__direction=SwPortVlan.DIRECTION_DOWN,
swportvlan__vlan__organization__isnull=False,
swport_vlans__direction=SwPortVlan.DIRECTION_DOWN,
swport_vlans__vlan__organization__isnull=False,
)
for interface in interfaces:
vlans.extend(
[v.vlan for v in interface.swportvlan_set.exclude(vlan__in=vlans)]
[v.vlan for v in interface.swport_vlans.exclude(vlan__in=vlans)]
)

return [v.organization for v in set(vlans) if v.organization]
Expand Down
8 changes: 4 additions & 4 deletions python/nav/web/networkexplorer/mixins.py
Expand Up @@ -125,11 +125,11 @@ def _get_expandable(gwport):
}
gwport_prefixes.append(p)

vlans = prefix.prefix.vlan.swportvlan_set.exclude(
vlans = prefix.prefix.vlan.swport_vlans.exclude(
vlan__net_type='static'
).filter(interface__netbox=gwport.netbox)
for vlan in vlans:
if not vlan.interface.swportblocked_set.filter(
if not vlan.interface.blocked_swports.filter(
vlan=vlan.vlan.vlan
).count():
has_children = True
Expand Down Expand Up @@ -158,7 +158,7 @@ def get_context_data(self, **kwargs):
vlans_found = set()
for prefix in prefixes:
for vlan in (
prefix.prefix.vlan.swportvlan_set.select_related(
prefix.prefix.vlan.swport_vlans.select_related(
'interface__to_interface__netbox',
'interface__netbox',
'vlan',
Expand All @@ -168,7 +168,7 @@ def get_context_data(self, **kwargs):
):

# Check if port is spanningtreeblocked
if vlan.interface.swportblocked_set.filter(
if vlan.interface.blocked_swports.filter(
vlan=vlan.vlan.vlan # really!
).count():
continue
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/networkexplorer/search.py
Expand Up @@ -87,7 +87,7 @@ def search_expand_swport(swportid=None, swport=None, scanned=[]):
found_gwports = []

# Find gwport that has the same vlan
for swportvlan in swport.swportvlan_set.exclude(
for swportvlan in swport.swport_vlans.exclude(
vlan__net_type='static'
).select_related():
for prefix in swportvlan.vlan.prefix_set.all():
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/portadmin/views.py
Expand Up @@ -292,7 +292,7 @@ def set_voice_vlan_attribute(voice_vlan, interfaces):
for interface in interfaces:
if not interface.trunk:
continue
allowed_vlans = interface.swportallowedvlan.get_allowed_vlans()
allowed_vlans = interface.swport_allowed_vlan.get_allowed_vlans()
interface.voice_activated = (
len(allowed_vlans) == 1 and voice_vlan in allowed_vlans
)
Expand Down