mirror of
				https://github.com/netbox-community/netbox.git
				synced 2024-05-10 07:54:54 +00:00 
			
		
		
		
	Extend linkify() to accept an attr name for the link text
This commit is contained in:
		@@ -79,9 +79,7 @@
 | 
			
		||||
                                </tr>
 | 
			
		||||
                                <tr>
 | 
			
		||||
                                    <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>
 | 
			
		||||
                                    <th scope="row">Type</th>
 | 
			
		||||
 
 | 
			
		||||
@@ -81,9 +81,7 @@
 | 
			
		||||
                            </tr>
 | 
			
		||||
                            <tr>
 | 
			
		||||
                                <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>
 | 
			
		||||
                                <th scope="row">Type</th>
 | 
			
		||||
 
 | 
			
		||||
@@ -8,9 +8,7 @@
 | 
			
		||||
    {% with tagged_vlans=obj.tagged_vlans.all %}
 | 
			
		||||
        {% if obj.untagged_vlan and obj.untagged_vlan not in tagged_vlans %}
 | 
			
		||||
            <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>
 | 
			
		||||
                    <input type="radio" name="untagged_vlan" value="{{ obj.untagged_vlan.pk }}" checked="checked" />
 | 
			
		||||
@@ -22,9 +20,7 @@
 | 
			
		||||
        {% endif %}
 | 
			
		||||
        {% for vlan in tagged_vlans %}
 | 
			
		||||
            <tr>
 | 
			
		||||
                <td>
 | 
			
		||||
                    <a href="{{ vlan.get_absolute_url }}">{{ vlan.vid }}</a>
 | 
			
		||||
                </td>
 | 
			
		||||
                <td>{{ vlan|linkify:"vid" }}</td>
 | 
			
		||||
                <td>{{ vlan.name }}</td>
 | 
			
		||||
                <td>
 | 
			
		||||
                    <input type="radio" name="untagged_vlan" value="{{ vlan.pk }}"{% if vlan == obj.untagged_vlan %} checked="checked"{% endif %} />
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@
 | 
			
		||||
      <tbody>
 | 
			
		||||
        {% for item in object.inventory_items.all %}
 | 
			
		||||
          <tr>
 | 
			
		||||
            <td><a href="{{ item.get_absolute_url }}">{{ item.name }}</a></td>
 | 
			
		||||
            <td>{{ item|linkify:"name" }}</td>
 | 
			
		||||
            <td>{{ item.label|placeholder }}</td>
 | 
			
		||||
            <td>{{ item.role|linkify|placeholder }}</td>
 | 
			
		||||
            <td class="text-end noprint">
 | 
			
		||||
 
 | 
			
		||||
@@ -152,9 +152,7 @@
 | 
			
		||||
                                    </tr>
 | 
			
		||||
                                    <tr>
 | 
			
		||||
                                        <th scope="row">Name</th>
 | 
			
		||||
                                        <td>
 | 
			
		||||
                                            <a href="{{ iface.get_absolute_url }}">{{ iface.name }}</a>
 | 
			
		||||
                                        </td>
 | 
			
		||||
                                        <td>{{ iface|linkify:"name" }}</td>
 | 
			
		||||
                                    </tr>
 | 
			
		||||
                                    <tr>
 | 
			
		||||
                                        <th scope="row">Type</th>
 | 
			
		||||
@@ -370,9 +368,7 @@
 | 
			
		||||
                                              —
 | 
			
		||||
                                            {% endif %}
 | 
			
		||||
                                        </td>
 | 
			
		||||
                                        <td>
 | 
			
		||||
                                            <a href="{{ wlan.get_absolute_url }}">{{ wlan.ssid }}</a>
 | 
			
		||||
                                        </td>
 | 
			
		||||
                                        <td>{{ wlan|linkify:"ssid" }}</td>
 | 
			
		||||
                                    </tr>
 | 
			
		||||
                                {% empty %}
 | 
			
		||||
                                    <tr>
 | 
			
		||||
 
 | 
			
		||||
@@ -127,9 +127,7 @@
 | 
			
		||||
                        </tr>
 | 
			
		||||
                        <tr>
 | 
			
		||||
                            <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>
 | 
			
		||||
                            <th scope="row">Type</th>
 | 
			
		||||
 
 | 
			
		||||
@@ -85,9 +85,7 @@
 | 
			
		||||
                            </tr>
 | 
			
		||||
                            <tr>
 | 
			
		||||
                                <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>
 | 
			
		||||
                                <th scope="row">Type</th>
 | 
			
		||||
 
 | 
			
		||||
@@ -85,9 +85,7 @@
 | 
			
		||||
                            </tr>
 | 
			
		||||
                            <tr>
 | 
			
		||||
                                <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>
 | 
			
		||||
                                <th scope="row">Type</th>
 | 
			
		||||
 
 | 
			
		||||
@@ -66,7 +66,7 @@
 | 
			
		||||
                        <td>
 | 
			
		||||
                            {% if object.tenant %}
 | 
			
		||||
                                {% if object.tenant.group %}
 | 
			
		||||
                                    {{ object.tenant.group|linkify }}/
 | 
			
		||||
                                    {{ object.tenant.group|linkify }} /
 | 
			
		||||
                                {% endif %}
 | 
			
		||||
                                {{ object|linkify }}
 | 
			
		||||
                            {% else %}
 | 
			
		||||
@@ -206,9 +206,7 @@
 | 
			
		||||
                    </tr>
 | 
			
		||||
                    {% for resv in reservations %}
 | 
			
		||||
                        <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.description }}<br />
 | 
			
		||||
 
 | 
			
		||||
@@ -43,7 +43,7 @@
 | 
			
		||||
                        <div class="float-end">
 | 
			
		||||
                            <span class="text-muted">{{ context.weight }}</span>
 | 
			
		||||
                        </div>
 | 
			
		||||
                        <a href="{{ context.get_absolute_url }}"><strong>{{ context.name }}</strong></a>
 | 
			
		||||
                        <strong>{{ context|linkify:"name" }}"></strong>
 | 
			
		||||
                        {% if context.description %}
 | 
			
		||||
                            <br /><small>{{ context.description }}</small>
 | 
			
		||||
                        {% endif %}
 | 
			
		||||
 
 | 
			
		||||
@@ -61,7 +61,7 @@
 | 
			
		||||
                        <th scope="row">Object</th>
 | 
			
		||||
                        <td>
 | 
			
		||||
                            {% 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 %}
 | 
			
		||||
                                {{ object.object_repr }}
 | 
			
		||||
                            {% endif %}
 | 
			
		||||
 
 | 
			
		||||
@@ -16,21 +16,14 @@
 | 
			
		||||
      <tbody>
 | 
			
		||||
        {% for assignment in object.fhrp_group_assignments.all %}
 | 
			
		||||
          <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>
 | 
			
		||||
              {% 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 %}
 | 
			
		||||
            </td>
 | 
			
		||||
            <td>
 | 
			
		||||
              {{ assignment.priority }}
 | 
			
		||||
            </td>
 | 
			
		||||
            <td>{{ assignment.priority }}</td>
 | 
			
		||||
            <td class="text-end noprint">
 | 
			
		||||
              {% 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">
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
<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.port_list }}</td>
 | 
			
		||||
    <td>
 | 
			
		||||
 
 | 
			
		||||
@@ -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'<a href="{instance.get_absolute_url()}">{instance}</a>')
 | 
			
		||||
    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'<a href="{url}">{text}</a>')
 | 
			
		||||
    except (AttributeError, TypeError):
 | 
			
		||||
        return text
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@register.filter()
 | 
			
		||||
def bettertitle(value):
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user