Update device/VM component views to use ObjectChildrenView

This commit is contained in:
jeremystretch 2021-12-14 13:41:01 -05:00
parent cfdb985d00
commit 8d3b296eed
3 changed files with 33 additions and 50 deletions

View File

@ -36,26 +36,15 @@ from .models import (
)
class DeviceComponentsView(generic.ObjectView):
class DeviceComponentsView(generic.ObjectChildrenView):
queryset = Device.objects.all()
model = None
table = None
def get_components(self, request, instance):
return self.model.objects.restrict(request.user, 'view').filter(device=instance)
def get_children(self, request, parent):
return self.child_model.objects.restrict(request.user, 'view').filter(device=parent)
def get_extra_context(self, request, instance):
components = self.get_components(request, instance)
table = self.table(data=components, user=request.user)
change_perm = f'{self.model._meta.app_label}.change_{self.model._meta.model_name}'
delete_perm = f'{self.model._meta.app_label}.delete_{self.model._meta.model_name}'
if request.user.has_perm(change_perm) or request.user.has_perm(delete_perm):
table.columns.show('pk')
paginate_table(table, request)
return {
'table': table,
'active_tab': f"{self.model._meta.verbose_name_plural.replace(' ', '-')}",
'active_tab': f"{self.child_model._meta.verbose_name_plural.replace(' ', '-')}",
}
@ -63,8 +52,8 @@ class DeviceTypeComponentsView(DeviceComponentsView):
queryset = DeviceType.objects.all()
template_name = 'dcim/devicetype/component_templates.html'
def get_components(self, request, instance):
return self.model.objects.restrict(request.user, 'view').filter(device_type=instance)
def get_children(self, request, parent):
return self.child_model.objects.restrict(request.user, 'view').filter(device_type=parent)
class BulkDisconnectView(GetReturnURLMixin, ObjectPermissionRequiredMixin, View):
@ -806,42 +795,42 @@ class DeviceTypeView(generic.ObjectView):
class DeviceTypeConsolePortsView(DeviceTypeComponentsView):
model = ConsolePortTemplate
child_model = ConsolePortTemplate
table = tables.ConsolePortTemplateTable
class DeviceTypeConsoleServerPortsView(DeviceTypeComponentsView):
model = ConsoleServerPortTemplate
child_model = ConsoleServerPortTemplate
table = tables.ConsoleServerPortTemplateTable
class DeviceTypePowerPortsView(DeviceTypeComponentsView):
model = PowerPortTemplate
child_model = PowerPortTemplate
table = tables.PowerPortTemplateTable
class DeviceTypePowerOutletsView(DeviceTypeComponentsView):
model = PowerOutletTemplate
child_model = PowerOutletTemplate
table = tables.PowerOutletTemplateTable
class DeviceTypeInterfacesView(DeviceTypeComponentsView):
model = InterfaceTemplate
child_model = InterfaceTemplate
table = tables.InterfaceTemplateTable
class DeviceTypeFrontPortsView(DeviceTypeComponentsView):
model = FrontPortTemplate
child_model = FrontPortTemplate
table = tables.FrontPortTemplateTable
class DeviceTypeRearPortsView(DeviceTypeComponentsView):
model = RearPortTemplate
child_model = RearPortTemplate
table = tables.RearPortTemplateTable
class DeviceTypeDeviceBaysView(DeviceTypeComponentsView):
model = DeviceBayTemplate
child_model = DeviceBayTemplate
table = tables.DeviceBayTemplateTable
@ -1337,61 +1326,61 @@ class DeviceView(generic.ObjectView):
class DeviceConsolePortsView(DeviceComponentsView):
model = ConsolePort
child_model = ConsolePort
table = tables.DeviceConsolePortTable
template_name = 'dcim/device/consoleports.html'
class DeviceConsoleServerPortsView(DeviceComponentsView):
model = ConsoleServerPort
child_model = ConsoleServerPort
table = tables.DeviceConsoleServerPortTable
template_name = 'dcim/device/consoleserverports.html'
class DevicePowerPortsView(DeviceComponentsView):
model = PowerPort
child_model = PowerPort
table = tables.DevicePowerPortTable
template_name = 'dcim/device/powerports.html'
class DevicePowerOutletsView(DeviceComponentsView):
model = PowerOutlet
child_model = PowerOutlet
table = tables.DevicePowerOutletTable
template_name = 'dcim/device/poweroutlets.html'
class DeviceInterfacesView(DeviceComponentsView):
model = Interface
child_model = Interface
table = tables.DeviceInterfaceTable
template_name = 'dcim/device/interfaces.html'
def get_components(self, request, instance):
return instance.vc_interfaces().restrict(request.user, 'view').prefetch_related(
def get_children(self, request, parent):
return parent.vc_interfaces().restrict(request.user, 'view').prefetch_related(
Prefetch('ip_addresses', queryset=IPAddress.objects.restrict(request.user)),
Prefetch('member_interfaces', queryset=Interface.objects.restrict(request.user))
)
class DeviceFrontPortsView(DeviceComponentsView):
model = FrontPort
child_model = FrontPort
table = tables.DeviceFrontPortTable
template_name = 'dcim/device/frontports.html'
class DeviceRearPortsView(DeviceComponentsView):
model = RearPort
child_model = RearPort
table = tables.DeviceRearPortTable
template_name = 'dcim/device/rearports.html'
class DeviceDeviceBaysView(DeviceComponentsView):
model = DeviceBay
child_model = DeviceBay
table = tables.DeviceDeviceBayTable
template_name = 'dcim/device/devicebays.html'
class DeviceInventoryView(DeviceComponentsView):
model = InventoryItem
child_model = InventoryItem
table = tables.DeviceInventoryItemTable
template_name = 'dcim/device/inventory.html'

View File

@ -8,7 +8,7 @@
{% csrf_token %}
{% include 'inc/table_controls.html' with table_modal="VirtualMachineVMInterfaceTable_config" %}
<div class="table-responsive">
{% render_table interface_table 'inc/table.html' %}
{% render_table table 'inc/table.html' %}
</div>
<div class="noprint">
{% if perms.virtualization.change_vminterface %}
@ -34,5 +34,5 @@
<div class="clearfix"></div>
</div>
</form>
{% table_config_form interface_table %}
{% table_config_form table %}
{% endblock %}

View File

@ -347,26 +347,20 @@ class VirtualMachineView(generic.ObjectView):
}
class VirtualMachineInterfacesView(generic.ObjectView):
class VirtualMachineInterfacesView(generic.ObjectChildrenView):
queryset = VirtualMachine.objects.all()
child_model = VMInterface
table = tables.VMInterfaceTable
template_name = 'virtualization/virtualmachine/interfaces.html'
def get_extra_context(self, request, instance):
interfaces = instance.interfaces.restrict(request.user, 'view').prefetch_related(
def get_children(self, request, parent):
return parent.interfaces.restrict(request.user, 'view').prefetch_related(
Prefetch('ip_addresses', queryset=IPAddress.objects.restrict(request.user)),
'tags',
)
interface_table = tables.VirtualMachineVMInterfaceTable(
data=interfaces,
user=request.user,
orderable=False
)
if request.user.has_perm('virtualization.change_vminterface') or \
request.user.has_perm('virtualization.delete_vminterface'):
interface_table.columns.show('pk')
def get_extra_context(self, request, instance):
return {
'interface_table': interface_table,
'active_tab': 'interfaces',
}