From 0ad9b83623547627201c748ebe6c482440f16022 Mon Sep 17 00:00:00 2001 From: Robin Schneider Date: Sun, 7 Feb 2021 23:54:21 +0100 Subject: [PATCH] Closes #5503: ISO 8601 date in UI and alternative format as tooltip With this commit all dates in the UI are now consistently displayed. I changed the long date format as suggested by @xkilian and confirmed by my own research. * DATETIME_FORMAT * Before July 20, 2020 4:52 p.m. * Now 20th July, 2020 16:52 "20th July, 2020" would be spoken as "the 20th of July, 2020" but the "the" and "of" are never written. The only exception is `object_list.html`. I tried it but there it does not work so easily because the dates are passed to Jinja as SafeString. --- netbox/extras/models/models.py | 5 ++- netbox/templates/base.html | 2 +- netbox/templates/circuits/circuit.html | 2 +- netbox/templates/dcim/rack.html | 2 +- netbox/templates/dcim/site.html | 6 +++- netbox/templates/extras/journalentry.html | 2 +- netbox/templates/extras/objectchange.html | 2 +- netbox/templates/extras/report.html | 2 +- netbox/templates/extras/report_list.html | 2 +- netbox/templates/extras/report_result.html | 2 +- netbox/templates/extras/script_list.html | 2 +- netbox/templates/extras/script_result.html | 4 +-- netbox/templates/generic/object.html | 4 +-- netbox/templates/home.html | 2 +- netbox/templates/inc/custom_fields_panel.html | 3 ++ netbox/templates/inc/image_attachments.html | 3 +- netbox/templates/ipam/aggregate.html | 2 +- netbox/templates/users/api_tokens.html | 4 +-- netbox/templates/users/profile.html | 2 +- netbox/templates/users/userkey.html | 7 ++-- netbox/utilities/templatetags/helpers.py | 36 +++++++++++++++++++ 21 files changed, 71 insertions(+), 25 deletions(-) diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index c2cebe163..6bce039fc 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -431,9 +431,8 @@ class JournalEntry(ChangeLoggedModel): verbose_name_plural = 'journal entries' def __str__(self): - created_date = timezone.localdate(self.created) - created_time = timezone.localtime(self.created) - return f"{date_format(created_date)} - {time_format(created_time)} ({self.get_kind_display()})" + created = timezone.localtime(self.created) + return f"{date_format(created, format='SHORT_DATETIME_FORMAT')} ({self.get_kind_display()})" def get_absolute_url(self): return reverse('extras:journalentry', args=[self.pk]) diff --git a/netbox/templates/base.html b/netbox/templates/base.html index 3a169d965..32ddc8c26 100644 --- a/netbox/templates/base.html +++ b/netbox/templates/base.html @@ -67,7 +67,7 @@

{{ settings.HOSTNAME }} (v{{ settings.VERSION }})

-

{% now 'Y-m-d H:i:s T' %}

+

{% annotated_now %} {% now 'T' %}

diff --git a/netbox/templates/circuits/circuit.html b/netbox/templates/circuits/circuit.html index ab8bd7394..5d57cd8d6 100644 --- a/netbox/templates/circuits/circuit.html +++ b/netbox/templates/circuits/circuit.html @@ -51,7 +51,7 @@ Install Date - {{ object.install_date|placeholder }} + {{ object.install_date|annotated_date|placeholder }} Commit Rate diff --git a/netbox/templates/dcim/rack.html b/netbox/templates/dcim/rack.html index 3bb0084a3..ada93518f 100644 --- a/netbox/templates/dcim/rack.html +++ b/netbox/templates/dcim/rack.html @@ -268,7 +268,7 @@ {{ resv.description }}
- {{ resv.user }} · {{ resv.created }} + {{ resv.user }} · {{ resv.created|annotated_date }} {% if perms.dcim.change_rackreservation %} diff --git a/netbox/templates/dcim/site.html b/netbox/templates/dcim/site.html index e0c707a49..a2ad76d53 100644 --- a/netbox/templates/dcim/site.html +++ b/netbox/templates/dcim/site.html @@ -80,7 +80,11 @@ {% if object.time_zone %} {{ object.time_zone }} (UTC {{ object.time_zone|tzoffset }})
- Site time: {% timezone object.time_zone %}{% now "SHORT_DATETIME_FORMAT" %}{% endtimezone %} + Site time: + {% timezone object.time_zone %} + {% annotated_now %} + {% endtimezone %} + {% else %} {% endif %} diff --git a/netbox/templates/extras/journalentry.html b/netbox/templates/extras/journalentry.html index f64741f36..6e986be6c 100644 --- a/netbox/templates/extras/journalentry.html +++ b/netbox/templates/extras/journalentry.html @@ -25,7 +25,7 @@ Created - {{ object.created }} + {{ object.created|annotated_date }} diff --git a/netbox/templates/extras/objectchange.html b/netbox/templates/extras/objectchange.html index c49cea79b..b13491a55 100644 --- a/netbox/templates/extras/objectchange.html +++ b/netbox/templates/extras/objectchange.html @@ -44,7 +44,7 @@ Time - {{ object.time }} + {{ object.time|annotated_date }} diff --git a/netbox/templates/extras/report.html b/netbox/templates/extras/report.html index 82c3f3042..642272c8f 100644 --- a/netbox/templates/extras/report.html +++ b/netbox/templates/extras/report.html @@ -38,7 +38,7 @@

diff --git a/netbox/templates/extras/report_list.html b/netbox/templates/extras/report_list.html index 525a40d12..ac8171a9a 100644 --- a/netbox/templates/extras/report_list.html +++ b/netbox/templates/extras/report_list.html @@ -32,7 +32,7 @@ {{ report.description|render_markdown|placeholder }} {% if report.result %} - {{ report.result.created }} + {{ report.result.created|annotated_date }} {% else %} Never {% endif %} diff --git a/netbox/templates/extras/report_result.html b/netbox/templates/extras/report_result.html index 3d01ca38e..073edc803 100644 --- a/netbox/templates/extras/report_result.html +++ b/netbox/templates/extras/report_result.html @@ -8,7 +8,7 @@

- Run: {{ result.created }} + Run: {{ result.created|annotated_date }} {% if result.completed %} Duration: {{ result.duration }} {% else %} diff --git a/netbox/templates/extras/script_list.html b/netbox/templates/extras/script_list.html index e87860622..71a73d80e 100644 --- a/netbox/templates/extras/script_list.html +++ b/netbox/templates/extras/script_list.html @@ -29,7 +29,7 @@ {{ script.Meta.description|render_markdown }} {% if script.result %} - {{ script.result.created }} + {{ script.result.created|annotated_date }} {% else %} Never diff --git a/netbox/templates/extras/script_result.html b/netbox/templates/extras/script_result.html index 76e0613b7..816a2348d 100644 --- a/netbox/templates/extras/script_result.html +++ b/netbox/templates/extras/script_result.html @@ -13,7 +13,7 @@

  • Scripts
  • {{ script.module|bettertitle }}
  • {{ script }}
  • -
  • {{ result.created }}
  • +
  • {{ result.created|annotated_date }}
  • @@ -32,7 +32,7 @@

    - Run: {{ result.created }} + Run: {{ result.created|annotated_date }} {% if result.completed %} Duration: {{ result.duration }} {% else %} diff --git a/netbox/templates/generic/object.html b/netbox/templates/generic/object.html index b2f6926d7..648a936e0 100644 --- a/netbox/templates/generic/object.html +++ b/netbox/templates/generic/object.html @@ -42,8 +42,8 @@

    {% block title %}{{ object }}{% endblock %}

    - Created {{ object.created }} · - Updated {{ object.last_updated|timesince }} ago + Created {{ object.created|annotated_date }} · + Updated {{ object.last_updated|timesince }} ago {{ object|meta:"app_label" }}.{{ object|meta:"model_name" }}:{{ object.pk }}

    diff --git a/netbox/templates/home.html b/netbox/templates/home.html index 273a78bc9..618deb8ca 100644 --- a/netbox/templates/home.html +++ b/netbox/templates/home.html @@ -291,7 +291,7 @@ {% for result in report_results %} {{ result.name }} - {% include 'extras/inc/job_label.html' %} + {% include 'extras/inc/job_label.html' %} {% endfor %} diff --git a/netbox/templates/inc/custom_fields_panel.html b/netbox/templates/inc/custom_fields_panel.html index bd80974eb..d938244ac 100644 --- a/netbox/templates/inc/custom_fields_panel.html +++ b/netbox/templates/inc/custom_fields_panel.html @@ -1,3 +1,4 @@ +{% load helpers %} {% with custom_fields=object.get_custom_fields %} {% if custom_fields %}
    @@ -17,6 +18,8 @@ {{ value|truncatechars:70 }} {% elif field.type == 'multiselect' and value %} {{ value|join:", " }} + {% elif field.type == 'date' and value %} + {{ value|annotated_date }} {% elif value is not None %} {{ value }} {% elif field.required %} diff --git a/netbox/templates/inc/image_attachments.html b/netbox/templates/inc/image_attachments.html index 38be9924d..d1f4e45dc 100644 --- a/netbox/templates/inc/image_attachments.html +++ b/netbox/templates/inc/image_attachments.html @@ -1,3 +1,4 @@ +{% load helpers %} {% if images %} @@ -13,7 +14,7 @@ {{ attachment }} - + - + diff --git a/netbox/templates/users/api_tokens.html b/netbox/templates/users/api_tokens.html index f14773293..ad2e3911a 100644 --- a/netbox/templates/users/api_tokens.html +++ b/netbox/templates/users/api_tokens.html @@ -24,12 +24,12 @@
    Created
    - {{ token.created|date }} + {{ token.created|annotated_date }}
    Expires
    {% if token.expires %} - {{ token.expires|date }} + {{ token.expires|annotated_date }} {% else %} Never {% endif %} diff --git a/netbox/templates/users/profile.html b/netbox/templates/users/profile.html index 35a94ac6f..2e575cca6 100644 --- a/netbox/templates/users/profile.html +++ b/netbox/templates/users/profile.html @@ -11,7 +11,7 @@ Email
    {{ request.user.email }}
    Registered -
    {{ request.user.date_joined }}
    +
    {{ request.user.date_joined|annotated_date }}
    Groups
    {{ request.user.groups.all|join:', ' }}
    Admin access diff --git a/netbox/templates/users/userkey.html b/netbox/templates/users/userkey.html index 3839a6925..95e9846db 100644 --- a/netbox/templates/users/userkey.html +++ b/netbox/templates/users/userkey.html @@ -1,4 +1,5 @@ {% extends 'users/base.html' %} +{% load helpers %} {% block title %}User Key{% endblock %} @@ -19,7 +20,9 @@ {% endif %}

    - Created {{ object.created }} · Updated {{ object.last_updated|timesince }} ago + + Created {{ object.created|annotated_date }} · + Updated {{ object.last_updated|timesince }} ago

    {% if not object.is_active %}

    Session key: Active

    - Created {{ object.session_key.created }} + Created {{ object.session_key.created|annotated_date }} {% else %}

    No active session key

    {% endif %} diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index 49d8323e9..54725502a 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -4,8 +4,10 @@ import re import yaml from django import template +from django.template.defaultfilters import date from django.conf import settings from django.urls import NoReverseMatch, reverse +from django.utils import timezone from django.utils.html import strip_tags from django.utils.safestring import mark_safe from markdown import markdown @@ -151,6 +153,40 @@ def tzoffset(value): return datetime.datetime.now(value).strftime('%z') +@register.filter(expects_localtime=True) +def annotated_date(date_value): + """ + Returns date as HTML span with short date format as the content and the + (long) date format as the title. + """ + if not date_value: + return '' + + # ../../templates/inc/custom_fields_panel.html passes a string. + if type(date_value) == str: + date_value = datetime.datetime.strptime(date_value, "%Y-%m-%d").date() + + if type(date_value) == datetime.date: + long_ts = date(date_value, 'DATE_FORMAT') + short_ts = date(date_value, 'SHORT_DATE_FORMAT') + else: + long_ts = date(date_value, 'DATETIME_FORMAT') + short_ts = date(date_value, 'SHORT_DATETIME_FORMAT') + + span = f'{short_ts}' + + return mark_safe(span) + + +@register.simple_tag +def annotated_now(): + """ + Returns the current date piped through the annotated_date filter. + """ + tzinfo = timezone.get_current_timezone() if settings.USE_TZ else None + return annotated_date(datetime.datetime.now(tz=tzinfo)) + + @register.filter() def fgcolor(value): """
    {{ attachment.size|filesizeformat }}{{ attachment.created }}{{ attachment.created|annotated_date }} {% if perms.extras.change_imageattachment %} diff --git a/netbox/templates/ipam/aggregate.html b/netbox/templates/ipam/aggregate.html index 468531b55..60a467644 100644 --- a/netbox/templates/ipam/aggregate.html +++ b/netbox/templates/ipam/aggregate.html @@ -54,7 +54,7 @@
    Date Added{{ object.date_added|placeholder }}{{ object.date_added|annotated_date|placeholder }}
    Description