From 1eb2c3a71153b91ffa0a62b0d6f6d28dbc762f84 Mon Sep 17 00:00:00 2001 From: PelleK Date: Tue, 26 Nov 2024 11:24:11 +0100 Subject: [PATCH] Add date-only and time-only formats to preferences --- src/argus/htmx/dateformat/constants.py | 19 +++++++++++++++---- src/argus/htmx/user/preferences/models.py | 6 +++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/argus/htmx/dateformat/constants.py b/src/argus/htmx/dateformat/constants.py index 67de6361f..18d33b7c4 100644 --- a/src/argus/htmx/dateformat/constants.py +++ b/src/argus/htmx/dateformat/constants.py @@ -1,3 +1,4 @@ +import dataclasses from django.conf import settings __all__ = [ @@ -8,12 +9,22 @@ ] +@dataclasses.dataclass +class DateTimeFormat: + datetime: str + date: str + time: str + + DATETIME_FALLBACK = "LOCALE" + +# Datetime format specification can be found here: +# https://docs.djangoproject.com/en/5.1/ref/templates/builtins/#date DATETIME_FORMATS = { - "LOCALE": "DATETIME_FORMAT", # default - "ISO": "Y-m-d H:i:s", - "RFC5322": "r", - "EPOCH": "U", + "LOCALE": DateTimeFormat(datetime="DATETIME_FORMAT", date="DATE_FORMAT", time="TIME_FORMAT"), # default + "ISO": DateTimeFormat(datetime="Y-m-d H:i:s", date="Y-m-d", time="H:i:s"), + "RFC5322": DateTimeFormat("r", "r", "r"), + "EPOCH": DateTimeFormat("U", "U", "U"), } DATETIME_CHOICES = tuple((format, format) for format in DATETIME_FORMATS) diff --git a/src/argus/htmx/user/preferences/models.py b/src/argus/htmx/user/preferences/models.py index 9524c6784..36f8b78be 100644 --- a/src/argus/htmx/user/preferences/models.py +++ b/src/argus/htmx/user/preferences/models.py @@ -41,4 +41,8 @@ class ArgusHtmxPreferences: def update_context(self, context): datetime_format_name = context.get("datetime_format_name", DATETIME_DEFAULT) datetime_format = DATETIME_FORMATS[datetime_format_name] - return {"datetime_format": datetime_format} + return { + "datetime_format": datetime_format.datetime, + "date_format": datetime_format.date, + "time_format": datetime_format.time, + }