Skip to content

Commit

Permalink
Closes #13794: Dynamically populate related objects list under tenant…
Browse files Browse the repository at this point in the history
… view
  • Loading branch information
jeremystretch committed Nov 6, 2023
1 parent 8dcbd66 commit bb7d3df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
35 changes: 3 additions & 32 deletions netbox/tenancy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
from django.shortcuts import get_object_or_404
from django.utils.translation import gettext as _

from circuits.models import Circuit
from dcim.models import Cable, Device, Location, PowerFeed, Rack, RackReservation, Site, VirtualDeviceContext
from ipam.models import Aggregate, ASN, IPAddress, IPRange, L2VPN, Prefix, VLAN, VRF
from netbox.views import generic
from utilities.utils import count_related
from utilities.utils import count_related, get_related_models
from utilities.views import register_model_view, ViewTab
from virtualization.models import VirtualMachine, Cluster
from wireless.models import WirelessLAN, WirelessLink
from . import filtersets, forms, tables
from .models import *

Expand Down Expand Up @@ -132,32 +127,8 @@ class TenantView(generic.ObjectView):

def get_extra_context(self, request, instance):
related_models = [
# DCIM
(Site.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(Rack.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(RackReservation.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(Location.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(Device.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(VirtualDeviceContext.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(Cable.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(PowerFeed.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
# IPAM
(VRF.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(Aggregate.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(Prefix.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(IPRange.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(IPAddress.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(ASN.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(VLAN.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(L2VPN.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
# Circuits
(Circuit.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
# Virtualization
(VirtualMachine.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(Cluster.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
# Wireless
(WirelessLAN.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(WirelessLink.objects.restrict(request.user, 'view').filter(tenant=instance), 'tenant_id'),
(model.objects.restrict(request.user, 'view').filter(tenant=instance), f'{field}_id')
for model, field in get_related_models(Tenant)
]

return {
Expand Down
16 changes: 15 additions & 1 deletion netbox/utilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import bleach
from django.contrib.contenttypes.models import ContentType
from django.core import serializers
from django.db.models import Count, OuterRef, Subquery
from django.db.models import Count, ManyToOneRel, OuterRef, Subquery
from django.db.models.functions import Coalesce
from django.http import QueryDict
from django.utils import timezone
Expand Down Expand Up @@ -567,3 +567,17 @@ def local_now():
Return the current date & time in the system timezone.
"""
return localtime(timezone.now())


def get_related_models(model):
"""
Return a list of all models which have a ForeignKey to the given model and the name of the field.
"""
related_models = []
for field in model._meta.get_fields():
if type(field) is ManyToOneRel:
related_models.append(
(field.related_model, field.remote_field.name)
)

return related_models

0 comments on commit bb7d3df

Please sign in to comment.