1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Closes #9647: Introduce customfield_value template tag

This commit is contained in:
jeremystretch
2022-07-01 14:45:22 -04:00
parent c11af40a06
commit a57398b0d6
5 changed files with 46 additions and 27 deletions

View File

@ -215,6 +215,8 @@ The following custom template tags are available in NetBox.
::: utilities.templatetags.builtins.tags.checkmark
::: utilities.templatetags.builtins.tags.customfield_value
::: utilities.templatetags.builtins.tags.tag
## Filters

View File

@ -40,6 +40,7 @@
* [#9075](https://github.com/netbox-community/netbox/issues/9075) - Introduce `AbortRequest` exception for cleanly interrupting object mutations
* [#9092](https://github.com/netbox-community/netbox/issues/9092) - Add support for `ObjectChildrenView` generic view
* [#9414](https://github.com/netbox-community/netbox/issues/9414) - Add `clone()` method to NetBoxModel for copying instance attributes
* [#9647](https://github.com/netbox-community/netbox/issues/9647) - Introduce `customfield_value` template tag
### Other Changes

View File

@ -16,33 +16,7 @@
<span title="{{ field.description|escape }}">{{ field }}</span>
</td>
<td>
{% if field.type == 'integer' and value is not None %}
{{ value }}
{% elif field.type == 'longtext' and value %}
{{ value|markdown }}
{% elif field.type == 'boolean' and value == True %}
{% checkmark value true="True" %}
{% elif field.type == 'boolean' and value == False %}
{% checkmark value false="False" %}
{% elif field.type == 'url' and value %}
<a href="{{ value }}">{{ value|truncatechars:70 }}</a>
{% elif field.type == 'json' and value %}
<pre>{{ value|json }}</pre>
{% elif field.type == 'multiselect' and value %}
{{ value|join:", " }}
{% elif field.type == 'object' and value %}
{{ value|linkify }}
{% elif field.type == 'multiobject' and value %}
{% for obj in value %}
{{ obj|linkify }}{% if not forloop.last %}<br />{% endif %}
{% endfor %}
{% elif value %}
{{ value }}
{% elif field.required %}
<span class="text-warning"><i class="mdi mdi-alert"></i> Not defined</span>
{% else %}
{{ ''|placeholder }}
{% endif %}
{% customfield_value field value %}
</td>
</tr>
{% endfor %}

View File

@ -0,0 +1,27 @@
{% if field.type == 'integer' and value is not None %}
{{ value }}
{% elif field.type == 'longtext' and value %}
{{ value|markdown }}
{% elif field.type == 'boolean' and value == True %}
{% checkmark value true="True" %}
{% elif field.type == 'boolean' and value == False %}
{% checkmark value false="False" %}
{% elif field.type == 'url' and value %}
<a href="{{ value }}">{{ value|truncatechars:70 }}</a>
{% elif field.type == 'json' and value %}
<pre>{{ value|json }}</pre>
{% elif field.type == 'multiselect' and value %}
{{ value|join:", " }}
{% elif field.type == 'object' and value %}
{{ value|linkify }}
{% elif field.type == 'multiobject' and value %}
{% for object in value %}
{{ object|linkify }}{% if not forloop.last %}<br />{% endif %}
{% endfor %}
{% elif value %}
{{ value }}
{% elif field.required %}
<span class="text-warning"><i class="mdi mdi-alert"></i> Not defined</span>
{% else %}
{{ ''|placeholder }}
{% endif %}

View File

@ -18,6 +18,21 @@ def tag(value, viewname=None):
}
@register.inclusion_tag('builtins/customfield_value.html')
def customfield_value(customfield, value):
"""
Render a custom field value according to the field type.
Args:
customfield: A CustomField instance
value: The custom field value applied to an object
"""
return {
'customfield': customfield,
'value': value,
}
@register.inclusion_tag('builtins/badge.html')
def badge(value, bg_color=None, show_empty=False):
"""