-
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
- Loading branch information
Showing
7 changed files
with
80 additions
and
15 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,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 |
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,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) |