Theme handling improvements

This commit is contained in:
Ariel Rin 2023-10-21 09:08:28 +00:00
parent 24376262f0
commit 4912f0f8f0
5 changed files with 13 additions and 4 deletions

View File

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

View File

@ -1,5 +1,5 @@
from django.conf import settings
from .views import NightModeRedirectView, ThemeRedirectView
from .views import NightModeRedirectView
def auth_settings(request):

View File

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

View File

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

View File

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