Skip to content

Commit

Permalink
Fixes #18324: Correct filter names for certain related object listings
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Jan 7, 2025
1 parent ed54122 commit c77bfc6
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions netbox/dcim/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from circuits.models import Circuit, CircuitTermination
from extras.views import ObjectConfigContextView
from ipam.models import ASN, IPAddress, VLANGroup
from ipam.models import ASN, IPAddress, Prefix, VLANGroup
from ipam.tables import InterfaceVLANTable, VLANTranslationRuleTable
from netbox.constants import DEFAULT_ACTION_PERMISSIONS
from netbox.views import generic
Expand All @@ -30,8 +30,9 @@
)
from virtualization.filtersets import VirtualMachineFilterSet
from virtualization.forms import VirtualMachineFilterForm
from virtualization.models import VirtualMachine
from virtualization.models import Cluster, VirtualMachine
from virtualization.tables import VirtualMachineTable
from wireless.models import WirelessLAN
from . import filtersets, forms, tables
from .choices import DeviceFaceChoices, InterfaceModeChoices
from .models import *
Expand Down Expand Up @@ -238,6 +239,7 @@ def get_extra_context(self, request, instance):
'related_models': self.get_related_models(
request,
regions,
omit=(Cluster, Prefix, WirelessLAN),
extra=(
(Location.objects.restrict(request.user, 'view').filter(site__region__in=regions), 'region_id'),
(Rack.objects.restrict(request.user, 'view').filter(site__region__in=regions), 'region_id'),
Expand All @@ -247,6 +249,11 @@ def get_extra_context(self, request, instance):
).distinct(),
'region_id'
),

# Handle these relations manually to avoid erroneous filter name resolution
(Cluster.objects.restrict(request.user, 'view').filter(_region__in=regions), 'region_id'),
(Prefix.objects.restrict(request.user, 'view').filter(_region__in=regions), 'region_id'),
(WirelessLAN.objects.restrict(request.user, 'view').filter(_region__in=regions), 'region_id'),
),
),
}
Expand Down Expand Up @@ -331,6 +338,7 @@ def get_extra_context(self, request, instance):
'related_models': self.get_related_models(
request,
groups,
omit=(Cluster, Prefix, WirelessLAN),
extra=(
(Location.objects.restrict(request.user, 'view').filter(site__group__in=groups), 'site_group_id'),
(Rack.objects.restrict(request.user, 'view').filter(site__group__in=groups), 'site_group_id'),
Expand All @@ -340,6 +348,20 @@ def get_extra_context(self, request, instance):
).distinct(),
'site_group_id'
),

# Handle these relations manually to avoid erroneous filter name resolution
(
Cluster.objects.restrict(request.user, 'view').filter(_site_group__in=groups),
'site_group_id'
),
(
Prefix.objects.restrict(request.user, 'view').filter(_site_group__in=groups),
'site_group_id'
),
(
WirelessLAN.objects.restrict(request.user, 'view').filter(_site_group__in=groups),
'site_group_id'
),
),
),
}
Expand Down Expand Up @@ -418,8 +440,8 @@ def get_extra_context(self, request, instance):
'related_models': self.get_related_models(
request,
instance,
[CableTermination, CircuitTermination],
(
omit=(CableTermination, CircuitTermination, Cluster, Prefix, WirelessLAN),
extra=(
(VLANGroup.objects.restrict(request.user, 'view').filter(
scope_type=ContentType.objects.get_for_model(Site),
scope_id=instance.pk
Expand All @@ -429,6 +451,11 @@ def get_extra_context(self, request, instance):
Circuit.objects.restrict(request.user, 'view').filter(terminations___site=instance).distinct(),
'site_id'
),

# Handle these relations manually to avoid erroneous filter name resolution
(Cluster.objects.restrict(request.user, 'view').filter(_site=instance), 'site_id'),
(Prefix.objects.restrict(request.user, 'view').filter(_site=instance), 'site_id'),
(WirelessLAN.objects.restrict(request.user, 'view').filter(_site=instance), 'site_id'),
),
),
}
Expand Down Expand Up @@ -506,14 +533,19 @@ def get_extra_context(self, request, instance):
'related_models': self.get_related_models(
request,
locations,
[CableTermination],
(
omit=[CableTermination, Cluster, Prefix, WirelessLAN],
extra=(
(
Circuit.objects.restrict(request.user, 'view').filter(
terminations___location=instance
).distinct(),
'location_id'
),

# Handle these relations manually to avoid erroneous filter name resolution
(Cluster.objects.restrict(request.user, 'view').filter(_location=instance), 'location_id'),
(Prefix.objects.restrict(request.user, 'view').filter(_location=instance), 'location_id'),
(WirelessLAN.objects.restrict(request.user, 'view').filter(_location=instance), 'location_id'),
),
),
}
Expand Down

0 comments on commit c77bfc6

Please sign in to comment.