Skip to content

Commit

Permalink
15794 Make "related objects" dynamic (#15876)
Browse files Browse the repository at this point in the history
* Closes #15794: Make "related objects" dynamic

Instead of hardcoding relationships between models for the detail view,
they are now dynamically generated.

* Fix related models call

* Remove extra related models hook

Instead of providing a rarely used hook method, additional related
models can now be passed directly to the lookup method.

* Fix relations view for ASNs

ASNs have ManyToMany relationships and therefore can't used automatic
resolving. Explicit relations have been restored as before.

* Add method call keywords for clarification

* Cleanup related models

---------

Co-authored-by: Jeremy Stretch <[email protected]>
  • Loading branch information
alehaa and jeremystretch authored Jun 12, 2024
1 parent 763d65b commit 5353f83
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 238 deletions.
47 changes: 18 additions & 29 deletions netbox/circuits/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from tenancy.views import ObjectContactsView
from utilities.forms import ConfirmationForm
from utilities.query import count_related
from utilities.views import register_model_view
from utilities.views import GetRelatedModelsMixin, register_model_view
from . import filtersets, forms, tables
from .models import *

Expand All @@ -26,17 +26,12 @@ class ProviderListView(generic.ObjectListView):


@register_model_view(Provider)
class ProviderView(generic.ObjectView):
class ProviderView(GetRelatedModelsMixin, generic.ObjectView):
queryset = Provider.objects.all()

def get_extra_context(self, request, instance):
related_models = (
(ProviderAccount.objects.restrict(request.user, 'view').filter(provider=instance), 'provider_id'),
(Circuit.objects.restrict(request.user, 'view').filter(provider=instance), 'provider_id'),
)

return {
'related_models': related_models,
'related_models': self.get_related_models(request, instance),
}


Expand Down Expand Up @@ -92,16 +87,12 @@ class ProviderAccountListView(generic.ObjectListView):


@register_model_view(ProviderAccount)
class ProviderAccountView(generic.ObjectView):
class ProviderAccountView(GetRelatedModelsMixin, generic.ObjectView):
queryset = ProviderAccount.objects.all()

def get_extra_context(self, request, instance):
related_models = (
(Circuit.objects.restrict(request.user, 'view').filter(provider_account=instance), 'provider_account_id'),
)

return {
'related_models': related_models,
'related_models': self.get_related_models(request, instance),
}


Expand Down Expand Up @@ -156,19 +147,21 @@ class ProviderNetworkListView(generic.ObjectListView):


@register_model_view(ProviderNetwork)
class ProviderNetworkView(generic.ObjectView):
class ProviderNetworkView(GetRelatedModelsMixin, generic.ObjectView):
queryset = ProviderNetwork.objects.all()

def get_extra_context(self, request, instance):
related_models = (
(
Circuit.objects.restrict(request.user, 'view').filter(terminations__provider_network=instance),
'provider_network_id',
),
)

return {
'related_models': related_models,
'related_models': self.get_related_models(
request,
instance,
extra=(
(
Circuit.objects.restrict(request.user, 'view').filter(terminations__provider_network=instance),
'provider_network_id',
),
),
),
}


Expand Down Expand Up @@ -215,16 +208,12 @@ class CircuitTypeListView(generic.ObjectListView):


@register_model_view(CircuitType)
class CircuitTypeView(generic.ObjectView):
class CircuitTypeView(GetRelatedModelsMixin, generic.ObjectView):
queryset = CircuitType.objects.all()

def get_extra_context(self, request, instance):
related_models = (
(Circuit.objects.restrict(request.user, 'view').filter(type=instance), 'type_id'),
)

return {
'related_models': related_models,
'related_models': self.get_related_models(request, instance),
}


Expand Down
10 changes: 3 additions & 7 deletions netbox/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from utilities.forms import ConfirmationForm
from utilities.htmx import htmx_partial
from utilities.query import count_related
from utilities.views import ContentTypePermissionRequiredMixin, register_model_view
from utilities.views import ContentTypePermissionRequiredMixin, GetRelatedModelsMixin, register_model_view
from . import filtersets, forms, tables
from .models import *

Expand All @@ -51,16 +51,12 @@ class DataSourceListView(generic.ObjectListView):


@register_model_view(DataSource)
class DataSourceView(generic.ObjectView):
class DataSourceView(GetRelatedModelsMixin, generic.ObjectView):
queryset = DataSource.objects.all()

def get_extra_context(self, request, instance):
related_models = (
(DataFile.objects.restrict(request.user, 'view').filter(source=instance), 'source_id'),
)

return {
'related_models': related_models,
'related_models': self.get_related_models(request, instance),
}


Expand Down
Loading

0 comments on commit 5353f83

Please sign in to comment.