forked from mirrors/bookwyrm
Check available themes in form
This commit is contained in:
parent
8850b68b52
commit
8259d16ee9
3 changed files with 40 additions and 23 deletions
|
@ -4,8 +4,6 @@ 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
|
||||||
|
@ -462,22 +460,13 @@ 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.Select(
|
"path": forms.Select(attrs={"aria-describedby": "desc_path"}),
|
||||||
attrs={"aria-describedby": "desc_path"}, choices=get_theme_choices()
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,12 @@
|
||||||
class="box"
|
class="box"
|
||||||
enctype="multipart/form-data"
|
enctype="multipart/form-data"
|
||||||
>
|
>
|
||||||
|
{% if not choices %}
|
||||||
|
<div class="notification is-warning">
|
||||||
|
{% trans "No available theme files detected" %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
<fieldset {% if not choices %}disabled{% endif %}>
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<div class="columns">
|
<div class="columns">
|
||||||
<div class="column is-half">
|
<div class="column is-half">
|
||||||
|
@ -68,7 +74,18 @@
|
||||||
</label>
|
</label>
|
||||||
<div class="control">
|
<div class="control">
|
||||||
<div class="select">
|
<div class="select">
|
||||||
{{ theme_form.path }}
|
<select
|
||||||
|
name="path"
|
||||||
|
aria-describedby="desc_path"
|
||||||
|
class=""
|
||||||
|
id="id_path"
|
||||||
|
>
|
||||||
|
{% for choice in choices %}
|
||||||
|
<option value="{{ choice }}">
|
||||||
|
{{ choice }}
|
||||||
|
</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
</div>
|
</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>
|
||||||
|
@ -76,6 +93,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button type="submit" class="button">{% trans "Add theme" %}</button>
|
<button type="submit" class="button">{% trans "Add theme" %}</button>
|
||||||
|
</fieldset>
|
||||||
</form>
|
</form>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
""" manage themes """
|
""" manage themes """
|
||||||
from django.contrib.auth.decorators import login_required, permission_required
|
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.template.response import TemplateResponse
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
from django.views import View
|
from django.views import View
|
||||||
|
@ -18,21 +20,29 @@ class Themes(View):
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
"""view existing themes and set defaults"""
|
"""view existing themes and set defaults"""
|
||||||
data = {
|
return TemplateResponse(request, "settings/themes.html", get_view_data())
|
||||||
"themes": models.Theme.objects.all(),
|
|
||||||
"theme_form": forms.ThemeForm(),
|
|
||||||
}
|
|
||||||
return TemplateResponse(request, "settings/themes.html", data)
|
|
||||||
|
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
"""edit the site settings"""
|
"""edit the site settings"""
|
||||||
form = forms.ThemeForm(request.POST, request.FILES)
|
form = forms.ThemeForm(request.POST, request.FILES)
|
||||||
data = {
|
|
||||||
"themes": models.Theme.objects.all(),
|
|
||||||
"theme_form": form,
|
|
||||||
}
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form.save()
|
form.save()
|
||||||
|
|
||||||
|
data = get_view_data()
|
||||||
|
|
||||||
|
if not form.is_valid():
|
||||||
|
data["theme_form"] = form
|
||||||
|
else:
|
||||||
data["success"] = True
|
data["success"] = True
|
||||||
data["theme_form"] = forms.ThemeForm()
|
|
||||||
return TemplateResponse(request, "settings/themes.html", data)
|
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(),
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue