Skip to content

Commit

Permalink
Add date-only and time-only formats to preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
elfjes authored and hmpf committed Nov 29, 2024
1 parent 36621ad commit 1eb2c3a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 15 additions & 4 deletions src/argus/htmx/dateformat/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dataclasses
from django.conf import settings

__all__ = [
Expand 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)

Expand Down
6 changes: 5 additions & 1 deletion src/argus/htmx/user/preferences/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit 1eb2c3a

Please sign in to comment.