Skip to content

Commit

Permalink
Make themes list depend on installed themes
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Nov 13, 2024
1 parent d1ddb2a commit e77e0c8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 15 deletions.
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ 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 ``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
Expand Down
1 change: 1 addition & 0 deletions src/argus_htmx/appconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"context_processors": [
"argus.auth.context_processors.preferences",
"argus_htmx.context_processors.path_to_stylesheet",
],
"middleware": {
"argus_htmx.middleware.LoginRequiredMiddleware": "end",
Expand Down
5 changes: 5 additions & 0 deletions src/argus_htmx/apps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pathlib

from django.apps import AppConfig


Expand All @@ -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
30 changes: 30 additions & 0 deletions src/argus_htmx/checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.core.checks import Error, register
from django.core.exceptions import ImproperlyConfigured

from .themes.utils import get_theme_names


@register
def check_for_valid_themes_list(app_configs, **kwargs):
errors = []
themes = []
try:
themes = get_theme_names()
except ImproperlyConfigured as e:
errors.append(
Error(
str(e),
hint="Regenerate styles.css",
id="argus_htmx.T001",
)
)
else:
if not themes:
errors.append(
Error(
"no themes installed",
hint='Check the settings "DAISYUI_THEMES" and "TAILWIND_THEME_OVERRIDE" and regenerate styles.css',
id="argus_htmx.T002",
)
)
return errors
10 changes: 3 additions & 7 deletions src/argus_htmx/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
2 changes: 2 additions & 0 deletions src/argus_htmx/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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("STYLESHEET_PATH", STYLESHEET_PATH_DEFAULT)
43 changes: 36 additions & 7 deletions src/argus_htmx/themes/utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
from importlib.resources import files
from pathlib import Path

from django.conf import settings
from argus_htmx import settings as argus_htmx_settings
from django.core.exceptions import ImproperlyConfigured

from argus_htmx import settings as default_htmx_settings

def get_themes():
return getattr(settings, "DAISYUI_THEMES", argus_htmx_settings.DAISYUI_THEMES)

__all__ = ["get_theme_names"]

def get_theme_names():
themes = get_themes()

def get_themes_from_setting():
themes_setting = getattr(settings, "DAISYUI_THEMES", default_htmx_settings.DAISYUI_THEMES)
theme_names = []
for theme in themes:
for theme in themes_setting:
if isinstance(theme, str):
theme_names.append(theme)
elif isinstance(theme, dict):
theme_names.extend(theme.keys())
return theme_names


def get_themes_from_css():
static_url = Path(settings.STATIC_URL).relative_to("/")
stylesheet_path = static_url / default_htmx_settings.STYLESHEET_PATH
styles_css = files("argus_htmx").joinpath(stylesheet_path).read_text()
styles_css_lines = styles_css.split("{")
theme_names = []
for line in styles_css_lines:
if "data-theme=" not in line:
continue
_, after = line.split("=", 1)
theme_name, _ = after.split("]", 1)
theme_names.append(theme_name.strip())
return theme_names


def get_theme_names():
themes_from_setting = set(get_themes_from_setting())
themes_from_css = set(get_themes_from_css())
installed_themes = themes_from_setting | themes_from_css
all_themes = themes_from_setting & themes_from_css
if all_themes != installed_themes:
raise ImproperlyConfigured("Themes in settings is out of sync with themes installed")
return installed_themes


def get_theme_default():
return getattr(settings, "THEME_DEFAULT", argus_htmx_settings.THEME_DEFAULT)
return getattr(settings, "THEME_DEFAULT", default_htmx_settings.THEME_DEFAULT)

0 comments on commit e77e0c8

Please sign in to comment.