Use selected theme

This commit is contained in:
Mouse Reeve 2022-02-26 13:38:45 -08:00
parent e15193e100
commit 43269429ac
6 changed files with 50 additions and 26 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -8,7 +8,9 @@
<head>
<title>{% block title %}BookWyrm{% endblock %} - {{ site.name }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="{% sass_src 'css/light.scss' %}" rel="stylesheet" type="text/css" />
{% with theme_path=user.get_theme %}
<link href="{% sass_src theme_path %}" rel="stylesheet" type="text/css" />
{% endwith %}
<link rel="search" type="application/opensearchdescription+xml" href="{% url 'opensearch' %}" title="{% blocktrans with site_name=site.name %}{{ site_name }} search{% endblocktrans %}" />