diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py index 81061f96..01228b8e 100644 --- a/bookwyrm/forms.py +++ b/bookwyrm/forms.py @@ -4,8 +4,6 @@ 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 @@ -462,22 +460,13 @@ class SiteThemeForm(CustomForm): 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() - ), + "path": forms.Select(attrs={"aria-describedby": "desc_path"}), } diff --git a/bookwyrm/templates/settings/themes.html b/bookwyrm/templates/settings/themes.html index a0619369..11c34405 100644 --- a/bookwyrm/templates/settings/themes.html +++ b/bookwyrm/templates/settings/themes.html @@ -50,6 +50,12 @@ class="box" enctype="multipart/form-data" > + {% if not choices %} +
+ {% trans "No available theme files detected" %} +
+ {% endif %} +
{% csrf_token %}
@@ -68,7 +74,18 @@
- {{ theme_form.path }} +
{% include 'snippets/form_errors.html' with errors_list=theme_form.path.errors id="desc_path" %}
@@ -76,6 +93,7 @@
+
diff --git a/bookwyrm/views/admin/themes.py b/bookwyrm/views/admin/themes.py index cf11bc6d..c1eacf44 100644 --- a/bookwyrm/views/admin/themes.py +++ b/bookwyrm/views/admin/themes.py @@ -1,5 +1,7 @@ """ manage themes """ from django.contrib.auth.decorators import login_required, permission_required +from django.contrib.staticfiles.utils import get_files +from django.contrib.staticfiles.storage import StaticFilesStorage from django.template.response import TemplateResponse from django.utils.decorators import method_decorator from django.views import View @@ -18,21 +20,29 @@ class Themes(View): def get(self, request): """view existing themes and set defaults""" - data = { - "themes": models.Theme.objects.all(), - "theme_form": forms.ThemeForm(), - } - return TemplateResponse(request, "settings/themes.html", data) + return TemplateResponse(request, "settings/themes.html", get_view_data()) def post(self, request): """edit the site settings""" form = forms.ThemeForm(request.POST, request.FILES) - data = { - "themes": models.Theme.objects.all(), - "theme_form": form, - } if form.is_valid(): form.save() + + data = get_view_data() + + if not form.is_valid(): + data["theme_form"] = form + else: data["success"] = True - data["theme_form"] = forms.ThemeForm() return TemplateResponse(request, "settings/themes.html", data) + + +def get_view_data(): + """data for view""" + choices = list(get_files(StaticFilesStorage(), location="css/themes")) + current = models.Theme.objects.values_list("path", flat=True) + return { + "themes": models.Theme.objects.all(), + "choices": [c for c in choices if c not in current and c[-5:] == ".scss"], + "theme_form": forms.ThemeForm(), + }