From 5875ef9658e85c8988a56cd3cd0ac4a5781eb05a Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sat, 25 Mar 2023 14:29:35 -0700 Subject: [PATCH] Remove support for 24H time format override --- babybuddy/formats/{pt => }/__init__.py | 0 babybuddy/formats/{tr => en}/__init__.py | 0 babybuddy/formats/en/formats.py | 5 ++ babybuddy/formats/pt/formats.py | 11 ---- babybuddy/formats/tr/formats.py | 9 --- babybuddy/middleware.py | 64 ---------------------- babybuddy/settings/base.py | 10 ---- babybuddy/tests/formats/tests_en.py | 11 ++++ babybuddy/tests/formats/tests_en_gb.py | 70 ------------------------ babybuddy/tests/formats/tests_en_us.py | 67 ----------------------- core/tests/tests_templatetags.py | 6 -- docs/configuration/application.md | 9 --- gulpfile.config.js | 1 - 13 files changed, 16 insertions(+), 247 deletions(-) rename babybuddy/formats/{pt => }/__init__.py (100%) rename babybuddy/formats/{tr => en}/__init__.py (100%) create mode 100644 babybuddy/formats/en/formats.py delete mode 100644 babybuddy/formats/pt/formats.py delete mode 100644 babybuddy/formats/tr/formats.py create mode 100644 babybuddy/tests/formats/tests_en.py delete mode 100644 babybuddy/tests/formats/tests_en_gb.py delete mode 100644 babybuddy/tests/formats/tests_en_us.py diff --git a/babybuddy/formats/pt/__init__.py b/babybuddy/formats/__init__.py similarity index 100% rename from babybuddy/formats/pt/__init__.py rename to babybuddy/formats/__init__.py diff --git a/babybuddy/formats/tr/__init__.py b/babybuddy/formats/en/__init__.py similarity index 100% rename from babybuddy/formats/tr/__init__.py rename to babybuddy/formats/en/__init__.py diff --git a/babybuddy/formats/en/formats.py b/babybuddy/formats/en/formats.py new file mode 100644 index 00000000..a3556b47 --- /dev/null +++ b/babybuddy/formats/en/formats.py @@ -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' diff --git a/babybuddy/formats/pt/formats.py b/babybuddy/formats/pt/formats.py deleted file mode 100644 index 8b32c55e..00000000 --- a/babybuddy/formats/pt/formats.py +++ /dev/null @@ -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 diff --git a/babybuddy/formats/tr/formats.py b/babybuddy/formats/tr/formats.py deleted file mode 100644 index 795d929e..00000000 --- a/babybuddy/formats/tr/formats.py +++ /dev/null @@ -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, -] diff --git a/babybuddy/middleware.py b/babybuddy/middleware.py index 4e7fd9e2..0d42cb8c 100644 --- a/babybuddy/middleware.py +++ b/babybuddy/middleware.py @@ -5,68 +5,9 @@ import pytz from django.conf import settings 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 -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: """ Customizes settings based on user language setting. @@ -85,11 +26,6 @@ class UserLanguageMiddleware: language = settings.LANGUAGE_CODE 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. translation.activate(language) diff --git a/babybuddy/settings/base.py b/babybuddy/settings/base.py index 464cce02..bb80554a 100644 --- a/babybuddy/settings/base.py +++ b/babybuddy/settings/base.py @@ -200,16 +200,6 @@ USE_L10N = True 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) # https://docs.djangoproject.com/en/4.0/howto/static-files/ diff --git a/babybuddy/tests/formats/tests_en.py b/babybuddy/tests/formats/tests_en.py new file mode 100644 index 00000000..bd585fcb --- /dev/null +++ b/babybuddy/tests/formats/tests_en.py @@ -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") diff --git a/babybuddy/tests/formats/tests_en_gb.py b/babybuddy/tests/formats/tests_en_gb.py deleted file mode 100644 index 09648507..00000000 --- a/babybuddy/tests/formats/tests_en_gb.py +++ /dev/null @@ -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') diff --git a/babybuddy/tests/formats/tests_en_us.py b/babybuddy/tests/formats/tests_en_us.py deleted file mode 100644 index 0b346222..00000000 --- a/babybuddy/tests/formats/tests_en_us.py +++ /dev/null @@ -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") diff --git a/core/tests/tests_templatetags.py b/core/tests/tests_templatetags.py index d518e04a..99f41a0f 100644 --- a/core/tests/tests_templatetags.py +++ b/core/tests/tests_templatetags.py @@ -144,9 +144,3 @@ class TemplateTagsTestCase(TestCase): 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"), - ) diff --git a/docs/configuration/application.md b/docs/configuration/application.md index 9f9e4495..9a9f2ab5 100644 --- a/docs/configuration/application.md +++ b/docs/configuration/application.md @@ -48,12 +48,3 @@ the user settings form. **See also** [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. diff --git a/gulpfile.config.js b/gulpfile.config.js index bbd4c2f6..d9b5aac2 100644 --- a/gulpfile.config.js +++ b/gulpfile.config.js @@ -73,7 +73,6 @@ module.exports = { }, testsConfig: { isolated: [ - 'babybuddy.tests.formats.tests_en_us.FormatsTestCase.test_use_24_hour_time_format', 'babybuddy.tests.tests_views.ViewsTestCase.test_password_reset' ], },