From 5a0e8cd545b94feb4b01798a17cfe154bdedf19a Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 16 Oct 2020 14:39:15 -0400 Subject: [PATCH] Convert device console ports list to table --- netbox/dcim/tables/devices.py | 26 +++++++- netbox/dcim/tables/template_code.py | 21 ++++++ netbox/dcim/views.py | 5 +- netbox/templates/dcim/device.html | 27 ++------ netbox/templates/dcim/inc/consoleport.html | 77 ---------------------- 5 files changed, 55 insertions(+), 101 deletions(-) delete mode 100644 netbox/templates/dcim/inc/consoleport.html diff --git a/netbox/dcim/tables/devices.py b/netbox/dcim/tables/devices.py index 21c2f4244..5f61e03ba 100644 --- a/netbox/dcim/tables/devices.py +++ b/netbox/dcim/tables/devices.py @@ -10,11 +10,14 @@ from utilities.tables import ( BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ColoredLabelColumn, LinkedCountColumn, TagColumn, ToggleColumn, ) -from .template_code import CABLETERMINATION, DEVICE_LINK, INTERFACE_IPADDRESSES, INTERFACE_TAGGED_VLANS +from .template_code import ( + CABLETERMINATION, CONSOLEPORT_BUTTONS, DEVICE_LINK, INTERFACE_IPADDRESSES, INTERFACE_TAGGED_VLANS, +) __all__ = ( 'ConsolePortTable', 'ConsoleServerPortTable', + 'DeviceConsolePortTable', 'DeviceImportTable', 'DeviceTable', 'DeviceBayTable', @@ -237,6 +240,27 @@ class ConsolePortTable(DeviceComponentTable, PathEndpointTable): default_columns = ('pk', 'device', 'name', 'label', 'type', 'description') +class DeviceConsolePortTable(ConsolePortTable): + name = tables.TemplateColumn( + template_code=' {{ value }}' + ) + actions = ButtonsColumn( + model=ConsolePort, + buttons=('edit', 'delete'), + prepend_template=CONSOLEPORT_BUTTONS + ) + + class Meta(DeviceComponentTable.Meta): + model = ConsolePort + fields = ( + 'pk', 'name', 'label', 'type', 'description', 'cable', 'cable_peer', 'connection', 'tags', 'actions' + ) + default_columns = ('pk', 'name', 'label', 'type', 'description', 'cable', 'cable_peer', 'actions') + row_attrs = { + 'class': lambda record: record.cable.get_status_class() if record.cable else '' + } + + class ConsoleServerPortTable(DeviceComponentTable, PathEndpointTable): tags = TagColumn( url_name='dcim:consoleserverport_list' diff --git a/netbox/dcim/tables/template_code.py b/netbox/dcim/tables/template_code.py index 7278de113..e17a5a877 100644 --- a/netbox/dcim/tables/template_code.py +++ b/netbox/dcim/tables/template_code.py @@ -73,3 +73,24 @@ UTILIZATION_GRAPH = """ {% load helpers %} {% utilization_graph value %} """ + +# +# Device component buttons +# + +CONSOLEPORT_BUTTONS = """ +{% if record.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 06fb7619f..3cf011503 100644 --- a/netbox/dcim/views.py +++ b/netbox/dcim/views.py @@ -1019,6 +1019,9 @@ class DeviceView(ObjectView): consoleports = ConsolePort.objects.restrict(request.user, 'view').filter(device=device).prefetch_related( 'cable', '_path__destination', ) + consoleport_table = tables.DeviceConsolePortTable(consoleports, orderable=False) + if request.user.has_perm('dcim.change_consoleport') or request.user.has_perm('dcim.delete_consoleport'): + consoleport_table.columns.show('pk') # Console server ports consoleserverports = ConsoleServerPort.objects.restrict(request.user, 'view').filter( @@ -1079,7 +1082,7 @@ class DeviceView(ObjectView): return render(request, 'dcim/device.html', { 'device': device, - 'consoleports': consoleports, + 'consoleport_table': consoleport_table, 'consoleserverports': consoleserverports, 'powerports': powerports, 'poweroutlets': poweroutlets, diff --git a/netbox/templates/dcim/device.html b/netbox/templates/dcim/device.html index c06f86bf0..1a74016b2 100644 --- a/netbox/templates/dcim/device.html +++ b/netbox/templates/dcim/device.html @@ -131,7 +131,7 @@ Rear Ports {% badge rearports|length %}
  • - Console Ports {% badge consoleports|length %} + Console Ports {% badge consoleport_table.rows|length %}
  • Console Server Ports {% badge consoleserverports|length %} @@ -670,27 +670,9 @@
    Console Ports
    - - - - {% if perms.dcim.change_consoleport or perms.dcim.delete_consoleport %} - - {% endif %} - - - - - - - - - - {% for cp in consoleports %} - {% include 'dcim/inc/consoleport.html' %} - {% endfor %} -
    NameTypeDescriptionCableCable TerminationConnection
    + {% include 'responsive_table.html' with table=consoleport_table %} {% endif %} +
    diff --git a/netbox/templates/dcim/inc/consoleport.html b/netbox/templates/dcim/inc/consoleport.html deleted file mode 100644 index ace09cfe2..000000000 --- a/netbox/templates/dcim/inc/consoleport.html +++ /dev/null @@ -1,77 +0,0 @@ - - - {# Checkbox #} - {% if perms.dcim.change_consoleport or perms.dcim.delete_consoleport %} - - - - {% endif %} - - {# Name #} - - - {{ cp }} - - - {# Type #} - - {% if cp.type %}{{ cp.get_type_display }}{% else %}—{% endif %} - - - {# Description #} - - {{ cp.description }} - - - {# Cable #} - {% if cp.cable %} - - {{ cp.cable }} - - - - - {% include 'dcim/inc/cabletermination.html' with termination=cp.get_cable_peer %} - {% else %} - - Not connected - - {% endif %} - - {# Connection #} - {% include 'dcim/inc/endpoint_connection.html' with path=cp.path %} - - {# Actions #} - - {% if cp.cable %} - {% include 'dcim/inc/cable_toggle_buttons.html' with cable=cp.cable %} - {% elif perms.dcim.add_cable %} - - - - - {% endif %} - {% if perms.dcim.change_consoleport %} - - - - {% endif %} - {% if perms.dcim.delete_consoleport %} - {% if cp.connected_endpoint %} - - {% else %} - - - - {% endif %} - {% endif %} - -