From 170205f1d5cf72f7c80c884c872bcd3c66eca662 Mon Sep 17 00:00:00 2001 From: "Rudy (zarya)" Date: Sun, 13 Oct 2024 23:12:03 +0200 Subject: [PATCH] Initial theme switcher --- src/profiles/urls.py | 2 ++ src/profiles/views.py | 16 +++++++++++++ src/templates/base.html | 3 +++ src/templates/includes/darkswitch.html | 32 ++++++++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 src/templates/includes/darkswitch.html diff --git a/src/profiles/urls.py b/src/profiles/urls.py index d3bc8c5c0..18ca97687 100644 --- a/src/profiles/urls.py +++ b/src/profiles/urls.py @@ -4,6 +4,7 @@ from .views import ProfileDetail from .views import ProfileUpdate from .views import ProfilePermissionList +from .views import ProfileThemeSelectView app_name = "profiles" urlpatterns = [ @@ -11,4 +12,5 @@ path("edit/", ProfileUpdate.as_view(), name="update"), path("api/", ProfileApiView.as_view(), name="api"), path("permissions/", ProfilePermissionList.as_view(), name="permissions_list"), + path("set_theme//", ProfileThemeSelectView.as_view(), name="set_theme"), ] diff --git a/src/profiles/views.py b/src/profiles/views.py index 7369bcdf2..bd7b21208 100644 --- a/src/profiles/views.py +++ b/src/profiles/views.py @@ -3,9 +3,12 @@ from django.contrib.auth.models import Permission from django.contrib.auth.mixins import LoginRequiredMixin from django.urls import reverse_lazy +from django.views import View from django.views.generic import DetailView from django.views.generic import ListView from django.views.generic import UpdateView +from django.http import HttpResponseRedirect + from jsonview.views import JsonView from oauth2_provider.views.generic import ScopedProtectedResourceView @@ -83,3 +86,16 @@ def get_queryset(self, *args, **kwargs): ) ) return perms + + +class ProfileThemeSelectView(View): + def get(self, request, *args, **kwargs): + request.session["theme"] = self.kwargs["theme"] + if self.request.user.is_authenticated and self.kwargs["theme"] in [ + "light", + "slate", + "default", + ]: + self.request.user.profile.theme = self.kwargs["theme"] + self.request.user.profile.save() + return HttpResponseRedirect(request.headers["referer"]) diff --git a/src/templates/base.html b/src/templates/base.html index 97c7a6993..2fdc59ac1 100644 --- a/src/templates/base.html +++ b/src/templates/base.html @@ -15,6 +15,8 @@ {# Load Bootstrap CSS and JavaScript #} {% if user.is_authenticated and user.profile.theme != "default" %} + {% elif not user.is_authenticated and request.session.theme|default:"default" != "default" %} + {% else %} @@ -155,6 +157,7 @@ {% else %} Login {% endif %} + {% include 'includes/darkswitch.html' %} diff --git a/src/templates/includes/darkswitch.html b/src/templates/includes/darkswitch.html new file mode 100644 index 000000000..744f55749 --- /dev/null +++ b/src/templates/includes/darkswitch.html @@ -0,0 +1,32 @@ + +{% if user.is_authenticated and user.profile.theme == "light" %} + +{% elif user.is_authenticated and user.profile.theme == "slate" %} + +{% elif not user.is_authenticated and request.session.theme|default:"default" == "light" %} + +{% elif not user.is_authenticated and request.session.theme|default:"default" == "slate" %} + +{% else %} + +{% endif %}