Use short datetime string for lists (WIP)

This commit is contained in:
Christopher C. Wells 2021-07-31 14:50:29 -07:00 committed by Christopher Charbonneau Wells
parent c1dab44596
commit 58acccfddb
11 changed files with 65 additions and 37 deletions

View File

@ -1,5 +1,5 @@
{% extends 'babybuddy/page.html' %} {% extends 'babybuddy/page.html' %}
{% load datetimepicker i18n %} {% load datetime i18n %}
{% block title %} {% block title %}
{% if request.resolver_match.url_name == 'diaperchange-update' %} {% if request.resolver_match.url_name == 'diaperchange-update' %}

View File

@ -1,5 +1,5 @@
{% extends 'babybuddy/page.html' %} {% extends 'babybuddy/page.html' %}
{% load bootstrap i18n widget_tweaks %} {% load bootstrap datetime i18n widget_tweaks %}
{% block title %}{% trans "Diaper Changes" %}{% endblock %} {% block title %}{% trans "Diaper Changes" %}{% endblock %}
@ -34,7 +34,7 @@
{% for change in object_list %} {% for change in object_list %}
{% cycle "odd" "even" as row_class silent %} {% cycle "odd" "even" as row_class silent %}
<tr class="{{ row_class }}"> <tr class="{{ row_class }}">
<th scope="row">{{ change.time }}</th> <th scope="row">{{ change.time|datetime_short }}</th>
<td><a href="{% url 'core:child' change.child.slug %}">{{ change.child }}</a></td> <td><a href="{% url 'core:child' change.child.slug %}">{{ change.child }}</a></td>
<td class="text-center">{{ change.wet|bool_icon }}</td> <td class="text-center">{{ change.wet|bool_icon }}</td>
<td class="text-center">{{ change.solid|bool_icon }}</td> <td class="text-center">{{ change.solid|bool_icon }}</td>

View File

@ -1,5 +1,5 @@
{% extends 'babybuddy/page.html' %} {% extends 'babybuddy/page.html' %}
{% load datetimepicker i18n %} {% load datetime i18n %}
{% block title %} {% block title %}
{% if request.resolver_match.url_name == 'feeding-update' %} {% if request.resolver_match.url_name == 'feeding-update' %}

View File

@ -1,5 +1,5 @@
{% extends 'babybuddy/page.html' %} {% extends 'babybuddy/page.html' %}
{% load datetimepicker i18n %} {% load datetime i18n %}
{% block title %} {% block title %}
{% if request.resolver_match.url_name == 'note-update' %} {% if request.resolver_match.url_name == 'note-update' %}

View File

@ -1,5 +1,5 @@
{% extends 'babybuddy/page.html' %} {% extends 'babybuddy/page.html' %}
{% load datetimepicker i18n %} {% load datetime i18n %}
{% block title %} {% block title %}
{% if request.resolver_match.url_name == 'sleep-update' %} {% if request.resolver_match.url_name == 'sleep-update' %}

View File

@ -1,5 +1,5 @@
{% extends 'babybuddy/page.html' %} {% extends 'babybuddy/page.html' %}
{% load datetimepicker i18n %} {% load datetime i18n %}
{% block title %} {% block title %}
{% if object %} {% if object %}

View File

@ -1,5 +1,5 @@
{% extends 'babybuddy/page.html' %} {% extends 'babybuddy/page.html' %}
{% load datetimepicker duration i18n %} {% load datetime duration i18n %}
{% block title %}{% trans "Timer" %}{% endblock %} {% block title %}{% trans "Timer" %}{% endblock %}

View File

@ -1,5 +1,5 @@
{% extends 'babybuddy/page.html' %} {% extends 'babybuddy/page.html' %}
{% load datetimepicker i18n %} {% load datetime i18n %}
{% block title %} {% block title %}
{% if request.resolver_match.url_name == 'tummytime-update' %} {% if request.resolver_match.url_name == 'tummytime-update' %}

View File

@ -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

View File

@ -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

View File

@ -4,7 +4,7 @@ from django.test import TestCase
from django.utils import timezone from django.utils import timezone
from core.models import Child, Timer 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): class TemplateTagsTestCase(TestCase):
@ -72,14 +72,14 @@ class TemplateTagsTestCase(TestCase):
timer.id, child.slug)) timer.id, child.slug))
def test_datetimepicker_format(self): def test_datetimepicker_format(self):
self.assertEqual(datetimepicker.datetimepicker_format(), 'L LT') self.assertEqual(datetime.datetimepicker_format(), 'L LT')
self.assertEqual(datetimepicker.datetimepicker_format('L LT'), 'L LT') self.assertEqual(datetime.datetimepicker_format('L LT'), 'L LT')
self.assertEqual( 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): 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( self.assertEqual(
datetimepicker.datetimepicker_format('L LT'), 'L HH:mm') datetime.datetimepicker_format('L LT'), 'L HH:mm')
self.assertEqual( self.assertEqual(
datetimepicker.datetimepicker_format('L LTS'), 'L HH:mm:ss') datetime.datetimepicker_format('L LTS'), 'L HH:mm:ss')