Introduce the isodate(), isotime(), and isodatetime() template filters

This commit is contained in:
Jeremy Stretch 2024-04-16 15:09:02 -04:00
parent 4e4c277711
commit 510f1326e6
19 changed files with 105 additions and 37 deletions

View File

@ -31,11 +31,19 @@
</tr>
<tr>
<th scope="row">{% trans "Account Created" %}</th>
<td>{{ request.user.date_joined|annotated_date }}</td>
<td>{{ request.user.date_joined|isodate }}</td>
</tr>
<tr>
<th scope="row">{% trans "Last Login" %}</th>
<td>{{ request.user.last_login|annotated_date }}</td>
<td>
{% if request.user.last_login %}
<span title="{{ object.last_login|isodatetime:"minutes" }}">
{{ request.user.last_login|timesince }} {% trans "ago" %}
</span>
{% else %}
{{ ''|placeholder }}
{% endif %}
</td>
</tr>
<tr>
<th scope="row">{% trans "Superuser" %}</th>

View File

@ -41,15 +41,24 @@
</tr>
<tr>
<th scope="row">{% trans "Created" %}</th>
<td>{{ object.created|annotated_date }}</td>
<td>{{ object.created|isodatetime }}</td>
</tr>
<tr>
<th scope="row">{% trans "Expires" %}</th>
<td>{{ object.expires|placeholder }}</td>
<td>{{ object.expires|isodatetime|placeholder }}</td>
</tr>
<tr>
<th scope="row">{% trans "Last used" %}</th>
<td>{{ object.last_used|placeholder }}</td>
<td>
{% if object.last_used %}
<span title="{{ object.last_used|isodatetime }}">
{{ object.last_used|timesince }}
</span>
{% trans "ago" %}
{% else %}
{{ ''|placeholder }}
{% endif %}
</td>
</tr>
<tr>
<th scope="row">{% trans "Allowed IPs" %}</th>

View File

@ -45,11 +45,11 @@
</tr>
<tr>
<th scope="row">{% trans "Install Date" %}</th>
<td>{{ object.install_date|annotated_date|placeholder }}</td>
<td>{{ object.install_date|isodate|placeholder }}</td>
</tr>
<tr>
<th scope="row">{% trans "Termination Date" %}</th>
<td>{{ object.termination_date|annotated_date|placeholder }}</td>
<td>{{ object.termination_date|isodate|placeholder }}</td>
</tr>
<tr>
<th scope="row">{% trans "Commit Rate" %}</th>

View File

@ -23,7 +23,7 @@
{% block subtitle %}
{% if object.created %}
<div class="text-secondary fs-5">
{% trans "Created" %} {{ object.created|annotated_date }}
{% trans "Created" %} {{ object.created|isodatetime }}
</div>
{% endif %}
{% endblock subtitle %}

View File

@ -9,7 +9,7 @@
{% block subtitle %}
<div class="text-secondary fs-5">
{% trans "Created" %} {{ object.created|annotated_date }}
{% trans "Created" %} {{ object.created|isodatetime }}
</div>
{% endblock %}

View File

@ -49,12 +49,12 @@
<table class="table table-hover attr-table">
<tr>
<th scope="row">{% trans "Created" %}</th>
<td>{{ object.created|annotated_date }}</td>
<td>{{ object.created|isodatetime }}</td>
</tr>
<tr>
<th scope="row">{% trans "Scheduled" %}</th>
<td>
{{ object.scheduled|annotated_date|placeholder }}
{{ object.scheduled|isodatetime|placeholder }}
{% if object.interval %}
({% blocktrans with interval=object.interval %}every {{ interval }} minutes{% endblocktrans %})
{% endif %}
@ -62,11 +62,11 @@
</tr>
<tr>
<th scope="row">{% trans "Started" %}</th>
<td>{{ object.started|annotated_date|placeholder }}</td>
<td>{{ object.started|isodatetime|placeholder }}</td>
</tr>
<tr>
<th scope="row">{% trans "Completed" %}</th>
<td>{{ object.completed|annotated_date|placeholder }}</td>
<td>{{ object.completed|isodatetime|placeholder }}</td>
</tr>
</table>
</div>

View File

@ -13,7 +13,7 @@
{% block subtitle %}
<div class="text-secondary fs-5">
<span>{% trans "Created" %} {{ job.created_at|annotated_date }}</span>
<span>{% trans "Created" %} {{ job.created_at|isodatetime }}</span>
</div>
{% endblock subtitle %}
@ -71,11 +71,11 @@
</tr>
<tr>
<th scope="row">{% trans "Created" %}</th>
<td>{{ job.created_at|annotated_date }}</td>
<td>{{ job.created_at|isodatetime }}</td>
</tr>
<tr>
<th scope="row">{% trans "Queued" %}</th>
<td>{{ job.enqueued_at|annotated_date }}</td>
<td>{{ job.enqueued_at|isodatetime }}</td>
</tr>
<tr>
<th scope="row">{% trans "Status" %}</th>

View File

@ -11,7 +11,7 @@
{% block subtitle %}
<div class="text-secondary fs-5">
<span>{% trans "Created" %} {{ worker.birth_date|annotated_date }}</span>
<span>{% trans "Created" %} {{ worker.birth_date|isodatetime }}</span>
</div>
{% endblock subtitle %}
@ -49,7 +49,7 @@
</tr>
<tr>
<th scope="row">{% trans "Birth" %}</th>
<td>{{ worker.birth_date|annotated_date }}</td>
<td>{{ worker.birth_date|isodatetime }}</td>
</tr>
<tr>
<th scope="row">{% trans "Queues" %}</th>

View File

@ -6,11 +6,11 @@
<div class="htmx-container">
<p>
{% if job.started %}
{% trans "Started" %}: <strong>{{ job.started|annotated_date }}</strong>
{% trans "Started" %}: <strong>{{ job.started|isodatetime }}</strong>
{% elif job.scheduled %}
{% trans "Scheduled for" %}: <strong>{{ job.scheduled|annotated_date }}</strong> ({{ job.scheduled|naturaltime }})
{% trans "Scheduled for" %}: <strong>{{ job.scheduled|isodatetime }}</strong> ({{ job.scheduled|naturaltime }})
{% else %}
{% trans "Created" %}: <strong>{{ job.created|annotated_date }}</strong>
{% trans "Created" %}: <strong>{{ job.created|isodatetime }}</strong>
{% endif %}
{% if job.completed %}
{% trans "Duration" %}: <strong>{{ job.duration }}</strong>

View File

@ -20,7 +20,7 @@
</tr>
<tr>
<th scope="row">{% trans "Created" %}</th>
<td>{{ object.created|annotated_date }}</td>
<td>{{ object.created|isodatetime:"minutes" }}</td>
</tr>
<tr>
<th scope="row">{% trans "Created By" %}</th>

View File

@ -29,9 +29,7 @@
<table class="table table-hover attr-table">
<tr>
<th scope="row">{% trans "Time" %}</th>
<td>
{{ object.time|annotated_date }}
</td>
<td>{{ object.time|isodatetime }}</td>
</tr>
<tr>
<th scope="row">{% trans "User" %}</th>

View File

@ -67,7 +67,7 @@
<td>{{ script.description|markdown|placeholder }}</td>
{% if last_job %}
<td>
<a href="{% url 'extras:script_result' job_pk=last_job.pk %}">{{ last_job.created|annotated_date }}</a>
<a href="{% url 'extras:script_result' job_pk=last_job.pk %}">{{ last_job.created|isodatetime }}</a>
</td>
<td>
{% badge last_job.get_status_display last_job.get_status_color %}

View File

@ -17,7 +17,7 @@
<li class="breadcrumb-item"><a href="{% url 'extras:script_list' %}">{% trans "Scripts" %}</a></li>
<li class="breadcrumb-item"><a href="{% url 'extras:script_list' %}#module.{{ script.module }}">{{ script.module|bettertitle }}</a></li>
<li class="breadcrumb-item"><a href="{{ script.get_absolute_url }}">{{ script }}</a></li>
<li class="breadcrumb-item">{{ job.created|annotated_date }}</li>
<li class="breadcrumb-item">{{ job.created|isodatetime }}</li>
</ol>
</nav>
</div>

View File

@ -48,10 +48,14 @@ Context:
{% block subtitle %}
<div class="text-secondary fs-5">
<span>{% trans "Created" %} {{ object.created|annotated_date }}</span>
{% trans "Created" %} {{ object.created|isodatetime:"minutes" }}
{% if object.last_updated %}
<span class="separator">&middot;</span>
<span>{% trans "Updated" %} <span title="{{ object.last_updated }}">{{ object.last_updated|timesince }}</span> {% trans "ago" %}</span>
{% trans "Updated" %}
<span title="{{ object.last_updated|isodatetime:"minutes" }}">
{{ object.last_updated|timesince }}
</span>
{% trans "ago" %}
{% endif %}
</div>
{% endblock subtitle %}

View File

@ -37,7 +37,7 @@
</tr>
<tr>
<th scope="row">{% trans "Date Added" %}</th>
<td>{{ object.date_added|annotated_date|placeholder }}</td>
<td>{{ object.date_added|isodate|placeholder }}</td>
</tr>
<tr>
<th scope="row">{% trans "Description" %}</th>

View File

@ -33,15 +33,24 @@
</tr>
<tr>
<th scope="row">{% trans "Created" %}</th>
<td>{{ object.created|annotated_date }}</td>
<td>{{ object.created|isodatetime }}</td>
</tr>
<tr>
<th scope="row">{% trans "Expires" %}</th>
<td>{{ object.expires|placeholder }}</td>
<td>{{ object.expires|isodatetime }}</td>
</tr>
<tr>
<th scope="row">{% trans "Last used" %}</th>
<td>{{ object.last_used|placeholder }}</td>
<td>
{% if object.last_used %}
<span title="{{ object.last_used|isodatetime }}">
{{ object.last_used|timesince }}
</span>
{% trans "ago" %}
{% else %}
{{ ''|placeholder }}
{% endif %}
</td>
</tr>
<tr>
<th scope="row">{% trans "Allowed IPs" %}</th>

View File

@ -27,11 +27,19 @@
</tr>
<tr>
<th scope="row">{% trans "Account Created" %}</th>
<td>{{ object.date_joined|annotated_date }}</td>
<td>{{ object.date_joined|isodate }}</td>
</tr>
<tr>
<th scope="row">{% trans "Last Login" %}</th>
<td>{{ object.last_login|annotated_date }}</td>
<td>
{% if object.last_login %}
<span title="{{ object.last_login|isodatetime:"minutes" }}">
{{ object.last_login|timesince }} {% trans "ago" %}
</span>
{% else %}
{{ ''|placeholder }}
{% endif %}
</td>
</tr>
<tr>
<th scope="row">{% trans "Active" %}</th>

View File

@ -9,9 +9,9 @@
{% elif customfield.type == 'boolean' and value == False %}
{% checkmark value false="False" %}
{% elif customfield.type == 'date' and value %}
{{ value|annotated_date }}
{{ value|isodate }}
{% elif customfield.type == 'datetime' and value %}
{{ value|annotated_date }}
{{ value|isodate }} {{ value|isodatetime }}
{% elif customfield.type == 'url' and value %}
<a href="{{ value }}">{{ value|truncatechars:70 }}</a>
{% elif customfield.type == 'json' and value %}

View File

@ -20,6 +20,9 @@ __all__ = (
'content_type',
'content_type_id',
'fgcolor',
'isodate',
'isodatetime',
'isotime',
'linkify',
'meta',
'placeholder',
@ -202,3 +205,32 @@ def render_yaml(value):
{{ data_dict|yaml }}
"""
return yaml.dump(json.loads(json.dumps(value)))
#
# Time & date
#
@register.filter()
def isodate(value):
if type(value) is datetime.date:
return value.isoformat()
if type(value) is datetime.datetime:
return value.date().isoformat()
return ''
@register.filter()
def isotime(value, spec='seconds'):
if type(value) is datetime.time:
return value.isoformat(timespec=spec)
if type(value) is datetime.datetime:
return value.time().isoformat(timespec=spec)
return ''
@register.filter()
def isodatetime(value, spec='seconds'):
if type(value) is datetime.datetime:
return f'{isodate(value)} {isotime(value, spec=spec)}'
return ''