From 4912f0f8f0a0aab481f943294475414723758b9d Mon Sep 17 00:00:00 2001 From: Ariel Rin Date: Sat, 21 Oct 2023 09:08:28 +0000 Subject: [PATCH] Theme handling improvements --- allianceauth/authentication/middleware.py | 2 ++ allianceauth/context_processors.py | 2 +- .../project_template/project_name/settings/base.py | 3 ++- allianceauth/theme/templatetags/theme_tags.py | 7 +++++-- allianceauth/views.py | 3 +++ 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/allianceauth/authentication/middleware.py b/allianceauth/authentication/middleware.py index f2fc8145..ca28d92f 100644 --- a/allianceauth/authentication/middleware.py +++ b/allianceauth/authentication/middleware.py @@ -44,6 +44,8 @@ class UserSettingsMiddleware(MiddlewareMixin): logger.exception(e) # AA v4 Themes + # Null = has not been set by the user ever, dont act + # DEFAULT_THEME or DEFAULT_THEME_DARK will be used in get_theme() try: if request.user.profile.theme is not None: request.session["THEME"] = request.user.profile.theme diff --git a/allianceauth/context_processors.py b/allianceauth/context_processors.py index cafde2ea..4feb243e 100644 --- a/allianceauth/context_processors.py +++ b/allianceauth/context_processors.py @@ -1,5 +1,5 @@ from django.conf import settings -from .views import NightModeRedirectView, ThemeRedirectView +from .views import NightModeRedirectView def auth_settings(request): diff --git a/allianceauth/project_template/project_name/settings/base.py b/allianceauth/project_template/project_name/settings/base.py index 2506ecfb..c6588024 100644 --- a/allianceauth/project_template/project_name/settings/base.py +++ b/allianceauth/project_template/project_name/settings/base.py @@ -193,7 +193,8 @@ DATABASES = { SITE_NAME = 'Alliance Auth' -DEFAULT_THEME = "allianceauth.theme.darkly" +DEFAULT_THEME = "allianceauth.theme.flatly.auth_hooks.FlatlyThemeHook" +DEFAULT_THEME_DARK = "allianceauth.theme.darkly.auth_hooks.DarklyThemeHook" # Legacy AAv3 user.profile.night_mode=1 LOGIN_URL = 'auth_login_user' # view that handles login logic diff --git a/allianceauth/theme/templatetags/theme_tags.py b/allianceauth/theme/templatetags/theme_tags.py index aff18233..4409c151 100644 --- a/allianceauth/theme/templatetags/theme_tags.py +++ b/allianceauth/theme/templatetags/theme_tags.py @@ -21,8 +21,11 @@ def get_theme_from_hooks(theme, hooks): def get_theme(request): theme = settings.DEFAULT_THEME hooks = get_hooks('theme_hook') - if request.user: - theme = request.user.profile.theme or theme + + try: + theme = request.session.get('THEME', settings.DEFAULT_THEME_DARK if request.session.get('NIGHT_MODE', False) is True else settings.DEFAULT_THEME) + except AttributeError: + pass theme_hook = get_theme_from_hooks(theme, hooks) diff --git a/allianceauth/views.py b/allianceauth/views.py index ae8edfea..81aa89a8 100644 --- a/allianceauth/views.py +++ b/allianceauth/views.py @@ -1,3 +1,4 @@ +import warnings from django.views.generic.base import View from django.http import HttpResponseRedirect from django.shortcuts import render, redirect @@ -33,6 +34,7 @@ class NightModeRedirectView(View): class ThemeRedirectView(View): + THEME_VAR = "THEME" def post(self, request, *args, **kwargs): theme = request.POST.get('theme', settings.DEFAULT_THEME) @@ -40,6 +42,7 @@ class ThemeRedirectView(View): try: request.user.profile.theme = theme request.user.profile.save() + request.session[self.THEME_VAR] = theme except Exception as e: logger.exception(e)