mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-21 12:52:21 -06:00
Moved object context rendering to ObjectConfigContextView and standardized the template
This commit is contained in:
@@ -2,7 +2,6 @@ from __future__ import unicode_literals
|
||||
|
||||
from collections import OrderedDict
|
||||
from datetime import date
|
||||
import json
|
||||
|
||||
import graphviz
|
||||
from django.contrib.auth.models import User
|
||||
@@ -21,6 +20,7 @@ from django.utils.safestring import mark_safe
|
||||
from dcim.constants import CONNECTION_STATUS_CONNECTED
|
||||
from utilities.utils import foreground_color
|
||||
from .constants import *
|
||||
from .querysets import ConfigContextQuerySet
|
||||
|
||||
|
||||
#
|
||||
@@ -676,6 +676,8 @@ class ConfigContext(models.Model):
|
||||
)
|
||||
data = JSONField()
|
||||
|
||||
objects = ConfigContextQuerySet.as_manager()
|
||||
|
||||
class Meta:
|
||||
ordering = ['weight', 'name']
|
||||
|
||||
@@ -696,22 +698,9 @@ class ConfigContextModel(models.Model):
|
||||
Return the rendered configuration context for a device or VM.
|
||||
"""
|
||||
|
||||
# `device_role` for Device; `role` for VirtualMachine
|
||||
role = getattr(self, 'device_role', None) or self.role
|
||||
|
||||
# Gather all ConfigContexts orders by weight, name
|
||||
contexts = ConfigContext.objects.filter(
|
||||
Q(regions=self.site.region) | Q(regions=None),
|
||||
Q(sites=self.site) | Q(sites=None),
|
||||
Q(roles=role) | Q(roles=None),
|
||||
Q(tenants=self.tenant) | Q(tenants=None),
|
||||
Q(platforms=self.platform) | Q(platforms=None),
|
||||
is_active=True,
|
||||
).order_by('weight', 'name')
|
||||
|
||||
# Compile all config data, overwriting lower-weight values with higher-weight values where a collision occurs
|
||||
data = {}
|
||||
for context in contexts:
|
||||
for context in ConfigContext.objects.get_for_object(self):
|
||||
data.update(context.data)
|
||||
|
||||
return data
|
||||
|
||||
23
netbox/extras/querysets.py
Normal file
23
netbox/extras/querysets.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db.models import Q, QuerySet
|
||||
|
||||
|
||||
class ConfigContextQuerySet(QuerySet):
|
||||
|
||||
def get_for_object(self, obj):
|
||||
"""
|
||||
Return all applicable ConfigContexts for a given object. Only active ConfigContexts will be included.
|
||||
"""
|
||||
|
||||
# `device_role` for Device; `role` for VirtualMachine
|
||||
role = getattr(obj, 'device_role', None) or obj.role
|
||||
|
||||
return self.filter(
|
||||
Q(regions=obj.site.region) | Q(regions=None),
|
||||
Q(sites=obj.site) | Q(sites=None),
|
||||
Q(roles=role) | Q(roles=None),
|
||||
Q(tenants=obj.tenant) | Q(tenants=None),
|
||||
Q(platforms=obj.platform) | Q(platforms=None),
|
||||
is_active=True,
|
||||
).order_by('weight', 'name')
|
||||
@@ -100,6 +100,24 @@ class ConfigContextBulkDeleteView(PermissionRequiredMixin, BulkDeleteView):
|
||||
default_return_url = 'extras:configcontext_list'
|
||||
|
||||
|
||||
class ObjectConfigContextView(View):
|
||||
object_class = None
|
||||
base_template = None
|
||||
|
||||
def get(self, request, pk):
|
||||
|
||||
obj = get_object_or_404(self.object_class, pk=pk)
|
||||
source_contexts = ConfigContext.objects.get_for_object(obj)
|
||||
|
||||
return render(request, 'extras/object_configcontext.html', {
|
||||
self.object_class._meta.model_name: obj,
|
||||
'rendered_context': obj.get_config_context(),
|
||||
'source_contexts': source_contexts,
|
||||
'base_template': self.base_template,
|
||||
'active_tab': 'config-context',
|
||||
})
|
||||
|
||||
|
||||
#
|
||||
# Change logging
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user