From 58acccfddbdb88fb82c4e211ff72db59958ab61a Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sat, 31 Jul 2021 14:50:29 -0700 Subject: [PATCH] Use short datetime string for lists (WIP) --- core/templates/core/diaperchange_form.html | 2 +- core/templates/core/diaperchange_list.html | 4 +- core/templates/core/feeding_form.html | 2 +- core/templates/core/note_form.html | 2 +- core/templates/core/sleep_form.html | 2 +- core/templates/core/temperature_form.html | 2 +- core/templates/core/timer_form.html | 2 +- core/templates/core/tummytime_form.html | 2 +- core/templatetags/datetime.py | 49 ++++++++++++++++++++++ core/templatetags/datetimepicker.py | 21 ---------- core/tests/tests_templatetags.py | 14 +++---- 11 files changed, 65 insertions(+), 37 deletions(-) create mode 100644 core/templatetags/datetime.py delete mode 100644 core/templatetags/datetimepicker.py diff --git a/core/templates/core/diaperchange_form.html b/core/templates/core/diaperchange_form.html index e0de13c1..4ee5492e 100644 --- a/core/templates/core/diaperchange_form.html +++ b/core/templates/core/diaperchange_form.html @@ -1,5 +1,5 @@ {% extends 'babybuddy/page.html' %} -{% load datetimepicker i18n %} +{% load datetime i18n %} {% block title %} {% if request.resolver_match.url_name == 'diaperchange-update' %} diff --git a/core/templates/core/diaperchange_list.html b/core/templates/core/diaperchange_list.html index 7eebeb0b..8406fdf6 100644 --- a/core/templates/core/diaperchange_list.html +++ b/core/templates/core/diaperchange_list.html @@ -1,5 +1,5 @@ {% extends 'babybuddy/page.html' %} -{% load bootstrap i18n widget_tweaks %} +{% load bootstrap datetime i18n widget_tweaks %} {% block title %}{% trans "Diaper Changes" %}{% endblock %} @@ -34,7 +34,7 @@ {% for change in object_list %} {% cycle "odd" "even" as row_class silent %} - {{ change.time }} + {{ change.time|datetime_short }} {{ change.child }} {{ change.wet|bool_icon }} {{ change.solid|bool_icon }} diff --git a/core/templates/core/feeding_form.html b/core/templates/core/feeding_form.html index 2348f654..e7b0da23 100644 --- a/core/templates/core/feeding_form.html +++ b/core/templates/core/feeding_form.html @@ -1,5 +1,5 @@ {% extends 'babybuddy/page.html' %} -{% load datetimepicker i18n %} +{% load datetime i18n %} {% block title %} {% if request.resolver_match.url_name == 'feeding-update' %} diff --git a/core/templates/core/note_form.html b/core/templates/core/note_form.html index 25b2a2d7..b475a104 100644 --- a/core/templates/core/note_form.html +++ b/core/templates/core/note_form.html @@ -1,5 +1,5 @@ {% extends 'babybuddy/page.html' %} -{% load datetimepicker i18n %} +{% load datetime i18n %} {% block title %} {% if request.resolver_match.url_name == 'note-update' %} diff --git a/core/templates/core/sleep_form.html b/core/templates/core/sleep_form.html index 411cc07a..a81dec53 100644 --- a/core/templates/core/sleep_form.html +++ b/core/templates/core/sleep_form.html @@ -1,5 +1,5 @@ {% extends 'babybuddy/page.html' %} -{% load datetimepicker i18n %} +{% load datetime i18n %} {% block title %} {% if request.resolver_match.url_name == 'sleep-update' %} diff --git a/core/templates/core/temperature_form.html b/core/templates/core/temperature_form.html index e20be1f3..3ec470b9 100644 --- a/core/templates/core/temperature_form.html +++ b/core/templates/core/temperature_form.html @@ -1,5 +1,5 @@ {% extends 'babybuddy/page.html' %} -{% load datetimepicker i18n %} +{% load datetime i18n %} {% block title %} {% if object %} diff --git a/core/templates/core/timer_form.html b/core/templates/core/timer_form.html index 15a33810..7c50b334 100644 --- a/core/templates/core/timer_form.html +++ b/core/templates/core/timer_form.html @@ -1,5 +1,5 @@ {% extends 'babybuddy/page.html' %} -{% load datetimepicker duration i18n %} +{% load datetime duration i18n %} {% block title %}{% trans "Timer" %}{% endblock %} diff --git a/core/templates/core/tummytime_form.html b/core/templates/core/tummytime_form.html index e88bf77d..4c59d942 100644 --- a/core/templates/core/tummytime_form.html +++ b/core/templates/core/tummytime_form.html @@ -1,5 +1,5 @@ {% extends 'babybuddy/page.html' %} -{% load datetimepicker i18n %} +{% load datetime i18n %} {% block title %} {% if request.resolver_match.url_name == 'tummytime-update' %} diff --git a/core/templatetags/datetime.py b/core/templatetags/datetime.py new file mode 100644 index 00000000..93181f81 --- /dev/null +++ b/core/templatetags/datetime.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +from django import template +from django.conf import settings +from django.utils import timezone, formats +from django.utils.translation import gettext_lazy as _ + +register = template.Library() + + +@register.simple_tag() +def datetimepicker_format(format_string='L LT'): + """ + Return a datetime format string for momentjs, with support for 24 hour time + override setting. + :param format_string: the default format string (locale based) + :return: the format string to use, as 24 hour time if configured. + """ + if settings.USE_24_HOUR_TIME_FORMAT: + if format_string == 'L LT': + return 'L HH:mm' + elif format_string == 'L LTS': + return 'L HH:mm:ss' + return format_string + + +@register.filter() +def datetime_short(date): + """ + Format a datetime object as short string for list views + :param date: datetime instance + :return: a string representation of `date`. + """ + now = timezone.now() + time_string = None + if now.date() == date.date(): + date_string = _('Today') + time_string = formats.date_format(date, format='TIME_FORMAT') + elif now.year == date.year: + date_string = formats.date_format(date, format='MONTH_DAY_FORMAT') + time_string = formats.date_format(date, format='TIME_FORMAT') + else: + date_string = formats.date_format(date, format='SHORT_DATETIME_FORMAT') + + if date_string and time_string: + datetime_string = '{}, {}'.format(date_string, time_string) + else: + datetime_string = date_string + + return datetime_string diff --git a/core/templatetags/datetimepicker.py b/core/templatetags/datetimepicker.py deleted file mode 100644 index 3df17198..00000000 --- a/core/templatetags/datetimepicker.py +++ /dev/null @@ -1,21 +0,0 @@ -# -*- coding: utf-8 -*- -from django import template -from django.conf import settings - -register = template.Library() - - -@register.simple_tag() -def datetimepicker_format(format_string='L LT'): - """ - Return a datetime format string for momentjs, with support for 24 hour time - override setting. - :param format_string: the default format string (locale based) - :return: the format string to use, as 24 hour time if configured. - """ - if settings.USE_24_HOUR_TIME_FORMAT: - if format_string == 'L LT': - return 'L HH:mm' - elif format_string == 'L LTS': - return 'L HH:mm:ss' - return format_string diff --git a/core/tests/tests_templatetags.py b/core/tests/tests_templatetags.py index 1cbd8cf5..ce42c655 100644 --- a/core/tests/tests_templatetags.py +++ b/core/tests/tests_templatetags.py @@ -4,7 +4,7 @@ from django.test import TestCase from django.utils import timezone from core.models import Child, Timer -from core.templatetags import bootstrap, datetimepicker, duration, timers +from core.templatetags import bootstrap, datetime, duration, timers class TemplateTagsTestCase(TestCase): @@ -72,14 +72,14 @@ class TemplateTagsTestCase(TestCase): timer.id, child.slug)) def test_datetimepicker_format(self): - self.assertEqual(datetimepicker.datetimepicker_format(), 'L LT') - self.assertEqual(datetimepicker.datetimepicker_format('L LT'), 'L LT') + self.assertEqual(datetime.datetimepicker_format(), 'L LT') + self.assertEqual(datetime.datetimepicker_format('L LT'), 'L LT') self.assertEqual( - datetimepicker.datetimepicker_format('L LTS'), 'L LTS') + datetime.datetimepicker_format('L LTS'), 'L LTS') with self.settings(USE_24_HOUR_TIME_FORMAT=True): - self.assertEqual(datetimepicker.datetimepicker_format(), 'L HH:mm') + self.assertEqual(datetime.datetimepicker_format(), 'L HH:mm') self.assertEqual( - datetimepicker.datetimepicker_format('L LT'), 'L HH:mm') + datetime.datetimepicker_format('L LT'), 'L HH:mm') self.assertEqual( - datetimepicker.datetimepicker_format('L LTS'), 'L HH:mm:ss') + datetime.datetimepicker_format('L LTS'), 'L HH:mm:ss')