From 953dc75a8d7fa75055628bfcad4f75fd64646e5d Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sat, 6 Jan 2024 21:43:40 +0800 Subject: [PATCH] added ability to change unauthenticated theme --- cookbook/templatetags/theming_tags.py | 38 ++++++++++++++++++++++----- docs/system/configuration.md | 10 +++++++ recipes/settings.py | 13 ++++++--- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/cookbook/templatetags/theming_tags.py b/cookbook/templatetags/theming_tags.py index ec35b2f0..e110f6a6 100644 --- a/cookbook/templatetags/theming_tags.py +++ b/cookbook/templatetags/theming_tags.py @@ -3,15 +3,13 @@ from django.templatetags.static import static from django_scopes import scopes_disabled from cookbook.models import UserPreference, UserFile, Space -from recipes.settings import STICKY_NAV_PREF_DEFAULT +from recipes.settings import STICKY_NAV_PREF_DEFAULT, UNAUTHENTICATED_THEME_FROM_SPACE register = template.Library() @register.simple_tag def theme_url(request): - if not request.user.is_authenticated: - return static('themes/tandoor.min.css') themes = { UserPreference.BOOTSTRAP: 'themes/bootstrap.min.css', UserPreference.FLATLY: 'themes/flatly.min.css', @@ -20,8 +18,13 @@ def theme_url(request): UserPreference.TANDOOR: 'themes/tandoor.min.css', UserPreference.TANDOOR_DARK: 'themes/tandoor_dark.min.css', } - # if request.space.custom_space_theme: - # return request.space.custom_space_theme.file.url + + if not request.user.is_authenticated: + if UNAUTHENTICATED_THEME_FROM_SPACE > 0: # TODO load unauth space setting on boot in settings.py and use them here + with scopes_disabled(): + return static(themes[Space.objects.filter(id=UNAUTHENTICATED_THEME_FROM_SPACE).first().space_theme]) + else: + return static('themes/tandoor.min.css') if request.space.space_theme in themes: return static(themes[request.space.space_theme]) @@ -36,10 +39,19 @@ def theme_url(request): def custom_theme(request): if request.user.is_authenticated and request.space.custom_space_theme: return request.space.custom_space_theme.file.url + elif UNAUTHENTICATED_THEME_FROM_SPACE > 0: + with scopes_disabled(): + return Space.objects.filter(id=UNAUTHENTICATED_THEME_FROM_SPACE).first().custom_space_theme.file.url @register.simple_tag def logo_url(request): + if not request.user.is_authenticated: + if UNAUTHENTICATED_THEME_FROM_SPACE > 0: + with scopes_disabled(): + space = Space.objects.filter(id=UNAUTHENTICATED_THEME_FROM_SPACE).first() + if getattr(space, 'nav_logo', None): + return space.nav_logo.file.url if request.user.is_authenticated and getattr(getattr(request, "space", {}), 'nav_logo', None): return request.space.nav_logo.file.url else: @@ -49,6 +61,11 @@ def logo_url(request): @register.simple_tag def nav_bg_color(request): if not request.user.is_authenticated: + if UNAUTHENTICATED_THEME_FROM_SPACE > 0: + with scopes_disabled(): + space = Space.objects.filter(id=UNAUTHENTICATED_THEME_FROM_SPACE).first() + if space.nav_bg_color: + return space.nav_bg_color return '#ddbf86' else: if request.space.nav_bg_color: @@ -59,8 +76,14 @@ def nav_bg_color(request): @register.simple_tag def nav_text_color(request): - type_mapping = {Space.DARK: 'navbar-light', Space.LIGHT: 'navbar-dark'} # inverted since navbar-dark means the background + type_mapping = {Space.DARK: 'navbar-light', + Space.LIGHT: 'navbar-dark'} # inverted since navbar-dark means the background if not request.user.is_authenticated: + if UNAUTHENTICATED_THEME_FROM_SPACE > 0: + with scopes_disabled(): + space = Space.objects.filter(id=UNAUTHENTICATED_THEME_FROM_SPACE).first() + if space.nav_text_color: + return type_mapping[space.nav_text_color] return 'navbar-dark' else: if request.space.nav_text_color != Space.BLANK: @@ -71,7 +94,8 @@ def nav_text_color(request): @register.simple_tag def sticky_nav(request): - if (not request.user.is_authenticated and STICKY_NAV_PREF_DEFAULT) or (request.user.is_authenticated and request.user.userpreference.nav_sticky): + if (not request.user.is_authenticated and STICKY_NAV_PREF_DEFAULT) or ( + request.user.is_authenticated and request.user.userpreference.nav_sticky): return 'position: sticky; top: 0; left: 0; z-index: 1000;' else: return '' diff --git a/docs/system/configuration.md b/docs/system/configuration.md index ad0c8107..c2351134 100644 --- a/docs/system/configuration.md +++ b/docs/system/configuration.md @@ -538,6 +538,16 @@ Usually everything is converted to the users timezone so this setting doesn't re TZ=Europe/Berlin ``` +#### Default Theme +> default `0` - options `1-X` (space ID) + +Tandoors appearance can be changed on a user and space level but unauthenticated users always see the tandoor default style. +With this setting you can specify the ID of a space of which the appearance settings should be applied if a user is not logged in. + +``` +UNAUTHENTICATED_THEME_FROM_SPACE= +``` + ### Rate Limiting / Performance #### Shopping auto sync diff --git a/recipes/settings.py b/recipes/settings.py index 0a68e63c..644bf8a4 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -57,6 +57,7 @@ COMMENT_PREF_DEFAULT = bool(int(os.getenv('COMMENT_PREF_DEFAULT', True))) FRACTION_PREF_DEFAULT = bool(int(os.getenv('FRACTION_PREF_DEFAULT', False))) KJ_PREF_DEFAULT = bool(int(os.getenv('KJ_PREF_DEFAULT', False))) STICKY_NAV_PREF_DEFAULT = bool(int(os.getenv('STICKY_NAV_PREF_DEFAULT', True))) +UNAUTHENTICATED_THEME_FROM_SPACE = int(os.getenv('UNAUTHENTICATED_THEME_FROM_SPACE', 0)) # minimum interval that users can set for automatic sync of shopping lists SHOPPING_MIN_AUTOSYNC_INTERVAL = int( @@ -69,7 +70,8 @@ if os.getenv('CSRF_TRUSTED_ORIGINS'): CSRF_TRUSTED_ORIGINS = os.getenv('CSRF_TRUSTED_ORIGINS').split(',') if CORS_ORIGIN_ALLOW_ALL := os.getenv('CORS_ORIGIN_ALLOW_ALL') is not None: - print('DEPRECATION WARNING: Environment var "CORS_ORIGIN_ALLOW_ALL" is deprecated. Please use "CORS_ALLOW_ALL_ORIGINS."') + print( + 'DEPRECATION WARNING: Environment var "CORS_ORIGIN_ALLOW_ALL" is deprecated. Please use "CORS_ALLOW_ALL_ORIGINS."') CORS_ALLOW_ALL_ORIGINS = CORS_ORIGIN_ALLOW_ALL else: CORS_ALLOW_ALL_ORIGINS = bool(int(os.getenv("CORS_ALLOW_ALL_ORIGINS", True))) @@ -158,7 +160,8 @@ try: INSTALLED_APPS.append(plugin_module) plugin_config = { - 'name': plugin_class.verbose_name if hasattr(plugin_class, 'verbose_name') else plugin_class.name, + 'name': plugin_class.verbose_name if hasattr(plugin_class, + 'verbose_name') else plugin_class.name, 'version': plugin_class.VERSION if hasattr(plugin_class, 'VERSION') else 'unknown', 'website': plugin_class.website if hasattr(plugin_class, 'website') else '', 'github': plugin_class.github if hasattr(plugin_class, 'github') else '', @@ -166,7 +169,8 @@ try: 'base_path': os.path.join(BASE_DIR, 'recipes', 'plugins', d), 'base_url': plugin_class.base_url, 'bundle_name': plugin_class.bundle_name if hasattr(plugin_class, 'bundle_name') else '', - 'api_router_name': plugin_class.api_router_name if hasattr(plugin_class, 'api_router_name') else '', + 'api_router_name': plugin_class.api_router_name if hasattr(plugin_class, + 'api_router_name') else '', 'nav_main': plugin_class.nav_main if hasattr(plugin_class, 'nav_main') else '', 'nav_dropdown': plugin_class.nav_dropdown if hasattr(plugin_class, 'nav_dropdown') else '', } @@ -256,7 +260,8 @@ if LDAP_AUTH: ldap.SCOPE_SUBTREE, os.getenv('AUTH_LDAP_USER_SEARCH_FILTER_STR', '(uid=%(user)s)'), ) - AUTH_LDAP_USER_ATTR_MAP = ast.literal_eval(os.getenv('AUTH_LDAP_USER_ATTR_MAP')) if os.getenv('AUTH_LDAP_USER_ATTR_MAP') else { + AUTH_LDAP_USER_ATTR_MAP = ast.literal_eval(os.getenv('AUTH_LDAP_USER_ATTR_MAP')) if os.getenv( + 'AUTH_LDAP_USER_ATTR_MAP') else { 'first_name': 'givenName', 'last_name': 'sn', 'email': 'mail',