diff --git a/README.rst b/README.rst index ccbb285b..af8aca80 100644 --- a/README.rst +++ b/README.rst @@ -242,7 +242,10 @@ How to customize the look: * `list of daisyUI color names`_ * `Tailwind CSS theme customization`_ -* Override the default main stylesheet path by providing a ``path_to_stylesheet`` value in a template ``context``. +* Override the default main stylesheet path by setting + ``ARGUS_STYLESHEET_PATH`` in the environment. The path is under + ``STATIC_URL``. This depends on the context processor + ``argus_htmx.context_processors.path_to_stylesheet``. * Include additional styles/stylesheets using the ``head`` block in your templates. * Generate a Tailwind config file by running the ``tailwind_config`` management command. By default the generated file will be based on diff --git a/src/argus_htmx/appconfig.py b/src/argus_htmx/appconfig.py index 62be90b2..534e6015 100644 --- a/src/argus_htmx/appconfig.py +++ b/src/argus_htmx/appconfig.py @@ -25,6 +25,7 @@ }, "context_processors": [ "argus.auth.context_processors.preferences", + "argus_htmx.context_processors.path_to_stylesheet", ], "middleware": { "argus_htmx.middleware.LoginRequiredMiddleware": "end", diff --git a/src/argus_htmx/apps.py b/src/argus_htmx/apps.py index cefe9aad..cc6169b5 100644 --- a/src/argus_htmx/apps.py +++ b/src/argus_htmx/apps.py @@ -1,4 +1,5 @@ import pathlib + from django.apps import AppConfig @@ -9,3 +10,7 @@ class HtmxFrontendConfig(AppConfig): def tailwind_css_files(self): yield from pathlib.Path(__file__).parent.glob("tailwindtheme/snippets/*.css") + + def ready(self): + # Register checks + from .checks import check_for_valid_themes_list # noqa: F401 diff --git a/src/argus_htmx/checks.py b/src/argus_htmx/checks.py new file mode 100644 index 00000000..6e55ad10 --- /dev/null +++ b/src/argus_htmx/checks.py @@ -0,0 +1,29 @@ +from django.core.checks import Error, Warning, register +from django.core.exceptions import ImproperlyConfigured + +from .themes.utils import get_theme_names, get_stylesheet_path + + +@register +def check_for_valid_themes_list(app_configs, **kwargs): + styles_path = get_stylesheet_path() + themes = [] + try: + themes = get_theme_names(quiet=False) + except ImproperlyConfigured as e: + return [ + Warning( + str(e), + hint=f"Regenerate {styles_path}", + id="argus_htmx.T001", + ) + ] + if not themes: + return [ + Error( + "no themes installed", + hint=f'Check the settings "DAISYUI_THEMES" and "TAILWIND_THEME_OVERRIDE" and regenerate {styles_path}', + id="argus_htmx.T002", + ) + ] + return [] diff --git a/src/argus_htmx/context_processors.py b/src/argus_htmx/context_processors.py index 67ccdd68..3f2b002e 100644 --- a/src/argus_htmx/context_processors.py +++ b/src/argus_htmx/context_processors.py @@ -7,12 +7,8 @@ See django settings for ``TEMPLATES``. """ -from argus.auth.models import Preferences +from .settings import STYLESHEET_PATH -def preferences(request): - pref_sets = Preferences.objects.filter(user=request.user) - prefdict = {} - for pref_set in pref_sets: - prefdict[pref_set._namespace] = pref_set.get_context() - return {"preferences": prefdict} +def path_to_stylesheet(request): + return {"path_to_stylesheet": STYLESHEET_PATH} diff --git a/src/argus_htmx/management/commands/tailwind_config.py b/src/argus_htmx/management/commands/tailwind_config.py index 0f4f68c1..a01d1408 100644 --- a/src/argus_htmx/management/commands/tailwind_config.py +++ b/src/argus_htmx/management/commands/tailwind_config.py @@ -8,7 +8,7 @@ from django.template.context import make_context from django.template.loader import get_template -from argus_htmx.themes.utils import get_themes +from argus_htmx.themes.utils import get_raw_themes_setting from argus_htmx import settings as argus_htmx_settings @@ -78,7 +78,7 @@ def get_context(self, target_dir: pathlib.Path): argus_htmx_settings.TAILWIND_THEME_OVERRIDE, ), "daisyuithemes": textwrap.indent( - json.dumps(get_themes(), indent=2), + json.dumps(get_raw_themes_setting(), indent=2), prefix=10 * " ", predicate=lambda line: line != "[\n", # this is kinda hacky, but eh ), diff --git a/src/argus_htmx/settings.py b/src/argus_htmx/settings.py index d72f9f81..00f14bb0 100644 --- a/src/argus_htmx/settings.py +++ b/src/argus_htmx/settings.py @@ -25,6 +25,7 @@ TAILWIND_CONFIG_TARGET = "src/argus_htmx/tailwindtheme/tailwind.config.js" TAILWIND_CSS_TARGET = "src/argus_htmx/tailwindtheme/styles.css" +STYLESHEET_PATH_DEFAULT = "styles.css" DEFAULT_THEMES = [ "dark", "light", @@ -57,3 +58,4 @@ 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) +STYLESHEET_PATH = get_str_env("ARGUS_STYLESHEET_PATH", STYLESHEET_PATH_DEFAULT) diff --git a/src/argus_htmx/templates/htmx/user/preferences.html b/src/argus_htmx/templates/htmx/user/preferences.html index 32d625de..c81ebec6 100644 --- a/src/argus_htmx/templates/htmx/user/preferences.html +++ b/src/argus_htmx/templates/htmx/user/preferences.html @@ -9,7 +9,7 @@