2022-02-27 18:00:50 +00:00
|
|
|
""" manage themes """
|
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
2022-02-27 19:19:09 +00:00
|
|
|
from django.contrib.staticfiles.utils import get_files
|
|
|
|
from django.contrib.staticfiles.storage import StaticFilesStorage
|
2022-02-28 17:54:07 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2022-02-27 18:00:50 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import View
|
2022-02-28 17:54:07 +00:00
|
|
|
from django.views.decorators.http import require_POST
|
2022-02-27 18:00:50 +00:00
|
|
|
|
|
|
|
from bookwyrm import forms, models
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable= no-self-use
|
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
@method_decorator(
|
|
|
|
permission_required("bookwyrm.edit_instance_settings", raise_exception=True),
|
|
|
|
name="dispatch",
|
|
|
|
)
|
|
|
|
class Themes(View):
|
|
|
|
"""manage things like the instance name"""
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
"""view existing themes and set defaults"""
|
2022-02-27 19:19:09 +00:00
|
|
|
return TemplateResponse(request, "settings/themes.html", get_view_data())
|
2022-02-27 18:00:50 +00:00
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
"""edit the site settings"""
|
|
|
|
form = forms.ThemeForm(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2022-02-27 19:19:09 +00:00
|
|
|
|
|
|
|
data = get_view_data()
|
|
|
|
|
|
|
|
if not form.is_valid():
|
|
|
|
data["theme_form"] = form
|
|
|
|
else:
|
2022-02-27 18:00:50 +00:00
|
|
|
data["success"] = True
|
|
|
|
return TemplateResponse(request, "settings/themes.html", data)
|
2022-02-27 19:19:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
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(),
|
|
|
|
}
|
2022-02-28 17:54:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@require_POST
|
|
|
|
@permission_required("bookwyrm.edit_instance_settings", raise_exception=True)
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def delete_theme(request, theme_id):
|
|
|
|
"""Remove a theme"""
|
|
|
|
get_object_or_404(models.Theme, id=theme_id).delete()
|
|
|
|
return redirect("settings-themes")
|