From 8d682041a43b6176198f64bd80a46ea9ed99d2d8 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Tue, 22 Mar 2022 15:17:34 -0400 Subject: [PATCH] Extend linkify() to accept an attr name for the link text --- netbox/templates/dcim/consoleport.html | 4 +--- netbox/templates/dcim/consoleserverport.html | 4 +--- .../dcim/inc/interface_vlans_table.html | 8 ++------ .../dcim/inc/panels/inventory_items.html | 2 +- netbox/templates/dcim/interface.html | 8 ++------ netbox/templates/dcim/powerfeed.html | 4 +--- netbox/templates/dcim/poweroutlet.html | 4 +--- netbox/templates/dcim/powerport.html | 4 +--- netbox/templates/dcim/rack.html | 6 ++---- .../extras/object_configcontext.html | 2 +- netbox/templates/extras/objectchange.html | 2 +- .../ipam/inc/panels/fhrp_groups.html | 15 ++++---------- netbox/templates/ipam/inc/service.html | 2 +- .../templatetags/builtins/filters.py | 20 +++++++++++++------ 14 files changed, 33 insertions(+), 52 deletions(-) diff --git a/netbox/templates/dcim/consoleport.html b/netbox/templates/dcim/consoleport.html index a8934357a..ce2c1655d 100644 --- a/netbox/templates/dcim/consoleport.html +++ b/netbox/templates/dcim/consoleport.html @@ -79,9 +79,7 @@ Name - - {{ object.connected_endpoint.name }} - + {{ object.connected_endpoint|linkify:"name" }} Type diff --git a/netbox/templates/dcim/consoleserverport.html b/netbox/templates/dcim/consoleserverport.html index 799aa57bd..52b1a3229 100644 --- a/netbox/templates/dcim/consoleserverport.html +++ b/netbox/templates/dcim/consoleserverport.html @@ -81,9 +81,7 @@ Name - - {{ object.connected_endpoint.name }} - + {{ object.connected_endpoint|linkify:"name" }} Type diff --git a/netbox/templates/dcim/inc/interface_vlans_table.html b/netbox/templates/dcim/inc/interface_vlans_table.html index 67ffb2954..0c23b5702 100644 --- a/netbox/templates/dcim/inc/interface_vlans_table.html +++ b/netbox/templates/dcim/inc/interface_vlans_table.html @@ -8,9 +8,7 @@ {% with tagged_vlans=obj.tagged_vlans.all %} {% if obj.untagged_vlan and obj.untagged_vlan not in tagged_vlans %} - - {{ obj.untagged_vlan.vid }} - + {{ obj.untagged_vlan|linkify:"vid" }} {{ obj.untagged_vlan.name }} @@ -22,9 +20,7 @@ {% endif %} {% for vlan in tagged_vlans %} - - {{ vlan.vid }} - + {{ vlan|linkify:"vid" }} {{ vlan.name }} diff --git a/netbox/templates/dcim/inc/panels/inventory_items.html b/netbox/templates/dcim/inc/panels/inventory_items.html index d8ca32dbd..d89b64814 100644 --- a/netbox/templates/dcim/inc/panels/inventory_items.html +++ b/netbox/templates/dcim/inc/panels/inventory_items.html @@ -15,7 +15,7 @@ {% for item in object.inventory_items.all %} - {{ item.name }} + {{ item|linkify:"name" }} {{ item.label|placeholder }} {{ item.role|linkify|placeholder }} diff --git a/netbox/templates/dcim/interface.html b/netbox/templates/dcim/interface.html index 53e2f2103..366509242 100644 --- a/netbox/templates/dcim/interface.html +++ b/netbox/templates/dcim/interface.html @@ -152,9 +152,7 @@ Name - - {{ iface.name }} - + {{ iface|linkify:"name" }} Type @@ -370,9 +368,7 @@ — {% endif %} - - {{ wlan.ssid }} - + {{ wlan|linkify:"ssid" }} {% empty %} diff --git a/netbox/templates/dcim/powerfeed.html b/netbox/templates/dcim/powerfeed.html index 18945c0a0..777af5563 100644 --- a/netbox/templates/dcim/powerfeed.html +++ b/netbox/templates/dcim/powerfeed.html @@ -127,9 +127,7 @@ Name - - {{ object.connected_endpoint.name }} - + {{ object.connected_endpoint|linkify:"name" }} Type diff --git a/netbox/templates/dcim/poweroutlet.html b/netbox/templates/dcim/poweroutlet.html index 3900d174a..6408bc759 100644 --- a/netbox/templates/dcim/poweroutlet.html +++ b/netbox/templates/dcim/poweroutlet.html @@ -85,9 +85,7 @@ Name - - {{ object.connected_endpoint.name }} - + {{ object.connected_endpoint|linkify:"name" }} Type diff --git a/netbox/templates/dcim/powerport.html b/netbox/templates/dcim/powerport.html index a542b3591..6dc06f528 100644 --- a/netbox/templates/dcim/powerport.html +++ b/netbox/templates/dcim/powerport.html @@ -85,9 +85,7 @@ Name - - {{ object.connected_endpoint.name }} - + {{ object.connected_endpoint|linkify:"name" }} Type diff --git a/netbox/templates/dcim/rack.html b/netbox/templates/dcim/rack.html index 8b4034bea..2f1efad75 100644 --- a/netbox/templates/dcim/rack.html +++ b/netbox/templates/dcim/rack.html @@ -66,7 +66,7 @@ {% if object.tenant %} {% if object.tenant.group %} - {{ object.tenant.group|linkify }}/ + {{ object.tenant.group|linkify }} / {% endif %} {{ object|linkify }} {% else %} @@ -206,9 +206,7 @@ {% for resv in reservations %} - - {{ resv.unit_list }} - + {{ resv|linkify:"unit_list" }} {{ resv.tenant|linkify|placeholder }} {{ resv.description }}
diff --git a/netbox/templates/extras/object_configcontext.html b/netbox/templates/extras/object_configcontext.html index 7edeeed27..ab730410e 100644 --- a/netbox/templates/extras/object_configcontext.html +++ b/netbox/templates/extras/object_configcontext.html @@ -43,7 +43,7 @@
{{ context.weight }}
- {{ context.name }} + {{ context|linkify:"name" }}"> {% if context.description %}
{{ context.description }} {% endif %} diff --git a/netbox/templates/extras/objectchange.html b/netbox/templates/extras/objectchange.html index 70fe19304..c66f675bc 100644 --- a/netbox/templates/extras/objectchange.html +++ b/netbox/templates/extras/objectchange.html @@ -61,7 +61,7 @@ Object {% if object.changed_object and object.changed_object.get_absolute_url %} - {{ object.changed_object }} + {{ object.changed_object|linkify }} {% else %} {{ object.object_repr }} {% endif %} diff --git a/netbox/templates/ipam/inc/panels/fhrp_groups.html b/netbox/templates/ipam/inc/panels/fhrp_groups.html index 63397a6f5..f83e6a4f3 100644 --- a/netbox/templates/ipam/inc/panels/fhrp_groups.html +++ b/netbox/templates/ipam/inc/panels/fhrp_groups.html @@ -16,21 +16,14 @@ {% for assignment in object.fhrp_group_assignments.all %} - - {{ assignment.group.group_id }} - - - {{ assignment.group.get_protocol_display }} - + {{ assignment.group|linkify:"group_id" }} + {{ assignment.group.get_protocol_display }} {% for ipaddress in assignment.group.ip_addresses.all %} - {{ ipaddress|linkify }} - {% if not forloop.last %}
{% endif %} + {{ ipaddress|linkify }}{% if not forloop.last %}
{% endif %} {% endfor %} - - {{ assignment.priority }} - + {{ assignment.priority }} {% if perms.ipam.change_fhrpgroupassignment %} diff --git a/netbox/templates/ipam/inc/service.html b/netbox/templates/ipam/inc/service.html index 2670b6fa5..0d6faf1dc 100644 --- a/netbox/templates/ipam/inc/service.html +++ b/netbox/templates/ipam/inc/service.html @@ -1,5 +1,5 @@ - {{ service.name }} + {{ service|linkify:"name" }} {{ service.get_protocol_display }} {{ service.port_list }} diff --git a/netbox/utilities/templatetags/builtins/filters.py b/netbox/utilities/templatetags/builtins/filters.py index eba0daa0e..4a3db0a3c 100644 --- a/netbox/utilities/templatetags/builtins/filters.py +++ b/netbox/utilities/templatetags/builtins/filters.py @@ -21,16 +21,24 @@ register = template.Library() # @register.filter() -def linkify(instance): +def linkify(instance, attr=None): """ - Render a hyperlink for object's with a `get_absolute_url()` method, using the object's string representation - as the link's text. If the object has no `get_absolute_url()` method, return an empty string. + Render a hyperlink for an object with a `get_absolute_url()` method, optionally specifying the name of an + attribute to use for the link text. If no attribute is given, the object's string representation will be + used. + + If the object has no `get_absolute_url()` method, return the text without a hyperlink element. """ - try: - return mark_safe(f'{instance}') - except (AttributeError, TypeError): + if instance is None: return '' + text = getattr(instance, attr) if attr is not None else str(instance) + try: + url = instance.get_absolute_url() + return mark_safe(f'{text}') + except (AttributeError, TypeError): + return text + @register.filter() def bettertitle(value):