mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-22 20:12:00 -06:00
Moved object context rendering to ObjectConfigContextView and standardized the template
This commit is contained in:
parent
b952ec73ce
commit
62989ecb6e
@ -19,6 +19,7 @@ from natsort import natsorted
|
||||
|
||||
from circuits.models import Circuit
|
||||
from extras.models import Graph, TopologyMap, GRAPH_TYPE_INTERFACE, GRAPH_TYPE_SITE
|
||||
from extras.views import ObjectConfigContextView
|
||||
from ipam.models import Prefix, Service, VLAN
|
||||
from utilities.forms import ConfirmationForm
|
||||
from utilities.paginator import EnhancedPaginator
|
||||
@ -994,16 +995,9 @@ class DeviceConfigView(PermissionRequiredMixin, View):
|
||||
})
|
||||
|
||||
|
||||
class DeviceConfigContextView(View):
|
||||
|
||||
def get(self, request, pk):
|
||||
|
||||
device = get_object_or_404(Device, pk=pk)
|
||||
|
||||
return render(request, 'dcim/device_configcontext.html', {
|
||||
'device': device,
|
||||
'active_tab': 'config-context',
|
||||
})
|
||||
class DeviceConfigContextView(ObjectConfigContextView):
|
||||
object_class = Device
|
||||
base_template = 'dcim/device.html'
|
||||
|
||||
|
||||
class DeviceCreateView(PermissionRequiredMixin, ObjectEditView):
|
||||
|
@ -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
|
||||
#
|
||||
|
@ -1,19 +0,0 @@
|
||||
{% extends 'dcim/device.html' %}
|
||||
{% load helpers %}
|
||||
|
||||
{% block title %}{{ device }} - Config Context{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<strong>Config Context</strong>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<pre>{{ device.get_config_context|render_json }}</pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
35
netbox/templates/extras/object_configcontext.html
Normal file
35
netbox/templates/extras/object_configcontext.html
Normal file
@ -0,0 +1,35 @@
|
||||
{% extends base_template %}
|
||||
{% load helpers %}
|
||||
|
||||
{% block title %}{{ block.super }} - Config Context{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<strong>Rendered Context</strong>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<pre>{{ rendered_context|render_json }}</pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<strong>Source Contexts</strong>
|
||||
</div>
|
||||
{% for context in source_contexts %}
|
||||
<div class="panel-body">
|
||||
<div class="pull-right">
|
||||
<span class="text-muted">{{ context.weight }}</span>
|
||||
</div>
|
||||
<a href="{{ context.get_absolute_url }}"><strong>{{ context.name }}</strong></a>
|
||||
<pre>{{ context.data|render_json }}</pre>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
@ -1,18 +0,0 @@
|
||||
{% extends 'virtualization/virtualmachine.html' %}
|
||||
|
||||
{% block title %}{{ virtualmachine }} - Config Context{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<strong>Config Context</strong>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<pre>{{ virtualmachine.get_config_context }}</pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
@ -9,6 +9,7 @@ from django.views.generic import View
|
||||
|
||||
from dcim.models import Device, Interface
|
||||
from dcim.tables import DeviceTable
|
||||
from extras.views import ObjectConfigContextView
|
||||
from ipam.models import Service
|
||||
from utilities.views import (
|
||||
BulkComponentCreateView, BulkDeleteView, BulkEditView, BulkImportView, ComponentCreateView, ObjectDeleteView,
|
||||
@ -269,16 +270,9 @@ class VirtualMachineView(View):
|
||||
})
|
||||
|
||||
|
||||
class VirtualMachineConfigContextView(View):
|
||||
|
||||
def get(self, request, pk):
|
||||
|
||||
virtualmachine = get_object_or_404(VirtualMachine, pk=pk)
|
||||
|
||||
return render(request, 'virtualization/virtualmachine_configcontext.html', {
|
||||
'virtualmachine': virtualmachine,
|
||||
'active_tab': 'config-context',
|
||||
})
|
||||
class VirtualMachineConfigContextView(ObjectConfigContextView):
|
||||
object_class = VirtualMachine
|
||||
base_template = 'virtualization/virtualmachine.html'
|
||||
|
||||
|
||||
class VirtualMachineCreateView(PermissionRequiredMixin, ObjectEditView):
|
||||
|
Loading…
Reference in New Issue
Block a user