Преглед изворни кода

Extend linkify() to accept an attr name for the link text

jeremystretch пре 3 година
родитељ
комит
8d682041a4

+ 1 - 3
netbox/templates/dcim/consoleport.html

@@ -79,9 +79,7 @@
                                 </tr>
                                 </tr>
                                 <tr>
                                 <tr>
                                     <th scope="row">Name</th>
                                     <th scope="row">Name</th>
-                                    <td>
-                                        <a href="{{ object.connected_endpoint.get_absolute_url }}">{{ object.connected_endpoint.name }}</a>
-                                    </td>
+                                    <td>{{ object.connected_endpoint|linkify:"name" }}</td>
                                 </tr>
                                 </tr>
                                 <tr>
                                 <tr>
                                     <th scope="row">Type</th>
                                     <th scope="row">Type</th>

+ 1 - 3
netbox/templates/dcim/consoleserverport.html

@@ -81,9 +81,7 @@
                             </tr>
                             </tr>
                             <tr>
                             <tr>
                                 <th scope="row">Name</th>
                                 <th scope="row">Name</th>
-                                <td>
-                                    <a href="{{ object.connected_endpoint.get_absolute_url }}">{{ object.connected_endpoint.name }}</a>
-                                </td>
+                                <td>{{ object.connected_endpoint|linkify:"name" }}</td>
                             </tr>
                             </tr>
                             <tr>
                             <tr>
                                 <th scope="row">Type</th>
                                 <th scope="row">Type</th>

+ 2 - 6
netbox/templates/dcim/inc/interface_vlans_table.html

@@ -8,9 +8,7 @@
     {% with tagged_vlans=obj.tagged_vlans.all %}
     {% with tagged_vlans=obj.tagged_vlans.all %}
         {% if obj.untagged_vlan and obj.untagged_vlan not in tagged_vlans %}
         {% if obj.untagged_vlan and obj.untagged_vlan not in tagged_vlans %}
             <tr>
             <tr>
-                <td>
-                    <a href="{{ obj.untagged_vlan.get_absolute_url }}">{{ obj.untagged_vlan.vid }}</a>
-                </td>
+                <td>{{ obj.untagged_vlan|linkify:"vid" }}</td>
                 <td>{{ obj.untagged_vlan.name }}</td>
                 <td>{{ obj.untagged_vlan.name }}</td>
                 <td>
                 <td>
                     <input type="radio" name="untagged_vlan" value="{{ obj.untagged_vlan.pk }}" checked="checked" />
                     <input type="radio" name="untagged_vlan" value="{{ obj.untagged_vlan.pk }}" checked="checked" />
@@ -22,9 +20,7 @@
         {% endif %}
         {% endif %}
         {% for vlan in tagged_vlans %}
         {% for vlan in tagged_vlans %}
             <tr>
             <tr>
-                <td>
-                    <a href="{{ vlan.get_absolute_url }}">{{ vlan.vid }}</a>
-                </td>
+                <td>{{ vlan|linkify:"vid" }}</td>
                 <td>{{ vlan.name }}</td>
                 <td>{{ vlan.name }}</td>
                 <td>
                 <td>
                     <input type="radio" name="untagged_vlan" value="{{ vlan.pk }}"{% if vlan == obj.untagged_vlan %} checked="checked"{% endif %} />
                     <input type="radio" name="untagged_vlan" value="{{ vlan.pk }}"{% if vlan == obj.untagged_vlan %} checked="checked"{% endif %} />

+ 1 - 1
netbox/templates/dcim/inc/panels/inventory_items.html

@@ -15,7 +15,7 @@
       <tbody>
       <tbody>
         {% for item in object.inventory_items.all %}
         {% for item in object.inventory_items.all %}
           <tr>
           <tr>
-            <td><a href="{{ item.get_absolute_url }}">{{ item.name }}</a></td>
+            <td>{{ item|linkify:"name" }}</td>
             <td>{{ item.label|placeholder }}</td>
             <td>{{ item.label|placeholder }}</td>
             <td>{{ item.role|linkify|placeholder }}</td>
             <td>{{ item.role|linkify|placeholder }}</td>
             <td class="text-end noprint">
             <td class="text-end noprint">

+ 2 - 6
netbox/templates/dcim/interface.html

@@ -152,9 +152,7 @@
                                     </tr>
                                     </tr>
                                     <tr>
                                     <tr>
                                         <th scope="row">Name</th>
                                         <th scope="row">Name</th>
-                                        <td>
-                                            <a href="{{ iface.get_absolute_url }}">{{ iface.name }}</a>
-                                        </td>
+                                        <td>{{ iface|linkify:"name" }}</td>
                                     </tr>
                                     </tr>
                                     <tr>
                                     <tr>
                                         <th scope="row">Type</th>
                                         <th scope="row">Type</th>
@@ -370,9 +368,7 @@
                                               &mdash;
                                               &mdash;
                                             {% endif %}
                                             {% endif %}
                                         </td>
                                         </td>
-                                        <td>
-                                            <a href="{{ wlan.get_absolute_url }}">{{ wlan.ssid }}</a>
-                                        </td>
+                                        <td>{{ wlan|linkify:"ssid" }}</td>
                                     </tr>
                                     </tr>
                                 {% empty %}
                                 {% empty %}
                                     <tr>
                                     <tr>

+ 1 - 3
netbox/templates/dcim/powerfeed.html

@@ -127,9 +127,7 @@
                         </tr>
                         </tr>
                         <tr>
                         <tr>
                             <th scope="row">Name</th>
                             <th scope="row">Name</th>
-                            <td>
-                                <a href="{{ object.connected_endpoint.get_absolute_url }}">{{ object.connected_endpoint.name }}</a>
-                            </td>
+                            <td>{{ object.connected_endpoint|linkify:"name" }}</td>
                         </tr>
                         </tr>
                         <tr>
                         <tr>
                             <th scope="row">Type</th>
                             <th scope="row">Type</th>

+ 1 - 3
netbox/templates/dcim/poweroutlet.html

@@ -85,9 +85,7 @@
                             </tr>
                             </tr>
                             <tr>
                             <tr>
                                 <th scope="row">Name</th>
                                 <th scope="row">Name</th>
-                                <td>
-                                    <a href="{{ object.connected_endpoint.get_absolute_url }}">{{ object.connected_endpoint.name }}</a>
-                                </td>
+                                <td>{{ object.connected_endpoint|linkify:"name" }}</td>
                             </tr>
                             </tr>
                             <tr>
                             <tr>
                                 <th scope="row">Type</th>
                                 <th scope="row">Type</th>

+ 1 - 3
netbox/templates/dcim/powerport.html

@@ -85,9 +85,7 @@
                             </tr>
                             </tr>
                             <tr>
                             <tr>
                                 <th scope="row">Name</th>
                                 <th scope="row">Name</th>
-                                <td>
-                                    <a href="{{ object.connected_endpoint.get_absolute_url }}">{{ object.connected_endpoint.name }}</a>
-                                </td>
+                                <td>{{ object.connected_endpoint|linkify:"name" }}</td>
                             </tr>
                             </tr>
                             <tr>
                             <tr>
                                 <th scope="row">Type</th>
                                 <th scope="row">Type</th>

+ 2 - 4
netbox/templates/dcim/rack.html

@@ -66,7 +66,7 @@
                         <td>
                         <td>
                             {% if object.tenant %}
                             {% if object.tenant %}
                                 {% if object.tenant.group %}
                                 {% if object.tenant.group %}
-                                    {{ object.tenant.group|linkify }}/
+                                    {{ object.tenant.group|linkify }} /
                                 {% endif %}
                                 {% endif %}
                                 {{ object|linkify }}
                                 {{ object|linkify }}
                             {% else %}
                             {% else %}
@@ -206,9 +206,7 @@
                     </tr>
                     </tr>
                     {% for resv in reservations %}
                     {% for resv in reservations %}
                         <tr>
                         <tr>
-                            <td>
-                                <a href="{{ resv.get_absolute_url }}">{{ resv.unit_list }}</a>
-                            </td>
+                            <td>{{ resv|linkify:"unit_list" }}</td>
                             <td>{{ resv.tenant|linkify|placeholder }}</td>
                             <td>{{ resv.tenant|linkify|placeholder }}</td>
                             <td>
                             <td>
                                 {{ resv.description }}<br />
                                 {{ resv.description }}<br />

+ 1 - 1
netbox/templates/extras/object_configcontext.html

@@ -43,7 +43,7 @@
                         <div class="float-end">
                         <div class="float-end">
                             <span class="text-muted">{{ context.weight }}</span>
                             <span class="text-muted">{{ context.weight }}</span>
                         </div>
                         </div>
-                        <a href="{{ context.get_absolute_url }}"><strong>{{ context.name }}</strong></a>
+                        <strong>{{ context|linkify:"name" }}"></strong>
                         {% if context.description %}
                         {% if context.description %}
                             <br /><small>{{ context.description }}</small>
                             <br /><small>{{ context.description }}</small>
                         {% endif %}
                         {% endif %}

+ 1 - 1
netbox/templates/extras/objectchange.html

@@ -61,7 +61,7 @@
                         <th scope="row">Object</th>
                         <th scope="row">Object</th>
                         <td>
                         <td>
                             {% if object.changed_object and object.changed_object.get_absolute_url %}
                             {% if object.changed_object and object.changed_object.get_absolute_url %}
-                                <a href="{{ object.changed_object.get_absolute_url }}">{{ object.changed_object }}</a>
+                                {{ object.changed_object|linkify }}
                             {% else %}
                             {% else %}
                                 {{ object.object_repr }}
                                 {{ object.object_repr }}
                             {% endif %}
                             {% endif %}

+ 4 - 11
netbox/templates/ipam/inc/panels/fhrp_groups.html

@@ -16,21 +16,14 @@
       <tbody>
       <tbody>
         {% for assignment in object.fhrp_group_assignments.all %}
         {% for assignment in object.fhrp_group_assignments.all %}
           <tr>
           <tr>
-            <td>
-              <a href="{{ assignment.group.get_absolute_url }}">{{ assignment.group.group_id }}</a>
-            </td>
-            <td>
-              {{ assignment.group.get_protocol_display }}
-            </td>
+            <td>{{ assignment.group|linkify:"group_id" }}</td>
+            <td>{{ assignment.group.get_protocol_display }}</td>
             <td>
             <td>
               {% for ipaddress in assignment.group.ip_addresses.all %}
               {% for ipaddress in assignment.group.ip_addresses.all %}
-                {{ ipaddress|linkify }}
-                {% if not forloop.last %}<br />{% endif %}
+                {{ ipaddress|linkify }}{% if not forloop.last %}<br />{% endif %}
               {% endfor %}
               {% endfor %}
             </td>
             </td>
-            <td>
-              {{ assignment.priority }}
-            </td>
+            <td>{{ assignment.priority }}</td>
             <td class="text-end noprint">
             <td class="text-end noprint">
               {% if perms.ipam.change_fhrpgroupassignment %}
               {% if perms.ipam.change_fhrpgroupassignment %}
                 <a href="{% url 'ipam:fhrpgroupassignment_edit' pk=assignment.pk %}?return_url={{ object.get_absolute_url }}" class="btn btn-warning btn-sm lh-1" title="Edit">
                 <a href="{% url 'ipam:fhrpgroupassignment_edit' pk=assignment.pk %}?return_url={{ object.get_absolute_url }}" class="btn btn-warning btn-sm lh-1" title="Edit">

+ 1 - 1
netbox/templates/ipam/inc/service.html

@@ -1,5 +1,5 @@
 <tr>
 <tr>
-    <td><a href="{{ service.get_absolute_url }}">{{ service.name }}</a></td>
+    <td>{{ service|linkify:"name" }}</td>
     <td>{{ service.get_protocol_display }}</td>
     <td>{{ service.get_protocol_display }}</td>
     <td>{{ service.port_list }}</td>
     <td>{{ service.port_list }}</td>
     <td>
     <td>

+ 13 - 5
netbox/utilities/templatetags/builtins/filters.py

@@ -21,15 +21,23 @@ register = template.Library()
 #
 #
 
 
 @register.filter()
 @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.
     """
     """
+    if instance is None:
+        return ''
+
+    text = getattr(instance, attr) if attr is not None else str(instance)
     try:
     try:
-        return mark_safe(f'<a href="{instance.get_absolute_url()}">{instance}</a>')
+        url = instance.get_absolute_url()
+        return mark_safe(f'<a href="{url}">{text}</a>')
     except (AttributeError, TypeError):
     except (AttributeError, TypeError):
-        return ''
+        return text
 
 
 
 
 @register.filter()
 @register.filter()