added ability to change unauthenticated theme

This commit is contained in:
vabene1111
2024-01-06 21:43:40 +08:00
parent ac5333d0e7
commit 953dc75a8d
3 changed files with 50 additions and 11 deletions

View File

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