Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial theme switcher #1685

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/profiles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from .views import ProfileDetail
from .views import ProfileUpdate
from .views import ProfilePermissionList
from .views import ProfileThemeSelectView

app_name = "profiles"
urlpatterns = [
path("", ProfileDetail.as_view(), name="detail"),
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/<str:theme>/", ProfileThemeSelectView.as_view(), name="set_theme"),
]
16 changes: 16 additions & 0 deletions src/profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"])
3 changes: 3 additions & 0 deletions src/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
{# Load Bootstrap CSS and JavaScript #}
{% if user.is_authenticated and user.profile.theme != "default" %}
<link href="{% static "css/bootstrap-"|add:user.profile.theme|add:".css" %}" rel="stylesheet">
{% elif not user.is_authenticated and request.session.theme|default:"default" != "default" %}
<link href="{% static "css/bootstrap-"|add:request.session.theme|add:".css" %}" rel="stylesheet">
{% else %}
<link href="{% static 'css/bootstrap-light.css' %}" rel="stylesheet">
<link href="{% static 'css/bootstrap-slate.css' %}" rel="stylesheet" media="(prefers-color-scheme: dark)">
Expand Down Expand Up @@ -155,6 +157,7 @@
{% else %}
<a class="nav-link" href="{% url 'account_login' %}">Login</a>
{% endif %}
{% include 'includes/darkswitch.html' %}
</div>
</div>
</div>
Expand Down
32 changes: 32 additions & 0 deletions src/templates/includes/darkswitch.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class="dropdown hide-for-no-js-users d-none d-lg-flex">
<a href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
{% if user.is_authenticated and user.profile.theme == "light" %}
<i class="fa-regular fa-sun fa-fw"></i>
{% elif user.is_authenticated and user.profile.theme == "slate" %}
<i class="fa-solid fa-moon fa-fw"></i>
{% elif not user.is_authenticated and request.session.theme|default:"default" == "slate" %}
<i class="fa-solid fa-moon fa-fw"></i>
{% elif not user.is_authenticated and request.session.theme|default:"default" == "light" %}
<i class="fa-regular fa-sun fa-fw"></i>
{% else %}
<i class="fa-solid fa-circle-half-stroke fa-fw"></i>
{% endif %}
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="{% url 'profiles:set_theme' theme="light" %}"><i class="fa-regular fa-sun fa-fw"></i> Light</a></li>
<li><a class="dropdown-item" href="{% url 'profiles:set_theme' theme="slate" %}"><i class="fa-solid fa-moon fa-fw"></i> Dark</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="{% url 'profiles:set_theme' theme="default" %}"><i class="fa-solid fa-circle-half-stroke fa-fw"></i> Auto</a></li>
</ul>
</div>
{% if user.is_authenticated and user.profile.theme == "light" %}
<li class="nav-item unhide-for-no-js-users d-lg-none"><a class="nav-link" href="{% url 'profiles:set_theme' theme="default" %}"><i class="fa-solid fa-circle-half-stroke"></i></a></li>
{% elif user.is_authenticated and user.profile.theme == "slate" %}
<li class="nav-item unhide-for-no-js-users d-lg-none"><a class="nav-link" href="{% url 'profiles:set_theme' theme="light" %}"><i class="fa-regular fa-sun"></i></a></li>
{% elif not user.is_authenticated and request.session.theme|default:"default" == "light" %}
<li class="nav-item unhide-for-no-js-users d-lg-none"><a class="nav-link" href="{% url 'profiles:set_theme' theme="default" %}"><i class="fa-solid fa-circle-half-stroke"></i></a></li>
{% elif not user.is_authenticated and request.session.theme|default:"default" == "slate" %}
<li class="nav-item unhide-for-no-js-users d-lg-none"><a class="nav-link" href="{% url 'profiles:set_theme' theme="light" %}"><i class="fa-regular fa-sun"></i></a></li>
{% else %}
<li class="nav-item unhide-for-no-js-users d-lg-none"><a class="nav-link" href="{% url 'profiles:set_theme' theme="slate" %}"><i class="fa-solid fa-moon"></i></a></li>
{% endif %}
Loading