Show theme options

This commit is contained in:
Mouse Reeve 2022-02-27 10:46:01 -08:00
parent cc015536fa
commit 8850b68b52
7 changed files with 29 additions and 18 deletions

View file

@ -4,6 +4,8 @@ from collections import defaultdict
from urllib.parse import urlparse from urllib.parse import urlparse
from django import forms 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 import ModelForm, PasswordInput, widgets, ChoiceField
from django.forms.widgets import Textarea from django.forms.widgets import Textarea
from django.utils import timezone from django.utils import timezone
@ -460,13 +462,22 @@ class SiteThemeForm(CustomForm):
fields = ["default_theme"] 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 ThemeForm(CustomForm):
class Meta: class Meta:
model = models.Theme model = models.Theme
fields = ["name", "path"] fields = ["name", "path"]
widgets = { widgets = {
"name": forms.TextInput(attrs={"aria-describedby": "desc_name"}), "name": forms.TextInput(attrs={"aria-describedby": "desc_name"}),
"path": forms.TextInput(attrs={"aria-describedby": "desc_path", "placeholder": _("example-theme.scss")}), "path": forms.Select(
attrs={"aria-describedby": "desc_path"}, choices=get_theme_choices()
),
} }

View file

@ -10,11 +10,11 @@ def add_default_themes(apps, schema_editor):
theme_model = apps.get_model("bookwyrm", "Theme") theme_model = apps.get_model("bookwyrm", "Theme")
theme_model.objects.using(db_alias).create( theme_model.objects.using(db_alias).create(
name="BookWyrm Light", name="BookWyrm Light",
path="bookwyrm-light.scss", path="css/themes/bookwyrm-light.scss",
) )
theme_model.objects.using(db_alias).create( theme_model.objects.using(db_alias).create(
name="BookWyrm Dark", name="BookWyrm Dark",
path="bookwyrm-dark.scss", path="css/themes/bookwyrm-dark.scss",
) )

View file

@ -176,15 +176,13 @@ class User(OrderedCollectionPageMixin, AbstractUser):
@property @property
def get_theme(self): def get_theme(self):
"""get the theme given the user/site""" """get the theme given the user/site"""
path = "bookwyrm-light.scss"
if self.theme: if self.theme:
path = self.theme.path return self.theme.path
else: site_model = apps.get_model("bookwyrm", "SiteSettings", require_ready=True)
site_model = apps.get_model("bookwyrm", "SiteSettings", require_ready=True) site = site_model.objects.get()
site = site_model.objects.get() if site.default_theme:
if site.default_theme: return site.default_theme.path
path = site.default_theme.path return "css/themes/bookwyrm-light.scss"
return f"css/{path}"
@property @property
def confirmation_link(self): def confirmation_link(self):

View file

@ -1,4 +1,4 @@
@import "vendor/bulma/sass/utilities/derived-variables.sass"; @import "../vendor/bulma/sass/utilities/derived-variables.sass";
/* Colors /* Colors
******************************************************************************/ ******************************************************************************/
@ -54,4 +54,4 @@ $menu-item-active-background-color: $link-background;
$family-primary: $family-sans-serif; $family-primary: $family-sans-serif;
$family-secondary: $family-sans-serif; $family-secondary: $family-sans-serif;
@import "bookwyrm.scss"; @import "../bookwyrm.scss";

View file

@ -1,4 +1,4 @@
@import "vendor/bulma/sass/utilities/derived-variables.sass"; @import "../vendor/bulma/sass/utilities/derived-variables.sass";
/* Colors /* Colors
******************************************************************************/ ******************************************************************************/
@ -52,4 +52,4 @@ $menu-item-active-background-color: $link-background;
$family-primary: $family-sans-serif; $family-primary: $family-sans-serif;
$family-secondary: $family-sans-serif; $family-secondary: $family-sans-serif;
@import "bookwyrm.scss"; @import "../bookwyrm.scss";

View file

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

View file

@ -20,7 +20,7 @@
<h2 class="title is-5">{% trans "How to add a theme" %}</h2> <h2 class="title is-5">{% trans "How to add a theme" %}</h2>
<ol> <ol>
<li> <li>
{% trans "Copy the theme file into the <code>bookwyrm/static/css/</code> directory on your server from the command line." %} {% trans "Copy the theme file into the <code>bookwyrm/static/css/themes</code> directory on your server from the command line." %}
</li> </li>
<li> <li>
{% trans "Run <code>./bw-dev compilescss</code>." %} {% trans "Run <code>./bw-dev compilescss</code>." %}
@ -67,7 +67,9 @@
{% trans "Theme filename" %} {% trans "Theme filename" %}
</label> </label>
<div class="control"> <div class="control">
{{ theme_form.path }} <div class="select">
{{ theme_form.path }}
</div>
{% include 'snippets/form_errors.html' with errors_list=theme_form.path.errors id="desc_path" %} {% include 'snippets/form_errors.html' with errors_list=theme_form.path.errors id="desc_path" %}
</div> </div>
</div> </div>