Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wash list of theme names #226

Merged
merged 13 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
29 changes: 29 additions & 0 deletions src/argus_htmx/checks.py
Original file line number Diff line number Diff line change
@@ -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",
)
]
hmpf marked this conversation as resolved.
Show resolved Hide resolved
return []
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}
4 changes: 2 additions & 2 deletions src/argus_htmx/management/commands/tailwind_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
),
Expand Down
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("ARGUS_STYLESHEET_PATH", STYLESHEET_PATH_DEFAULT)
2 changes: 1 addition & 1 deletion src/argus_htmx/templates/htmx/user/preferences.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h2 class="card-title">User preferences</h2>
Logged in as: <span class="text-info">{{ request.user }}</span>
<div class="divider divider-secondary my-0"></div>
</li>
<li>{% include "htmx/themes/theme_dropdown.html" %}</li>
<li>{% include "htmx/themes/_theme_dropdown.html" %}</li>
<li>{% include "htmx/dateformat/_dateformat_dropdown.html" %}</li>
<li>{% include "htmx/page_size/_page_size_dropdown.html" %}</li>
</ul>
Expand Down
61 changes: 54 additions & 7 deletions src/argus_htmx/themes/utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,68 @@
import logging
from pathlib import Path
from re import findall

from django.conf import settings
from argus_htmx import settings as argus_htmx_settings
from django.core.exceptions import ImproperlyConfigured
from django.contrib.staticfiles.finders import find

from argus_htmx import settings as fallbacks


__all__ = [
"get_raw_themes_setting",
"get_theme_names",
"get_theme_default",
]


LOG = logging.getLogger(__name__)

def get_themes():
return getattr(settings, "DAISYUI_THEMES", argus_htmx_settings.DAISYUI_THEMES)
hmpf marked this conversation as resolved.
Show resolved Hide resolved

def get_raw_themes_setting():
return getattr(settings, "DAISYUI_THEMES", fallbacks.DAISYUI_THEMES)

def get_theme_names():
themes = get_themes()

def get_themes_from_setting():
themes_setting = get_raw_themes_setting()
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_stylesheet_path():
return getattr(settings, "STYLESHEET_PATH", fallbacks.STYLESHEET_PATH)


def get_themes_from_css():
THEME_NAME_RE = "(?P<theme>[-_\w]+)"
DATA_THEME_RE = f"\[data-theme={THEME_NAME_RE}\]"

absolute_stylesheet_path = Path(find(get_stylesheet_path()))
styles_css = absolute_stylesheet_path.read_text()

return findall(DATA_THEME_RE, styles_css)


def get_theme_names(quiet=True):
ERROR_MSG = "Themes in settings are out of sync with themes installed"

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:
LOG.warning(ERROR_MSG)
if not quiet:
raise ImproperlyConfigured(ERROR_MSG)

return installed_themes


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