diff --git a/core/templatetags/datetime.py b/core/templatetags/datetime.py index 6114840e..f3d370fd 100644 --- a/core/templatetags/datetime.py +++ b/core/templatetags/datetime.py @@ -7,19 +7,37 @@ from django.utils.translation import gettext_lazy as _ register = template.Library() -@register.simple_tag() -def datetimepicker_format(format_string='L LT'): +@register.simple_tag(takes_context=True) +def datetimepicker_format(context, format_string='L LT'): """ Return a datetime format string for momentjs, with support for 24 hour time override setting. + :param context: caller context data :param format_string: the default format string (locale based) :return: the format string to use, as 24 hour time if configured. """ + try: + user = context['request'].user + if hasattr(user, 'settings') and user.settings.language: + language = user.settings.language + else: + language = settings.LANGUAGE_CODE + except KeyError: + language = None + if settings.USE_24_HOUR_TIME_FORMAT: if format_string == 'L LT': - return 'L HH:mm' + format_string = 'L HH:mm' elif format_string == 'L LTS': - return 'L HH:mm:ss' + format_string = 'L HH:mm:ss' + elif language and language == 'en-GB': + # Force 12-hour format if 24 hour format is not configured for en-GB + # (Django default is 12H, momentjs default is 24H). + if format_string == 'L LT': + format_string = 'L h:mm a' + elif format_string == 'L LTS': + format_string = 'L h:mm:ss a' + return format_string diff --git a/core/tests/tests_templatetags.py b/core/tests/tests_templatetags.py index d775160b..ec0e614f 100644 --- a/core/tests/tests_templatetags.py +++ b/core/tests/tests_templatetags.py @@ -1,12 +1,17 @@ # -*- coding: utf-8 -*- from django.contrib.auth.models import User -from django.test import TestCase +from django.test import TestCase, override_settings from django.utils import timezone, formats from core.models import Child, Timer from core.templatetags import bootstrap, datetime, duration, timers +class MockUserRequest: + def __init__(self, user): + self.user = user + + class TemplateTagsTestCase(TestCase): def test_bootstrap_bool_icon(self): self.assertEqual( @@ -72,17 +77,41 @@ class TemplateTagsTestCase(TestCase): timer.id, child.slug)) def test_datetimepicker_format(self): - self.assertEqual(datetime.datetimepicker_format(), 'L LT') - self.assertEqual(datetime.datetimepicker_format('L LT'), 'L LT') - self.assertEqual( - datetime.datetimepicker_format('L LTS'), 'L LTS') + request = MockUserRequest(User.objects.first()) + request.user.settings.dashboard_hide_empty = True + context = {'request': request} + + with self.settings(USE_24_HOUR_TIME_FORMAT=False): + self.assertEqual(datetime.datetimepicker_format(context), 'L LT') + self.assertEqual( + datetime.datetimepicker_format(context, 'L LT'), 'L LT') + self.assertEqual( + datetime.datetimepicker_format(context, 'L LTS'), 'L LTS') with self.settings(USE_24_HOUR_TIME_FORMAT=True): - self.assertEqual(datetime.datetimepicker_format(), 'L HH:mm') self.assertEqual( - datetime.datetimepicker_format('L LT'), 'L HH:mm') + datetime.datetimepicker_format(context), 'L HH:mm') self.assertEqual( - datetime.datetimepicker_format('L LTS'), 'L HH:mm:ss') + datetime.datetimepicker_format(context, 'L LT'), 'L HH:mm') + self.assertEqual( + datetime.datetimepicker_format(context, 'L LTS'), 'L HH:mm:ss') + + @override_settings(USE_24_HOUR_TIME_FORMAT=False) + def test_datetimepicker_format_en_gb(self): + user = User.objects.first() + user.settings.language = 'en-GB' + user.save() + + request = MockUserRequest(user) + request.user.settings.dashboard_hide_empty = True + context = {'request': request} + + self.assertEqual( + datetime.datetimepicker_format(context), 'L h:mm a') + self.assertEqual( + datetime.datetimepicker_format(context, 'L LT'), 'L h:mm a') + self.assertEqual( + datetime.datetimepicker_format(context, 'L LTS'), 'L h:mm:ss a') def test_datetime_short(self): date = timezone.localtime()