From d33d922e5ec36e9f26cccbbda07aedb2168715c0 Mon Sep 17 00:00:00 2001
From: PelleK <elfjes@users.noreply.github.com>
Date: Tue, 12 Nov 2024 13:38:04 +0100
Subject: [PATCH] Add THEME_DEFAULT setting

.. changeable in settings file and via envvar.
---
 src/argus_htmx/constants.py               | 3 ++-
 src/argus_htmx/settings.py                | 3 ++-
 src/argus_htmx/themes/constants.py        | 4 +++-
 src/argus_htmx/themes/utils.py            | 4 ++++
 src/argus_htmx/user/preferences/models.py | 8 ++------
 5 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/argus_htmx/constants.py b/src/argus_htmx/constants.py
index 35ba71dd..24fc8dae 100644
--- a/src/argus_htmx/constants.py
+++ b/src/argus_htmx/constants.py
@@ -1,6 +1,6 @@
 from argus_htmx.dateformat.constants import DATETIME_DEFAULT, DATETIME_FORMATS, DATETIME_CHOICES
 from argus_htmx.incidents.constants import DEFAULT_PAGE_SIZE, ALLOWED_PAGE_SIZES, PAGE_SIZE_CHOICES
-from argus_htmx.themes.constants import THEME_CHOICES, THEME_NAMES
+from argus_htmx.themes.constants import THEME_CHOICES, THEME_NAMES, THEME_DEFAULT
 
 
 __all__ = [
@@ -11,5 +11,6 @@
     "DEFAULT_PAGE_SIZE",
     "PAGE_SIZE_CHOICES",
     "THEME_CHOICES",
+    "THEME_DEFAULT",
     "THEME_NAMES",
 ]
diff --git a/src/argus_htmx/settings.py b/src/argus_htmx/settings.py
index c00f7f74..d72f9f81 100644
--- a/src/argus_htmx/settings.py
+++ b/src/argus_htmx/settings.py
@@ -2,7 +2,7 @@
 # items in INCIDENT_TABLE_COLUMNS can be either a `str` referring to a key in
 # argus_htmx.incidents.customization.BUILTIN_COLUMNS or an instance of
 # argus_htmx.incidents.customization.IncidentTableColumn
-from argus.site.settings import get_json_env
+from argus.site.settings import get_json_env, get_str_env
 
 INCIDENT_TABLE_COLUMNS = [
     "row_select",
@@ -54,5 +54,6 @@
     },
 ]
 DAISYUI_THEMES = get_json_env("DAISYUI_THEMES", DEFAULT_THEMES, quiet=True)
+THEME_DEFAULT = get_str_env("ARGUS_THEME_DEFAULT", "argus")
 DEFAULT_THEME_OVERRIDE = {}
 TAILWIND_THEME_OVERRIDE = get_json_env("TAILWIND_THEME_OVERRIDE", DEFAULT_THEME_OVERRIDE, quiet=True)
diff --git a/src/argus_htmx/themes/constants.py b/src/argus_htmx/themes/constants.py
index bf43dae7..46f1a72e 100644
--- a/src/argus_htmx/themes/constants.py
+++ b/src/argus_htmx/themes/constants.py
@@ -1,11 +1,13 @@
-from argus_htmx.themes.utils import get_theme_names
+from argus_htmx.themes.utils import get_theme_default, get_theme_names
 
 
 __all__ = [
     "THEME_CHOICES",
+    "THEME_DEFAULT",
     "THEME_NAMES",
 ]
 
 
 THEME_NAMES = sorted(get_theme_names())
 THEME_CHOICES = tuple((theme, theme) for theme in THEME_NAMES)
+THEME_DEFAULT = get_theme_default()
diff --git a/src/argus_htmx/themes/utils.py b/src/argus_htmx/themes/utils.py
index dd601668..9927bb5a 100644
--- a/src/argus_htmx/themes/utils.py
+++ b/src/argus_htmx/themes/utils.py
@@ -15,3 +15,7 @@ def get_theme_names():
         elif isinstance(theme, dict):
             theme_names.extend(theme.keys())
     return theme_names
+
+
+def get_theme_default():
+    return getattr(settings, "THEME_DEFAULT", argus_htmx_settings.THEME_DEFAULT)
diff --git a/src/argus_htmx/user/preferences/models.py b/src/argus_htmx/user/preferences/models.py
index 294d3fc7..f02af767 100644
--- a/src/argus_htmx/user/preferences/models.py
+++ b/src/argus_htmx/user/preferences/models.py
@@ -9,22 +9,17 @@
     PAGE_SIZE_CHOICES,
     DEFAULT_PAGE_SIZE,
     THEME_CHOICES,
+    THEME_DEFAULT,
 )
 
 
 class DateTimeFormatForm(forms.Form):
     datetime_format_name = forms.ChoiceField(required=False, choices=DATETIME_CHOICES)
 
-    def clean_datetime_format_name(self):
-        return self.cleaned_data.get("datetime_format_name", DATETIME_DEFAULT) or DATETIME_DEFAULT
-
 
 class PageSizeForm(forms.Form):
     page_size = forms.TypedChoiceField(required=False, choices=PAGE_SIZE_CHOICES, coerce=int)
 
-    def clean_page_size(self):
-        return self.cleaned_data.get("page_size", DEFAULT_PAGE_SIZE) or DEFAULT_PAGE_SIZE
-
 
 class ThemeForm(forms.Form):
     theme = forms.ChoiceField(choices=THEME_CHOICES)
@@ -40,6 +35,7 @@ class ArgusHtmxPreferences(Preferences):
     _FIELD_DEFAULTS = {
         "datetime_format_name": DATETIME_DEFAULT,
         "page_size": DEFAULT_PAGE_SIZE,
+        "theme": THEME_DEFAULT,
     }
 
     class Meta: