mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-25 19:11:09 +00:00
Select theme in context processors
This commit is contained in:
parent
a6883b5b87
commit
f4dc07b6b9
4 changed files with 96 additions and 15 deletions
|
@ -8,8 +8,16 @@ def site_settings(request): # pylint: disable=unused-argument
|
|||
if not request.is_secure():
|
||||
request_protocol = "http://"
|
||||
|
||||
site = models.SiteSettings.objects.get()
|
||||
theme = "css/themes/bookwyrm-light.scss"
|
||||
if request.user.is_authenticated and request.user.theme:
|
||||
theme = request.user.theme.path
|
||||
elif site.default_theme:
|
||||
theme = site.default_theme.path
|
||||
|
||||
return {
|
||||
"site": models.SiteSettings.objects.get(),
|
||||
"site": site,
|
||||
"site_theme": theme,
|
||||
"active_announcements": models.Announcement.active_announcements(),
|
||||
"thumbnail_generation_enabled": settings.ENABLE_THUMBNAIL_GENERATION,
|
||||
"media_full_url": settings.MEDIA_FULL_URL,
|
||||
|
|
|
@ -174,17 +174,6 @@ class User(OrderedCollectionPageMixin, AbstractUser):
|
|||
property_fields = [("following_link", "following")]
|
||||
field_tracker = FieldTracker(fields=["name", "avatar"])
|
||||
|
||||
@property
|
||||
def get_theme(self):
|
||||
"""get the theme given the user/site"""
|
||||
if self.theme:
|
||||
return self.theme.path
|
||||
site_model = apps.get_model("bookwyrm", "SiteSettings", require_ready=True)
|
||||
site = site_model.objects.get()
|
||||
if site.default_theme:
|
||||
return site.default_theme.path
|
||||
return "css/themes/bookwyrm-light.scss"
|
||||
|
||||
@property
|
||||
def confirmation_link(self):
|
||||
"""helper for generating confirmation links"""
|
||||
|
|
|
@ -8,9 +8,7 @@
|
|||
<head>
|
||||
<title>{% block title %}BookWyrm{% endblock %} - {{ site.name }}</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
{% with theme_path=user.get_theme %}
|
||||
<link href="{% sass_src theme_path %}" rel="stylesheet" type="text/css" />
|
||||
{% endwith %}
|
||||
<link href="{% sass_src site_theme %}" rel="stylesheet" type="text/css" />
|
||||
|
||||
<link rel="search" type="application/opensearchdescription+xml" href="{% url 'opensearch' %}" title="{% blocktrans with site_name=site.name %}{{ site_name }} search{% endblocktrans %}" />
|
||||
|
||||
|
|
86
bookwyrm/tests/test_context_processors.py
Normal file
86
bookwyrm/tests/test_context_processors.py
Normal file
|
@ -0,0 +1,86 @@
|
|||
""" test for context processor """
|
||||
from unittest.mock import patch
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.context_processors import site_settings
|
||||
|
||||
|
||||
class ContextProcessor(TestCase):
|
||||
"""pages you land on without really trying"""
|
||||
|
||||
def setUp(self):
|
||||
"""we need basic test data and mocks"""
|
||||
self.factory = RequestFactory()
|
||||
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
||||
"bookwyrm.activitystreams.populate_stream_task.delay"
|
||||
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
|
||||
self.local_user = models.User.objects.create_user(
|
||||
"mouse@local.com",
|
||||
"mouse@mouse.mouse",
|
||||
"password",
|
||||
local=True,
|
||||
localname="mouse",
|
||||
)
|
||||
self.anonymous_user = AnonymousUser
|
||||
self.anonymous_user.is_authenticated = False
|
||||
self.site = models.SiteSettings.objects.create()
|
||||
|
||||
def test_theme_unset(self):
|
||||
"""logged in user, no selected theme"""
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
settings = site_settings(request)
|
||||
self.assertEqual(settings["site_theme"], "css/themes/bookwyrm-light.scss")
|
||||
|
||||
def test_theme_unset_logged_out(self):
|
||||
"""logged out user, no selected theme"""
|
||||
request = self.factory.get("")
|
||||
request.user = self.anonymous_user
|
||||
settings = site_settings(request)
|
||||
self.assertEqual(settings["site_theme"], "css/themes/bookwyrm-light.scss")
|
||||
|
||||
def test_theme_instance_default(self):
|
||||
"""logged in user, instance default theme"""
|
||||
self.site.default_theme = models.Theme.objects.get(name="BookWyrm Dark")
|
||||
self.site.save()
|
||||
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
settings = site_settings(request)
|
||||
self.assertEqual(settings["site_theme"], "css/themes/bookwyrm-dark.scss")
|
||||
|
||||
def test_theme_instance_default_logged_out(self):
|
||||
"""logged out user, instance default theme"""
|
||||
self.site.default_theme = models.Theme.objects.get(name="BookWyrm Dark")
|
||||
self.site.save()
|
||||
|
||||
request = self.factory.get("")
|
||||
request.user = self.anonymous_user
|
||||
settings = site_settings(request)
|
||||
self.assertEqual(settings["site_theme"], "css/themes/bookwyrm-dark.scss")
|
||||
|
||||
def test_theme_user_set(self):
|
||||
"""logged in user, user theme"""
|
||||
self.local_user.theme = models.Theme.objects.get(name="BookWyrm Dark")
|
||||
self.local_user.save(broadcast=False, update_fields=["theme"])
|
||||
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
settings = site_settings(request)
|
||||
self.assertEqual(settings["site_theme"], "css/themes/bookwyrm-dark.scss")
|
||||
|
||||
def test_theme_user_set_instance_default(self):
|
||||
"""logged in user, instance default theme"""
|
||||
self.site.default_theme = models.Theme.objects.get(name="BookWyrm Dark")
|
||||
self.site.save()
|
||||
|
||||
self.local_user.theme = models.Theme.objects.get(name="BookWyrm Light")
|
||||
self.local_user.save(broadcast=False, update_fields=["theme"])
|
||||
|
||||
request = self.factory.get("")
|
||||
request.user = self.local_user
|
||||
settings = site_settings(request)
|
||||
self.assertEqual(settings["site_theme"], "css/themes/bookwyrm-light.scss")
|
Loading…
Reference in a new issue