from django import forms
from django.contrib import messages
from django.http.response import HttpResponseRedirect
from django.template import loader
from django.utils.translation import gettext_lazy as _
from django.views.generic import TemplateView
from django.views.generic.edit import FormView
from shuup.admin.shop_provider import get_shop
from shuup.admin.toolbar import URLActionButton, get_default_edit_toolbar
from shuup.admin.utils.picotable import Column, TextFilter
from shuup.admin.utils.views import CreateOrUpdateView, PicotableListView
from shuup.admin.views.wizard import TemplatedWizardFormDef, WizardPane
from shuup.core import cache
from shuup.utils.django_compat import reverse
from shuup.utils.importing import cached_load
from shuup.xtheme._theme import get_theme_by_identifier, get_theme_cache_key, set_current_theme
from shuup.xtheme.forms import AdminThemeForm, FontForm
from shuup.xtheme.models import AdminThemeSettings, Font, ThemeSettings
[docs]
class ThemeWizardPane(WizardPane):
identifier = "theme"
icon = "xtheme/theme.png"
title = _("Theme")
text = _("Choose a theme for your shop")
[docs]
def valid(self):
from shuup.admin.utils.permissions import has_permission
return has_permission(self.request.user, "telemetry")
[docs]
def visible(self):
return ThemeSettings.objects.filter(active=False, shop=self.object).count() == 0
[docs]
class ThemeConfigView(FormView):
"""
A view for listing and activating themes.
"""
template_name = "shuup/xtheme/admin/config.jinja"
form_class = ActivationForm
[docs]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
shop = get_shop(self.request)
context.update(cached_load("SHUUP_XTHEME_ADMIN_THEME_CONTEXT")(shop))
return context
[docs]
class ThemeConfigDetailView(CreateOrUpdateView):
"""
A view for configuring a single theme.
"""
model = ThemeSettings
template_name = "shuup/xtheme/admin/config_detail.jinja"
form_class = forms.Form
context_object_name = "theme_settings"
add_form_errors_as_messages = True
[docs]
def get_object(self, queryset=None):
return ThemeSettings.objects.get_or_create(
theme_identifier=self.kwargs["theme_identifier"],
shop=get_shop(self.request),
)[0]
[docs]
def get_theme(self):
"""
Get the theme object to configure.
:return: Theme object.
:rtype: shuup.xtheme.Theme
"""
return get_theme_by_identifier(identifier=self.kwargs["theme_identifier"], shop=get_shop(self.request))
[docs]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
shop = get_shop(self.request)
theme = self.get_theme()
context["theme"] = theme
context["guide"] = None
if theme.guide_template:
template = loader.get_template(theme.guide_template)
context["guide"] = template.render({}, request=self.request)
context["active_stylesheet"] = self.object.data.get("settings", {}).get("stylesheet", None)
context["shop"] = shop
return context
[docs]
def get_success_url(self):
return reverse(
"shuup_admin:xtheme.config_detail",
kwargs={"theme_identifier": self.object.theme_identifier},
)
[docs]
class ThemeGuideTemplateView(TemplateView):
template_name = None
[docs]
def dispatch(self, request, *args, **kwargs):
theme = get_theme_by_identifier(kwargs["theme_identifier"], shop=get_shop(self.request))
self.template_name = theme.guide_template
return super().dispatch(request, *args, **kwargs)
[docs]
class FontEditView(CreateOrUpdateView):
model = Font
form_class = FontForm
template_name = "shuup/xtheme/admin/font_create.jinja"
context_object_name = "font"
[docs]
def get_queryset(self):
return Font.objects.filter(shop=get_shop(self.request))
[docs]
class FontListView(PicotableListView):
url_identifier = "xtheme.font"
model = Font
default_columns = [
Column(
"name",
_("Name"),
sort_field="name",
display="name",
filter_config=TextFilter(filter_field="name", placeholder=_("Filter by name...")),
),
Column("woff", _("Woff"), display="format_woff"),
Column("woff2", _("Woff2"), display="format_woff2"),
Column("ttf", _("TTF"), display="format_ttf"),
Column("svg", _("SVG"), display="format_svg"),
Column("eot", _("EOT"), display="format_eot"),
]
[docs]
def get_queryset(self):
return Font.objects.filter(shop=get_shop(self.request))
[docs]
class AdminThemeConfigDetailView(FormView):
form_class = AdminThemeForm
template_name = "shuup/xtheme/admin/admin_config_detail.jinja"
[docs]
def get_success_url(self):
return reverse("shuup_admin:xtheme.admin_config_detail")