diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py index 7ae4e446..81061f96 100644 --- a/bookwyrm/forms.py +++ b/bookwyrm/forms.py @@ -4,6 +4,8 @@ from collections import defaultdict from urllib.parse import urlparse from django import forms +from django.contrib.staticfiles.utils import get_files +from django.contrib.staticfiles.storage import StaticFilesStorage from django.forms import ModelForm, PasswordInput, widgets, ChoiceField from django.forms.widgets import Textarea from django.utils import timezone @@ -454,6 +456,31 @@ class SiteForm(CustomForm): } +class SiteThemeForm(CustomForm): + class Meta: + model = models.SiteSettings + fields = ["default_theme"] + + +def get_theme_choices(): + """static files""" + choices = list(get_files(StaticFilesStorage(), location="css/themes")) + current = models.Theme.objects.values_list("path", flat=True) + return [(c, c) for c in choices if c not in current and c[-5:] == ".scss"] + + +class ThemeForm(CustomForm): + class Meta: + model = models.Theme + fields = ["name", "path"] + widgets = { + "name": forms.TextInput(attrs={"aria-describedby": "desc_name"}), + "path": forms.Select( + attrs={"aria-describedby": "desc_path"}, choices=get_theme_choices() + ), + } + + class AnnouncementForm(CustomForm): class Meta: model = models.Announcement diff --git a/bookwyrm/migrations/0142_auto_20220227_1752.py b/bookwyrm/migrations/0142_auto_20220227_1752.py new file mode 100644 index 00000000..2282679d --- /dev/null +++ b/bookwyrm/migrations/0142_auto_20220227_1752.py @@ -0,0 +1,68 @@ +# Generated by Django 3.2.12 on 2022-02-27 17:52 + +from django.db import migrations, models +import django.db.models.deletion + + +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).create( + name="BookWyrm Light", + path="css/themes/bookwyrm-light.scss", + ) + theme_model.objects.using(db_alias).create( + name="BookWyrm Dark", + path="css/themes/bookwyrm-dark.scss", + ) + + +class Migration(migrations.Migration): + + dependencies = [ + ("bookwyrm", "0141_alter_report_status"), + ] + + operations = [ + migrations.CreateModel( + name="Theme", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("created_date", models.DateTimeField(auto_now_add=True)), + ("name", models.CharField(max_length=50, unique=True)), + ("path", models.CharField(max_length=50, unique=True)), + ], + ), + migrations.AddField( + model_name="sitesettings", + name="default_theme", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + 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, 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 a40d295b..c6c53f76 100644 --- a/bookwyrm/models/site.py +++ b/bookwyrm/models/site.py @@ -24,6 +24,9 @@ class SiteSettings(models.Model): ) instance_description = models.TextField(default="This instance has no description.") instance_short_description = models.CharField(max_length=255, blank=True, null=True) + default_theme = models.ForeignKey( + "Theme", null=True, blank=True, on_delete=models.SET_NULL + ) # admin setup options install_mode = models.BooleanField(default=False) @@ -104,6 +107,18 @@ class SiteSettings(models.Model): super().save(*args, **kwargs) +class Theme(models.Model): + """Theme files""" + + created_date = models.DateTimeField(auto_now_add=True) + name = models.CharField(max_length=50, unique=True) + path = models.CharField(max_length=50, unique=True) + + def __str__(self): + # pylint: disable=invalid-str-returned + return self.name + + class SiteInvite(models.Model): """gives someone access to create an account on the instance""" diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index 6367dcae..1198717e 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,17 @@ 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""" diff --git a/bookwyrm/static/css/bookwyrm.scss b/bookwyrm/static/css/bookwyrm.scss index 6b5e7e6b..ee25b728 100644 --- a/bookwyrm/static/css/bookwyrm.scss +++ b/bookwyrm/static/css/bookwyrm.scss @@ -1,7 +1,6 @@ @charset "utf-8"; @import "instance-settings"; -@import "themes/light.scss"; @import "vendor/bulma/bulma.sass"; @import "vendor/icons.css"; @import "bookwyrm/all.scss"; diff --git a/bookwyrm/static/css/themes/bookwyrm-dark.scss b/bookwyrm/static/css/themes/bookwyrm-dark.scss index c24e153e..1230c726 100644 --- a/bookwyrm/static/css/themes/bookwyrm-dark.scss +++ b/bookwyrm/static/css/themes/bookwyrm-dark.scss @@ -1,5 +1,6 @@ @import "../vendor/bulma/sass/utilities/initial-variables.sass"; + /* Colors ******************************************************************************/ @@ -77,3 +78,6 @@ $progress-value-background-color: $border-light; ******************************************************************************/ $family-primary: $family-sans-serif; $family-secondary: $family-sans-serif; + + +@import "../bookwyrm.scss"; \ No newline at end of file diff --git a/bookwyrm/static/css/themes/bookwyrm-light.scss b/bookwyrm/static/css/themes/bookwyrm-light.scss index 60781ff5..91cad72e 100644 --- a/bookwyrm/static/css/themes/bookwyrm-light.scss +++ b/bookwyrm/static/css/themes/bookwyrm-light.scss @@ -54,3 +54,6 @@ $invisible-overlay-background-color: rgba($scheme-invert, 0.66); ******************************************************************************/ $family-primary: $family-sans-serif; $family-secondary: $family-sans-serif; + + +@import "../bookwyrm.scss"; \ No newline at end of file diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index 0e874072..aaf21717 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -8,7 +8,9 @@