Force AM/PM for en-GB 12H setting combination

This commit is contained in:
Christopher C. Wells 2021-11-28 15:51:51 -05:00
parent f0a7f53752
commit e13f00e024
2 changed files with 59 additions and 12 deletions

View File

@ -7,19 +7,37 @@ from django.utils.translation import gettext_lazy as _
register = template.Library() register = template.Library()
@register.simple_tag() @register.simple_tag(takes_context=True)
def datetimepicker_format(format_string='L LT'): def datetimepicker_format(context, format_string='L LT'):
""" """
Return a datetime format string for momentjs, with support for 24 hour time Return a datetime format string for momentjs, with support for 24 hour time
override setting. override setting.
:param context: caller context data
:param format_string: the default format string (locale based) :param format_string: the default format string (locale based)
:return: the format string to use, as 24 hour time if configured. :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 settings.USE_24_HOUR_TIME_FORMAT:
if format_string == 'L LT': if format_string == 'L LT':
return 'L HH:mm' format_string = 'L HH:mm'
elif format_string == 'L LTS': 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 return format_string

View File

@ -1,12 +1,17 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.contrib.auth.models import User 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 django.utils import timezone, formats
from core.models import Child, Timer from core.models import Child, Timer
from core.templatetags import bootstrap, datetime, duration, timers from core.templatetags import bootstrap, datetime, duration, timers
class MockUserRequest:
def __init__(self, user):
self.user = user
class TemplateTagsTestCase(TestCase): class TemplateTagsTestCase(TestCase):
def test_bootstrap_bool_icon(self): def test_bootstrap_bool_icon(self):
self.assertEqual( self.assertEqual(
@ -72,17 +77,41 @@ class TemplateTagsTestCase(TestCase):
timer.id, child.slug)) timer.id, child.slug))
def test_datetimepicker_format(self): def test_datetimepicker_format(self):
self.assertEqual(datetime.datetimepicker_format(), 'L LT') request = MockUserRequest(User.objects.first())
self.assertEqual(datetime.datetimepicker_format('L LT'), 'L LT') 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( self.assertEqual(
datetime.datetimepicker_format('L LTS'), 'L LTS') 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): with self.settings(USE_24_HOUR_TIME_FORMAT=True):
self.assertEqual(datetime.datetimepicker_format(), 'L HH:mm')
self.assertEqual( self.assertEqual(
datetime.datetimepicker_format('L LT'), 'L HH:mm') datetime.datetimepicker_format(context), 'L HH:mm')
self.assertEqual( 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): def test_datetime_short(self):
date = timezone.localtime() date = timezone.localtime()