From 5a5ce4d736bf7cd3030767de19872948b755dfc3 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sun, 7 Jan 2024 17:17:14 +0800 Subject: [PATCH] moved theming functions to main tag --- cookbook/templates/base.html | 6 ++-- cookbook/templatetags/theming_tags.py | 40 +++++++++++++++++++++++---- recipes/settings.py | 2 +- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/cookbook/templates/base.html b/cookbook/templates/base.html index 6c5f425d..3c1a92e0 100644 --- a/cookbook/templates/base.html +++ b/cookbook/templates/base.html @@ -29,9 +29,9 @@ - - {% if request.user.is_authenticated and request.space.custom_space_theme %} - + + {% if theme_values.custom_theme %} + {% endif %} diff --git a/cookbook/templatetags/theming_tags.py b/cookbook/templatetags/theming_tags.py index 97bf48d7..1bf56953 100644 --- a/cookbook/templatetags/theming_tags.py +++ b/cookbook/templatetags/theming_tags.py @@ -10,6 +10,14 @@ register = template.Library() @register.simple_tag def theme_values(request): + themes = { + UserPreference.BOOTSTRAP: 'themes/bootstrap.min.css', + UserPreference.FLATLY: 'themes/flatly.min.css', + UserPreference.DARKLY: 'themes/darkly.min.css', + UserPreference.SUPERHERO: 'themes/superhero.min.css', + UserPreference.TANDOOR: 'themes/tandoor.min.css', + UserPreference.TANDOOR_DARK: 'themes/tandoor_dark.min.css', + } # TODO move all theming values to this tag to prevent double queries tv = { 'logo_color_32': static('assets/logo_color_32.png'), @@ -19,6 +27,8 @@ def theme_values(request): 'logo_color_192': static('assets/logo_color_192.png'), 'logo_color_512': static('assets/logo_color_512.png'), 'logo_color_svg': static('assets/logo_color_svg.svg'), + 'custom_theme': None, + 'theme': static(themes[UserPreference.TANDOOR]) } space = None if request.space: @@ -32,6 +42,24 @@ def theme_values(request): if logo.startswith('logo_color_') and getattr(space, logo, None): tv[logo] = getattr(space, logo).file.url + with scopes_disabled(): + if not request.user.is_authenticated and UNAUTHENTICATED_THEME_FROM_SPACE > 0: + with scopes_disabled(): + space = Space.objects.filter(id=UNAUTHENTICATED_THEME_FROM_SPACE).first() + if space: + if space.custom_space_theme: + tv['custom_theme'] = space.custom_space_theme.file.url + if space.space_theme in themes: + return static(themes[space.space_theme]) + + if request.user.is_authenticated: + if request.space.custom_space_theme: + tv['custom_theme'] = request.space.custom_space_theme.file.url + if request.space.space_theme in themes: + tv['theme'] = themes[request.space.space_theme] + else: + tv['theme'] = themes[request.user.userpreference.theme] + return tv @@ -54,14 +82,14 @@ def theme_url(request): return static(themes[theme]) else: return static('themes/tandoor.min.css') - - if request.space.space_theme in themes: - return static(themes[request.space.space_theme]) else: - if request.user.userpreference.theme in themes: - return static(themes[request.user.userpreference.theme]) + if request.space.space_theme in themes: + return static(themes[request.space.space_theme]) else: - raise AttributeError + if request.user.userpreference.theme in themes: + return static(themes[request.user.userpreference.theme]) + else: + return static('themes/tandoor.min.css') @register.simple_tag diff --git a/recipes/settings.py b/recipes/settings.py index 644bf8a4..a22cd394 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -57,7 +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)) +UNAUTHENTICATED_THEME_FROM_SPACE = 2 #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(