From 68060cf9e93f17fe15476c39ead5bc0fb4fab5b6 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 16 Oct 2020 15:28:28 -0400 Subject: [PATCH] Convert device front ports list to table --- netbox/dcim/tables/devices.py | 31 +++++++++- netbox/dcim/tables/template_code.py | 20 +++++++ netbox/dcim/views.py | 5 +- netbox/templates/dcim/device.html | 35 +++--------- netbox/templates/dcim/inc/frontport.html | 72 ------------------------ 5 files changed, 60 insertions(+), 103 deletions(-) delete mode 100644 netbox/templates/dcim/inc/frontport.html diff --git a/netbox/dcim/tables/devices.py b/netbox/dcim/tables/devices.py index 3c35ffd41..95e459a47 100644 --- a/netbox/dcim/tables/devices.py +++ b/netbox/dcim/tables/devices.py @@ -11,8 +11,8 @@ from utilities.tables import ( TagColumn, ToggleColumn, ) from .template_code import ( - CABLETERMINATION, CONSOLEPORT_BUTTONS, CONSOLESERVERPORT_BUTTONS, DEVICE_LINK, INTERFACE_IPADDRESSES, - INTERFACE_TAGGED_VLANS, POWEROUTLET_BUTTONS, POWERPORT_BUTTONS, + CABLETERMINATION, CONSOLEPORT_BUTTONS, CONSOLESERVERPORT_BUTTONS, DEVICE_LINK, FRONTPORT_BUTTONS, + INTERFACE_IPADDRESSES, INTERFACE_TAGGED_VLANS, POWEROUTLET_BUTTONS, POWERPORT_BUTTONS, ) __all__ = ( @@ -21,6 +21,7 @@ __all__ = ( 'DeviceBayTable', 'DeviceConsolePortTable', 'DeviceConsoleServerPortTable', + 'DeviceFrontPortTable', 'DeviceImportTable', 'DevicePowerPortTable', 'DevicePowerOutletTable', @@ -420,6 +421,32 @@ class FrontPortTable(DeviceComponentTable, CableTerminationTable): default_columns = ('pk', 'device', 'name', 'label', 'type', 'rear_port', 'rear_port_position', 'description') +class DeviceFrontPortTable(FrontPortTable): + name = tables.TemplateColumn( + template_code=' ' + '{{ value }}' + ) + actions = ButtonsColumn( + model=FrontPort, + buttons=('edit', 'delete'), + prepend_template=FRONTPORT_BUTTONS + ) + + class Meta(DeviceComponentTable.Meta): + model = FrontPort + fields = ( + 'pk', 'name', 'label', 'type', 'rear_port', 'rear_port_position', 'description', 'cable', 'cable_peer', + 'connection', 'tags', 'actions', + ) + default_columns = ( + 'pk', 'name', 'label', 'type', 'rear_port', 'rear_port_position', 'description', 'cable', 'cable_peer', + 'actions', + ) + row_attrs = { + 'class': lambda record: record.cable.get_status_class() if record.cable else '' + } + + class RearPortTable(DeviceComponentTable, CableTerminationTable): tags = TagColumn( url_name='dcim:rearport_list' diff --git a/netbox/dcim/tables/template_code.py b/netbox/dcim/tables/template_code.py index b32d6264e..e7182e1da 100644 --- a/netbox/dcim/tables/template_code.py +++ b/netbox/dcim/tables/template_code.py @@ -137,3 +137,23 @@ POWEROUTLET_BUTTONS = """ {% endif %} """ + +FRONTPORT_BUTTONS = """ +{% if frontport.cable %} + {% include 'dcim/inc/cable_toggle_buttons.html' with cable=record.cable %} +{% elif perms.dcim.add_cable %} + + + + +{% endif %} +""" diff --git a/netbox/dcim/views.py b/netbox/dcim/views.py index ed8652f5e..1056a8078 100644 --- a/netbox/dcim/views.py +++ b/netbox/dcim/views.py @@ -1061,6 +1061,9 @@ class DeviceView(ObjectView): frontports = FrontPort.objects.restrict(request.user, 'view').filter(device=device).prefetch_related( 'rear_port', 'cable', ) + frontport_table = tables.DeviceFrontPortTable(frontports, orderable=False) + if request.user.has_perm('dcim.change_frontport') or request.user.has_perm('dcim.delete_frontport'): + frontport_table.columns.show('pk') # Rear ports rearports = RearPort.objects.restrict(request.user, 'view').filter(device=device).prefetch_related('cable') @@ -1097,7 +1100,7 @@ class DeviceView(ObjectView): 'powerport_table': powerport_table, 'poweroutlet_table': poweroutlet_table, 'interfaces': interfaces, - 'frontports': frontports, + 'frontport_table': frontport_table, 'rearports': rearports, 'devicebays': devicebays, 'inventoryitems': inventoryitems, diff --git a/netbox/templates/dcim/device.html b/netbox/templates/dcim/device.html index d1294ed5c..94f66b345 100644 --- a/netbox/templates/dcim/device.html +++ b/netbox/templates/dcim/device.html @@ -125,7 +125,7 @@ Interfaces {% badge interfaces|length %}
  • - Front Ports {% badge frontports|length %} + Front Ports {% badge frontport_table.rows|length %}
  • Rear Ports {% badge rearports|length %} @@ -542,8 +542,8 @@ Add interfaces -
    {% endif %} +
    @@ -555,28 +555,7 @@
    Front Ports
    - - - - {% if perms.dcim.change_frontport or perms.dcim.delete_frontport %} - - {% endif %} - - - - - - - - - - - - {% for frontport in frontports %} - {% include 'dcim/inc/frontport.html' %} - {% endfor %} - -
    NameTypeRear PortPositionDescriptionCableCable Termination
    + {% include 'responsive_table.html' with table=frontport_table %} -
    {% endif %} +
    @@ -657,8 +636,8 @@ Add rear ports -
    {% endif %} +
    @@ -804,8 +783,8 @@ Add power outlets -
    {% endif %} +
    @@ -857,8 +836,8 @@ Add device bays -
    {% endif %} +
    diff --git a/netbox/templates/dcim/inc/frontport.html b/netbox/templates/dcim/inc/frontport.html deleted file mode 100644 index 91374cb1e..000000000 --- a/netbox/templates/dcim/inc/frontport.html +++ /dev/null @@ -1,72 +0,0 @@ -{% load helpers %} - - - {# Checkbox #} - {% if perms.dcim.change_frontport or perms.dcim.delete_frontport %} - - - - {% endif %} - - {# Name #} - - - {{ frontport }} - - - {# Type #} - {{ frontport.get_type_display }} - - {# Rear port #} - {{ frontport.rear_port }} - {{ frontport.rear_port_position }} - - {# Description #} - {{ frontport.description|placeholder }} - - {# Cable #} - {% if frontport.cable %} - - {{ frontport.cable }} - - - - - {% include 'dcim/inc/cabletermination.html' with termination=frontport.get_cable_peer %} - {% else %} - - Not connected - - {% endif %} - - {# Actions #} - - {% if frontport.cable %} - {% include 'dcim/inc/cable_toggle_buttons.html' with cable=frontport.cable %} - {% elif perms.dcim.add_cable %} - - - - - {% endif %} - {% if perms.dcim.change_frontport %} - - - - {% endif %} - {% if perms.dcim.delete_frontport %} - - - - {% endif %} - -