From 43269429acdec88d1c369a142543d124ef060427 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 26 Feb 2022 13:38:45 -0800 Subject: [PATCH] Use selected theme --- ...226_2024.py => 0142_auto_20220226_2047.py} | 36 +++++++++++-------- bookwyrm/models/__init__.py | 2 +- bookwyrm/models/site.py | 16 ++++----- bookwyrm/models/user.py | 14 ++++++++ bookwyrm/static/css/bookwyrm-dark.scss | 4 ++- bookwyrm/templates/layout.html | 4 ++- 6 files changed, 50 insertions(+), 26 deletions(-) rename bookwyrm/migrations/{0142_auto_20220226_2024.py => 0142_auto_20220226_2047.py} (67%) diff --git a/bookwyrm/migrations/0142_auto_20220226_2024.py b/bookwyrm/migrations/0142_auto_20220226_2047.py similarity index 67% rename from bookwyrm/migrations/0142_auto_20220226_2024.py rename to bookwyrm/migrations/0142_auto_20220226_2047.py index 662cd9bd..928d556c 100644 --- a/bookwyrm/migrations/0142_auto_20220226_2024.py +++ b/bookwyrm/migrations/0142_auto_20220226_2047.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2.12 on 2022-02-26 20:24 +# Generated by Django 3.2.12 on 2022-02-26 20:47 import django.core.validators from django.db import migrations, models @@ -9,17 +9,13 @@ def add_default_themes(apps, schema_editor): """add light and dark themes""" db_alias = schema_editor.connection.alias theme_model = apps.get_model("bookwyrm", "Theme") - theme_model.objects.using(db_alias).bulk_create( - [ - theme_model.objects.using(db_alias)( - name="BookWyrm Light", - path="bookwyrm-light.scss", - ), - theme_model.objects.using(db_alias)( - name="BookWyrm Dark", - path="bookwyrm-dark.scss", - ), - ] + theme_model.objects.using(db_alias).create( + name="BookWyrm Light", + path="bookwyrm-light.scss", + ) + theme_model.objects.using(db_alias).create( + name="BookWyrm Dark", + path="bookwyrm-dark.scss", ) @@ -43,10 +39,11 @@ class Migration(migrations.Migration): ), ), ("created_date", models.DateTimeField(auto_now_add=True)), - ("name", models.CharField(max_length=10, unique=True)), + ("name", models.CharField(max_length=50, unique=True)), ( "theme_file", models.FileField( + null=True, upload_to="css/", validators=[ django.core.validators.FileExtensionValidator( @@ -55,6 +52,7 @@ class Migration(migrations.Migration): ], ), ), + ("path", models.CharField(blank=True, max_length=50, null=True)), ], ), migrations.AddField( @@ -67,7 +65,17 @@ class Migration(migrations.Migration): to="bookwyrm.theme", ), ), + migrations.AddField( + model_name="user", + name="theme", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to="bookwyrm.theme", + ), + ), migrations.RunPython( - add_default_themes, reversed_code=migrations.RunPython.noop + add_default_themes, reverse_code=migrations.RunPython.noop ), ] diff --git a/bookwyrm/models/__init__.py b/bookwyrm/models/__init__.py index 440d18d9..a8a84f09 100644 --- a/bookwyrm/models/__init__.py +++ b/bookwyrm/models/__init__.py @@ -26,7 +26,7 @@ from .group import Group, GroupMember, GroupMemberInvitation from .import_job import ImportJob, ImportItem -from .site import SiteSettings, SiteInvite +from .site import SiteSettings, Theme, SiteInvite from .site import PasswordReset, InviteRequest from .announcement import Announcement from .antispam import EmailBlocklist, IPBlocklist, AutoMod, automod_task diff --git a/bookwyrm/models/site.py b/bookwyrm/models/site.py index 602a3f59..30ebfe74 100644 --- a/bookwyrm/models/site.py +++ b/bookwyrm/models/site.py @@ -112,7 +112,7 @@ class Theme(models.Model): """Theme files""" created_date = models.DateTimeField(auto_now_add=True) - name = models.CharField(max_length=10, unique=True) + name = models.CharField(max_length=50, unique=True) theme_file = models.FileField( upload_to="css/", validators=[FileExtensionValidator(["scss", "sass"])], @@ -120,15 +120,13 @@ class Theme(models.Model): ) path = models.CharField(max_length=50, blank=True, null=True) - @classmethod - def get_theme(cls, user): + def __str__(self): + return self.name + + @property + def theme_path(self): """get the theme given the user/site""" - if user and user.theme: - return user.theme.path - site = SiteSettings.objects.get() - if site.theme: - return site.theme.path - return "light.scss" + return self.theme_file.path if self.theme_file else self.path class SiteInvite(models.Model): diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index 6367dcae..3d12e604 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -136,6 +136,7 @@ class User(OrderedCollectionPageMixin, AbstractUser): updated_date = models.DateTimeField(auto_now=True) last_active_date = models.DateTimeField(default=timezone.now) manually_approves_followers = fields.BooleanField(default=False) + theme = models.ForeignKey("Theme", null=True, blank=True, on_delete=models.SET_NULL) # options to turn features on and off show_goal = models.BooleanField(default=True) @@ -172,6 +173,19 @@ 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: + path = self.theme.theme_path + else: + site_model = apps.get_model("bookwyrm", "SiteSettings", require_ready=True) + site = site_model.objects.get() + if site.default_theme: + path = site.default_theme.theme_path + path = path or "light.scss" + return f"css/{path}" + @property def confirmation_link(self): """helper for generating confirmation links""" diff --git a/bookwyrm/static/css/bookwyrm-dark.scss b/bookwyrm/static/css/bookwyrm-dark.scss index 8df4ce50..957187ff 100644 --- a/bookwyrm/static/css/bookwyrm-dark.scss +++ b/bookwyrm/static/css/bookwyrm-dark.scss @@ -1,4 +1,4 @@ -@import "../vendor/bulma/sass/utilities/derived-variables.sass"; +@import "vendor/bulma/sass/utilities/derived-variables.sass"; /* Colors ******************************************************************************/ @@ -53,3 +53,5 @@ $menu-item-active-background-color: $link-background; ******************************************************************************/ $family-primary: $family-sans-serif; $family-secondary: $family-sans-serif; + +@import "bookwyrm.scss"; diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index a3149996..444241e5 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -8,7 +8,9 @@ {% block title %}BookWyrm{% endblock %} - {{ site.name }} - + {% with theme_path=user.get_theme %} + + {% endwith %}