-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make themes list depend on installed themes, not themes in settings. If they are out of sync it will be reported on startup with a chcek, and logged.
- Loading branch information
Showing
8 changed files
with
100 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) | ||
] | ||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
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) |