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

View File

@ -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')
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('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):
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()