mirror of https://github.com/snachodog/mybuddy.git
Remove support for 24H time format override
This commit is contained in:
parent
decc97a807
commit
5875ef9658
|
@ -0,0 +1,5 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Add custom "short" version of `MONTH_DAY_FORMAT`. This customization will
|
||||||
|
# only work with the locale format locale specified by this file.
|
||||||
|
SHORT_MONTH_DAY_FORMAT = 'M j'
|
|
@ -1,11 +0,0 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
from django.conf.locale.pt import formats
|
|
||||||
|
|
||||||
# Limit datetime input formats to those support by moment.
|
|
||||||
formats_supported = list(
|
|
||||||
filter(
|
|
||||||
lambda dt_format: not dt_format.startswith("%Y-%m-%d"),
|
|
||||||
formats.DATETIME_INPUT_FORMATS,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
DATETIME_INPUT_FORMATS = formats_supported
|
|
|
@ -1,9 +0,0 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
from django.conf.locale.tr import formats
|
|
||||||
|
|
||||||
# Add formats supported by moment.
|
|
||||||
DATETIME_INPUT_FORMATS = [
|
|
||||||
"%d.%m.%Y %H:%M:%S",
|
|
||||||
"%d.%m.%Y %H:%M",
|
|
||||||
*formats.DATETIME_INPUT_FORMATS,
|
|
||||||
]
|
|
|
@ -5,68 +5,9 @@ import pytz
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils import timezone, translation
|
from django.utils import timezone, translation
|
||||||
from django.conf.locale.en import formats as formats_en_us
|
|
||||||
from django.conf.locale.en_GB import formats as formats_en_gb
|
|
||||||
from django.contrib.auth.middleware import RemoteUserMiddleware
|
from django.contrib.auth.middleware import RemoteUserMiddleware
|
||||||
|
|
||||||
|
|
||||||
def update_en_us_date_formats():
|
|
||||||
"""
|
|
||||||
Update the datetime formats for the en-US locale. This is handled here and
|
|
||||||
not using `FORMAT_MODULE_PATH` because the processing of format modules
|
|
||||||
does not allow us to distinguish appropriately between en-US and en-GB
|
|
||||||
based on user settings.
|
|
||||||
"""
|
|
||||||
if settings.USE_24_HOUR_TIME_FORMAT:
|
|
||||||
formats_en_us.DATETIME_FORMAT = "N j, Y, H:i:s"
|
|
||||||
custom_input_formats = [
|
|
||||||
"%m/%d/%Y %H:%M:%S", # '10/25/2006 14:30:59'
|
|
||||||
"%m/%d/%Y %H:%M", # '10/25/2006 14:30'
|
|
||||||
]
|
|
||||||
formats_en_us.SHORT_DATETIME_FORMAT = "m/d/Y G:i:s"
|
|
||||||
formats_en_us.TIME_FORMAT = "H:i:s"
|
|
||||||
else:
|
|
||||||
# These formats are added to support the locale style of Baby Buddy's
|
|
||||||
# frontend library, which uses momentjs.
|
|
||||||
custom_input_formats = [
|
|
||||||
"%m/%d/%Y %I:%M:%S %p", # '10/25/2006 2:30:59 PM'
|
|
||||||
"%m/%d/%Y %I:%M %p", # '10/25/2006 2:30 PM'
|
|
||||||
]
|
|
||||||
|
|
||||||
# Add custom "short" version of `MONTH_DAY_FORMAT`.
|
|
||||||
formats_en_us.SHORT_MONTH_DAY_FORMAT = "M j"
|
|
||||||
|
|
||||||
# Append all other input formats from the base locale.
|
|
||||||
formats_en_us.DATETIME_INPUT_FORMATS = (
|
|
||||||
custom_input_formats + formats_en_us.DATETIME_INPUT_FORMATS
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def update_en_gb_date_formats():
|
|
||||||
if settings.USE_24_HOUR_TIME_FORMAT:
|
|
||||||
# 25 October 2006 14:30:00
|
|
||||||
formats_en_gb.DATETIME_FORMAT = "j F Y H:i:s"
|
|
||||||
custom_input_formats = [
|
|
||||||
"%d/%m/%Y %H:%M:%S", # '25/10/2006 14:30:59'
|
|
||||||
"%d/%m/%Y %H:%M", # '25/10/2006 14:30'
|
|
||||||
]
|
|
||||||
formats_en_gb.SHORT_DATETIME_FORMAT = "d/m/Y H:i"
|
|
||||||
formats_en_gb.TIME_FORMAT = "H:i"
|
|
||||||
else:
|
|
||||||
formats_en_gb.DATETIME_FORMAT = "j F Y f a" # 25 October 2006 2:30 p.m
|
|
||||||
# These formats are added to support the locale style of Baby Buddy's
|
|
||||||
# frontend library, which uses momentjs.
|
|
||||||
custom_input_formats = [
|
|
||||||
"%d/%m/%Y %I:%M:%S %p", # '25/10/2006 2:30:59 PM'
|
|
||||||
"%d/%m/%Y %I:%M %p", # '25/10/2006 2:30 PM'
|
|
||||||
]
|
|
||||||
|
|
||||||
# Append all other input formats from the base locale.
|
|
||||||
formats_en_gb.DATETIME_INPUT_FORMATS = (
|
|
||||||
custom_input_formats + formats_en_gb.DATETIME_INPUT_FORMATS
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class UserLanguageMiddleware:
|
class UserLanguageMiddleware:
|
||||||
"""
|
"""
|
||||||
Customizes settings based on user language setting.
|
Customizes settings based on user language setting.
|
||||||
|
@ -85,11 +26,6 @@ class UserLanguageMiddleware:
|
||||||
language = settings.LANGUAGE_CODE
|
language = settings.LANGUAGE_CODE
|
||||||
|
|
||||||
if language:
|
if language:
|
||||||
if language == "en-US":
|
|
||||||
update_en_us_date_formats()
|
|
||||||
elif language == "en-GB":
|
|
||||||
update_en_gb_date_formats()
|
|
||||||
|
|
||||||
# Set the language before generating the response.
|
# Set the language before generating the response.
|
||||||
translation.activate(language)
|
translation.activate(language)
|
||||||
|
|
||||||
|
|
|
@ -200,16 +200,6 @@ USE_L10N = True
|
||||||
|
|
||||||
FORMAT_MODULE_PATH = ["babybuddy.formats"]
|
FORMAT_MODULE_PATH = ["babybuddy.formats"]
|
||||||
|
|
||||||
# Custom setting that can be used to override the locale-based time set by
|
|
||||||
# USE_L10N _for specific locales_ to use 24-hour format. In order for this to
|
|
||||||
# work with a given locale it must be set at the FORMAT_MODULE_PATH with
|
|
||||||
# conditionals on this setting. See babybuddy/forms/en/formats.py for an example
|
|
||||||
# implementation for the English locale.
|
|
||||||
|
|
||||||
USE_24_HOUR_TIME_FORMAT = bool(
|
|
||||||
strtobool(os.environ.get("USE_24_HOUR_TIME_FORMAT") or "False")
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# Static files (CSS, JavaScript, Images)
|
# Static files (CSS, JavaScript, Images)
|
||||||
# https://docs.djangoproject.com/en/4.0/howto/static-files/
|
# https://docs.djangoproject.com/en/4.0/howto/static-files/
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.utils.formats import date_format
|
||||||
|
|
||||||
|
|
||||||
|
class FormatsTestCase(TestCase):
|
||||||
|
def test_short_month_day_format(self):
|
||||||
|
dt = datetime.datetime(year=2021, month=7, day=31, hour=5, minute=5, second=5)
|
||||||
|
self.assertEqual(date_format(dt, "SHORT_MONTH_DAY_FORMAT"), "Jul 31")
|
|
@ -1,70 +0,0 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
import datetime
|
|
||||||
|
|
||||||
from django.core.exceptions import ValidationError
|
|
||||||
from django.forms.fields import DateTimeField
|
|
||||||
from django.test import TestCase, override_settings # , tag
|
|
||||||
from django.utils.formats import date_format, time_format
|
|
||||||
|
|
||||||
from babybuddy.middleware import update_en_gb_date_formats
|
|
||||||
|
|
||||||
|
|
||||||
class GbFormatsTestCase(TestCase):
|
|
||||||
@override_settings(LANGUAGE_CODE="en-GB")
|
|
||||||
def test_datetime_input_formats(self):
|
|
||||||
update_en_gb_date_formats()
|
|
||||||
field = DateTimeField()
|
|
||||||
supported_custom_examples = [
|
|
||||||
"20/01/2020",
|
|
||||||
"20/01/2020 9:30 AM",
|
|
||||||
"20/01/2020 9:30:03 AM",
|
|
||||||
"01/10/2020 11:30 PM",
|
|
||||||
"01/10/2020 11:30:03 AM",
|
|
||||||
]
|
|
||||||
|
|
||||||
for example in supported_custom_examples:
|
|
||||||
try:
|
|
||||||
result = field.to_python(example)
|
|
||||||
self.assertIsInstance(result, datetime.datetime)
|
|
||||||
except ValidationError:
|
|
||||||
self.fail('Format of "{}" not recognized!'.format(example))
|
|
||||||
|
|
||||||
with self.assertRaises(ValidationError):
|
|
||||||
field.to_python("invalid date string!")
|
|
||||||
|
|
||||||
# @tag('isolate')
|
|
||||||
@override_settings(LANGUAGE_CODE="en-GB", USE_24_HOUR_TIME_FORMAT=True)
|
|
||||||
def test_use_24_hour_time_format(self):
|
|
||||||
update_en_gb_date_formats()
|
|
||||||
field = DateTimeField()
|
|
||||||
supported_custom_examples = [
|
|
||||||
"25/10/2006 2:30:59",
|
|
||||||
"25/10/2006 2:30",
|
|
||||||
"25/10/2006 14:30:59",
|
|
||||||
"25/10/2006 14:30",
|
|
||||||
]
|
|
||||||
|
|
||||||
for example in supported_custom_examples:
|
|
||||||
try:
|
|
||||||
result = field.to_python(example)
|
|
||||||
self.assertIsInstance(result, datetime.datetime)
|
|
||||||
except ValidationError:
|
|
||||||
self.fail('Format of "{}" not recognized!'.format(example))
|
|
||||||
|
|
||||||
with self.assertRaises(ValidationError):
|
|
||||||
field.to_python("invalid date string!")
|
|
||||||
|
|
||||||
dt = datetime.datetime(year=2011, month=11, day=4, hour=23, minute=5, second=59)
|
|
||||||
self.assertEqual(date_format(dt, "DATETIME_FORMAT"), "4 November 2011 23:05:59")
|
|
||||||
|
|
||||||
dt = datetime.datetime(year=2011, month=11, day=4, hour=2, minute=5, second=59)
|
|
||||||
self.assertEqual(date_format(dt, "SHORT_DATETIME_FORMAT"), "04/11/2011 02:05")
|
|
||||||
|
|
||||||
t = datetime.time(hour=16, minute=2, second=25)
|
|
||||||
self.assertEqual(time_format(t), "16:02")
|
|
||||||
|
|
||||||
# def test_short_month_day_format(self):
|
|
||||||
# update_en_gb_date_formats()
|
|
||||||
# dt = datetime.datetime(year=2021, month=7, day=31, hour=5, minute=5,
|
|
||||||
# second=5)
|
|
||||||
# self.assertEqual(date_format(dt, 'SHORT_MONTH_DAY_FORMAT'), '31 Jul')
|
|
|
@ -1,67 +0,0 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
import datetime
|
|
||||||
|
|
||||||
from django.core.exceptions import ValidationError
|
|
||||||
from django.forms.fields import DateTimeField
|
|
||||||
from django.test import TestCase, override_settings, tag
|
|
||||||
from django.utils.formats import date_format, time_format
|
|
||||||
|
|
||||||
from babybuddy.middleware import update_en_us_date_formats
|
|
||||||
|
|
||||||
|
|
||||||
class FormatsTestCase(TestCase):
|
|
||||||
def test_datetime_input_formats(self):
|
|
||||||
update_en_us_date_formats()
|
|
||||||
field = DateTimeField()
|
|
||||||
supported_custom_examples = [
|
|
||||||
"01/20/2020 9:30 AM",
|
|
||||||
"01/20/2020 9:30:03 AM",
|
|
||||||
"10/01/2020 11:30 PM",
|
|
||||||
"10/01/2020 11:30:03 AM",
|
|
||||||
]
|
|
||||||
|
|
||||||
for example in supported_custom_examples:
|
|
||||||
try:
|
|
||||||
result = field.to_python(example)
|
|
||||||
self.assertIsInstance(result, datetime.datetime)
|
|
||||||
except ValidationError:
|
|
||||||
self.fail('Format of "{}" not recognized!'.format(example))
|
|
||||||
|
|
||||||
with self.assertRaises(ValidationError):
|
|
||||||
field.to_python("invalid date string!")
|
|
||||||
|
|
||||||
@tag("isolate")
|
|
||||||
@override_settings(LANGUAGE_CODE="en-US", USE_24_HOUR_TIME_FORMAT=True)
|
|
||||||
def test_use_24_hour_time_format(self):
|
|
||||||
update_en_us_date_formats()
|
|
||||||
field = DateTimeField()
|
|
||||||
supported_custom_examples = [
|
|
||||||
"10/25/2006 2:30:59",
|
|
||||||
"10/25/2006 2:30",
|
|
||||||
"10/25/2006 14:30:59",
|
|
||||||
"10/25/2006 14:30",
|
|
||||||
]
|
|
||||||
|
|
||||||
for example in supported_custom_examples:
|
|
||||||
try:
|
|
||||||
result = field.to_python(example)
|
|
||||||
self.assertIsInstance(result, datetime.datetime)
|
|
||||||
except ValidationError:
|
|
||||||
self.fail('Format of "{}" not recognized!'.format(example))
|
|
||||||
|
|
||||||
with self.assertRaises(ValidationError):
|
|
||||||
field.to_python("invalid date string!")
|
|
||||||
|
|
||||||
dt = datetime.datetime(year=2011, month=11, day=4, hour=23, minute=5, second=59)
|
|
||||||
self.assertEqual(date_format(dt, "DATETIME_FORMAT"), "Nov. 4, 2011, 23:05:59")
|
|
||||||
|
|
||||||
dt = datetime.datetime(year=2011, month=11, day=4, hour=2, minute=5, second=59)
|
|
||||||
self.assertEqual(date_format(dt, "SHORT_DATETIME_FORMAT"), "11/04/2011 2:05:59")
|
|
||||||
|
|
||||||
t = datetime.time(hour=16, minute=2, second=25)
|
|
||||||
self.assertEqual(time_format(t), "16:02:25")
|
|
||||||
|
|
||||||
def test_short_month_day_format(self):
|
|
||||||
update_en_us_date_formats()
|
|
||||||
dt = datetime.datetime(year=2021, month=7, day=31, hour=5, minute=5, second=5)
|
|
||||||
self.assertEqual(date_format(dt, "SHORT_MONTH_DAY_FORMAT"), "Jul 31")
|
|
|
@ -144,9 +144,3 @@ class TemplateTagsTestCase(TestCase):
|
||||||
formats.date_format(date, format="TIME_FORMAT"),
|
formats.date_format(date, format="TIME_FORMAT"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
date = timezone.localtime() - timezone.timedelta(days=500)
|
|
||||||
self.assertEqual(
|
|
||||||
datetime.datetime_short(date),
|
|
||||||
formats.date_format(date, format="SHORT_DATETIME_FORMAT"),
|
|
||||||
)
|
|
||||||
|
|
|
@ -48,12 +48,3 @@ the user settings form.
|
||||||
**See also**
|
**See also**
|
||||||
|
|
||||||
[List of tz database time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
|
[List of tz database time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
|
||||||
|
|
||||||
## `USE_24_HOUR_TIME_FORMAT`
|
|
||||||
|
|
||||||
*Default:* `False`
|
|
||||||
|
|
||||||
Whether to force 24-hour time format for locales that do not ordinarily use it
|
|
||||||
(e.g. `en`). Support for this feature must be implemented on a per-locale basis.
|
|
||||||
See format files under [`babybuddy/formats`](https://github.com/babybuddy/babybuddy/tree/master/babybuddy/formats)
|
|
||||||
for supported locales.
|
|
||||||
|
|
|
@ -73,7 +73,6 @@ module.exports = {
|
||||||
},
|
},
|
||||||
testsConfig: {
|
testsConfig: {
|
||||||
isolated: [
|
isolated: [
|
||||||
'babybuddy.tests.formats.tests_en_us.FormatsTestCase.test_use_24_hour_time_format',
|
|
||||||
'babybuddy.tests.tests_views.ViewsTestCase.test_password_reset'
|
'babybuddy.tests.tests_views.ViewsTestCase.test_password_reset'
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue