moved theming functions to main tag

This commit is contained in:
vabene1111 2024-01-07 17:17:14 +08:00
parent 1dda4126c1
commit 5a5ce4d736
3 changed files with 38 additions and 10 deletions

View File

@ -29,9 +29,9 @@
<meta name="apple-mobile-web-app-capable" content="yes"/>
<!-- Bootstrap 4 -->
<link id="id_main_css" href="{% theme_url request %}" rel="stylesheet">
{% if request.user.is_authenticated and request.space.custom_space_theme %}
<link id="id_custom_css" href="{% custom_theme request %}" rel="stylesheet">
<link id="id_main_css" href="{{ theme_values.theme }}" rel="stylesheet">
{% if theme_values.custom_theme %}
<link id="id_custom_css" href="{{ theme_values.custom_theme }}" rel="stylesheet">
{% endif %}

View File

@ -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

View File

@ -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(