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 django_scopes import scopes_disabled
from cookbook.models import UserPreference, UserFile, Space 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 = template.Library()
@register.simple_tag @register.simple_tag
def theme_url(request): def theme_url(request):
if not request.user.is_authenticated:
return static('themes/tandoor.min.css')
themes = { themes = {
UserPreference.BOOTSTRAP: 'themes/bootstrap.min.css', UserPreference.BOOTSTRAP: 'themes/bootstrap.min.css',
UserPreference.FLATLY: 'themes/flatly.min.css', UserPreference.FLATLY: 'themes/flatly.min.css',
@ -20,8 +18,13 @@ def theme_url(request):
UserPreference.TANDOOR: 'themes/tandoor.min.css', UserPreference.TANDOOR: 'themes/tandoor.min.css',
UserPreference.TANDOOR_DARK: 'themes/tandoor_dark.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: if request.space.space_theme in themes:
return static(themes[request.space.space_theme]) return static(themes[request.space.space_theme])
@ -36,10 +39,19 @@ def theme_url(request):
def custom_theme(request): def custom_theme(request):
if request.user.is_authenticated and request.space.custom_space_theme: if request.user.is_authenticated and request.space.custom_space_theme:
return request.space.custom_space_theme.file.url 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 @register.simple_tag
def logo_url(request): 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): if request.user.is_authenticated and getattr(getattr(request, "space", {}), 'nav_logo', None):
return request.space.nav_logo.file.url return request.space.nav_logo.file.url
else: else:
@ -49,6 +61,11 @@ def logo_url(request):
@register.simple_tag @register.simple_tag
def nav_bg_color(request): def nav_bg_color(request):
if not request.user.is_authenticated: 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' return '#ddbf86'
else: else:
if request.space.nav_bg_color: if request.space.nav_bg_color:
@ -59,8 +76,14 @@ def nav_bg_color(request):
@register.simple_tag @register.simple_tag
def nav_text_color(request): 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 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' return 'navbar-dark'
else: else:
if request.space.nav_text_color != Space.BLANK: if request.space.nav_text_color != Space.BLANK:
@ -71,7 +94,8 @@ def nav_text_color(request):
@register.simple_tag @register.simple_tag
def sticky_nav(request): 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;' return 'position: sticky; top: 0; left: 0; z-index: 1000;'
else: else:
return '' return ''

View File

@ -538,6 +538,16 @@ Usually everything is converted to the users timezone so this setting doesn't re
TZ=Europe/Berlin 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 ### Rate Limiting / Performance
#### Shopping auto sync #### Shopping auto sync

View File

@ -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))) FRACTION_PREF_DEFAULT = bool(int(os.getenv('FRACTION_PREF_DEFAULT', False)))
KJ_PREF_DEFAULT = bool(int(os.getenv('KJ_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))) 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 # minimum interval that users can set for automatic sync of shopping lists
SHOPPING_MIN_AUTOSYNC_INTERVAL = int( SHOPPING_MIN_AUTOSYNC_INTERVAL = int(
@ -69,7 +70,8 @@ if os.getenv('CSRF_TRUSTED_ORIGINS'):
CSRF_TRUSTED_ORIGINS = os.getenv('CSRF_TRUSTED_ORIGINS').split(',') CSRF_TRUSTED_ORIGINS = os.getenv('CSRF_TRUSTED_ORIGINS').split(',')
if CORS_ORIGIN_ALLOW_ALL := os.getenv('CORS_ORIGIN_ALLOW_ALL') is not None: 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 CORS_ALLOW_ALL_ORIGINS = CORS_ORIGIN_ALLOW_ALL
else: else:
CORS_ALLOW_ALL_ORIGINS = bool(int(os.getenv("CORS_ALLOW_ALL_ORIGINS", True))) CORS_ALLOW_ALL_ORIGINS = bool(int(os.getenv("CORS_ALLOW_ALL_ORIGINS", True)))
@ -158,7 +160,8 @@ try:
INSTALLED_APPS.append(plugin_module) INSTALLED_APPS.append(plugin_module)
plugin_config = { 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', 'version': plugin_class.VERSION if hasattr(plugin_class, 'VERSION') else 'unknown',
'website': plugin_class.website if hasattr(plugin_class, 'website') else '', 'website': plugin_class.website if hasattr(plugin_class, 'website') else '',
'github': plugin_class.github if hasattr(plugin_class, 'github') 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_path': os.path.join(BASE_DIR, 'recipes', 'plugins', d),
'base_url': plugin_class.base_url, 'base_url': plugin_class.base_url,
'bundle_name': plugin_class.bundle_name if hasattr(plugin_class, 'bundle_name') else '', '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_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 '', 'nav_dropdown': plugin_class.nav_dropdown if hasattr(plugin_class, 'nav_dropdown') else '',
} }
@ -256,7 +260,8 @@ if LDAP_AUTH:
ldap.SCOPE_SUBTREE, ldap.SCOPE_SUBTREE,
os.getenv('AUTH_LDAP_USER_SEARCH_FILTER_STR', '(uid=%(user)s)'), 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', 'first_name': 'givenName',
'last_name': 'sn', 'last_name': 'sn',
'email': 'mail', 'email': 'mail',