|
|
@@ -4,18 +4,16 @@ from django.core.paginator import EmptyPage, PageNotAnInteger
|
|
|
from django.db import transaction
|
|
|
from django.db.models import Prefetch
|
|
|
from django.forms import ModelMultipleChoiceField, MultipleHiddenInput, modelformset_factory
|
|
|
-from django.http import HttpResponse
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
|
from django.urls import reverse
|
|
|
from django.utils.html import escape
|
|
|
from django.utils.safestring import mark_safe
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
from django.views.generic import View
|
|
|
-from jinja2.exceptions import TemplateError
|
|
|
|
|
|
from circuits.models import Circuit, CircuitTermination
|
|
|
-from extras.views import ObjectConfigContextView
|
|
|
-from ipam.models import ASN, IPAddress, VLANGroup
|
|
|
+from extras.views import ObjectConfigContextView, ObjectRenderConfigView
|
|
|
+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
|
|
|
@@ -30,8 +28,9 @@ from utilities.views import (
|
|
|
)
|
|
|
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 *
|
|
|
@@ -238,6 +237,7 @@ class RegionView(GetRelatedModelsMixin, generic.ObjectView):
|
|
|
'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'),
|
|
|
@@ -247,6 +247,11 @@ class RegionView(GetRelatedModelsMixin, generic.ObjectView):
|
|
|
).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'),
|
|
|
),
|
|
|
),
|
|
|
}
|
|
|
@@ -331,6 +336,7 @@ class SiteGroupView(GetRelatedModelsMixin, generic.ObjectView):
|
|
|
'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'),
|
|
|
@@ -340,6 +346,20 @@ class SiteGroupView(GetRelatedModelsMixin, generic.ObjectView):
|
|
|
).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'
|
|
|
+ ),
|
|
|
),
|
|
|
),
|
|
|
}
|
|
|
@@ -402,7 +422,8 @@ class SiteGroupContactsView(ObjectContactsView):
|
|
|
@register_model_view(Site, 'list', path='', detail=False)
|
|
|
class SiteListView(generic.ObjectListView):
|
|
|
queryset = Site.objects.annotate(
|
|
|
- device_count=count_related(Device, 'site')
|
|
|
+ device_count=count_related(Device, 'site'),
|
|
|
+ asn_count=count_related(ASN, 'sites')
|
|
|
)
|
|
|
filterset = filtersets.SiteFilterSet
|
|
|
filterset_form = forms.SiteFilterForm
|
|
|
@@ -418,8 +439,8 @@ class SiteView(GetRelatedModelsMixin, generic.ObjectView):
|
|
|
'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
|
|
|
@@ -429,6 +450,11 @@ class SiteView(GetRelatedModelsMixin, generic.ObjectView):
|
|
|
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'),
|
|
|
),
|
|
|
),
|
|
|
}
|
|
|
@@ -479,18 +505,24 @@ class SiteContactsView(ObjectContactsView):
|
|
|
@register_model_view(Location, 'list', path='', detail=False)
|
|
|
class LocationListView(generic.ObjectListView):
|
|
|
queryset = Location.objects.add_related_count(
|
|
|
- Location.objects.add_related_count(
|
|
|
- Location.objects.all(),
|
|
|
- Device,
|
|
|
- 'location',
|
|
|
- 'device_count',
|
|
|
- cumulative=True
|
|
|
- ),
|
|
|
- Rack,
|
|
|
- 'location',
|
|
|
- 'rack_count',
|
|
|
- cumulative=True
|
|
|
- )
|
|
|
+ Location.objects.add_related_count(
|
|
|
+ Location.objects.add_related_count(
|
|
|
+ Location.objects.all(),
|
|
|
+ Device,
|
|
|
+ 'location',
|
|
|
+ 'device_count',
|
|
|
+ cumulative=True
|
|
|
+ ),
|
|
|
+ Rack,
|
|
|
+ 'location',
|
|
|
+ 'rack_count',
|
|
|
+ cumulative=True
|
|
|
+ ),
|
|
|
+ VLANGroup,
|
|
|
+ 'location',
|
|
|
+ 'vlangroup_count',
|
|
|
+ cumulative=True
|
|
|
+ )
|
|
|
filterset = filtersets.LocationFilterSet
|
|
|
filterset_form = forms.LocationFilterForm
|
|
|
table = tables.LocationTable
|
|
|
@@ -502,18 +534,26 @@ class LocationView(GetRelatedModelsMixin, generic.ObjectView):
|
|
|
|
|
|
def get_extra_context(self, request, instance):
|
|
|
locations = instance.get_descendants(include_self=True)
|
|
|
+ location_content_type = ContentType.objects.get_for_model(instance)
|
|
|
return {
|
|
|
'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'),
|
|
|
+ (VLANGroup.objects.restrict(request.user, 'view').filter(
|
|
|
+ scope_type_id=location_content_type.id, scope_id=instance.id), 'location'),
|
|
|
),
|
|
|
),
|
|
|
}
|
|
|
@@ -934,6 +974,7 @@ class RackReservationBulkDeleteView(generic.BulkDeleteView):
|
|
|
@register_model_view(Manufacturer, 'list', path='', detail=False)
|
|
|
class ManufacturerListView(generic.ObjectListView):
|
|
|
queryset = Manufacturer.objects.annotate(
|
|
|
+ racktype_count=count_related(RackType, 'manufacturer'),
|
|
|
devicetype_count=count_related(DeviceType, 'manufacturer'),
|
|
|
moduletype_count=count_related(ModuleType, 'manufacturer'),
|
|
|
inventoryitem_count=count_related(InventoryItem, 'manufacturer'),
|
|
|
@@ -2221,53 +2262,13 @@ class DeviceConfigContextView(ObjectConfigContextView):
|
|
|
|
|
|
|
|
|
@register_model_view(Device, 'render-config')
|
|
|
-class DeviceRenderConfigView(generic.ObjectView):
|
|
|
+class DeviceRenderConfigView(ObjectRenderConfigView):
|
|
|
queryset = Device.objects.all()
|
|
|
- template_name = 'dcim/device/render_config.html'
|
|
|
+ base_template = 'dcim/device/base.html'
|
|
|
tab = ViewTab(
|
|
|
label=_('Render Config'),
|
|
|
- weight=2100
|
|
|
- )
|
|
|
-
|
|
|
- def get(self, request, **kwargs):
|
|
|
- instance = self.get_object(**kwargs)
|
|
|
- context = self.get_extra_context(request, instance)
|
|
|
-
|
|
|
- # If a direct export has been requested, return the rendered template content as a
|
|
|
- # downloadable file.
|
|
|
- if request.GET.get('export'):
|
|
|
- content = context['rendered_config'] or context['error_message']
|
|
|
- response = HttpResponse(content, content_type='text')
|
|
|
- filename = f"{instance.name or 'config'}.txt"
|
|
|
- response['Content-Disposition'] = f'attachment; filename="{filename}"'
|
|
|
- return response
|
|
|
-
|
|
|
- return render(request, self.get_template_name(), {
|
|
|
- 'object': instance,
|
|
|
- 'tab': self.tab,
|
|
|
- **context,
|
|
|
- })
|
|
|
-
|
|
|
- def get_extra_context(self, request, instance):
|
|
|
- # Compile context data
|
|
|
- context_data = instance.get_config_context()
|
|
|
- context_data.update({'device': instance})
|
|
|
-
|
|
|
- # Render the config template
|
|
|
- rendered_config = None
|
|
|
- error_message = None
|
|
|
- if config_template := instance.get_config_template():
|
|
|
- try:
|
|
|
- rendered_config = config_template.render(context=context_data)
|
|
|
- except TemplateError as e:
|
|
|
- error_message = _("An error occurred while rendering the template: {error}").format(error=e)
|
|
|
-
|
|
|
- return {
|
|
|
- 'config_template': config_template,
|
|
|
- 'context_data': context_data,
|
|
|
- 'rendered_config': rendered_config,
|
|
|
- 'error_message': error_message,
|
|
|
- }
|
|
|
+ weight=2100,
|
|
|
+ )
|
|
|
|
|
|
|
|
|
@register_model_view(Device, 'virtual-machines')
|