from django import forms
from django.http.response import HttpResponseNotAllowed
from django.utils.translation import gettext_lazy as _
from django.views.generic.edit import BaseDeleteView
from shuup.admin.shop_provider import get_shop
from shuup.admin.toolbar import get_default_edit_toolbar
from shuup.admin.utils.picotable import Column, TextFilter
from shuup.admin.utils.views import CreateOrUpdateView, PicotableListView
from shuup.apps.provides import get_provide_objects
from shuup.core import cache
from shuup.utils.django_compat import force_text, reverse_lazy
from shuup.xtheme.admin_module.widgets import XthemeCodeEditorWidget
from shuup.xtheme.models import Snippet
from shuup.xtheme.resources import GLOBAL_SNIPPETS_CACHE_KEY
class SnippetForm(forms.ModelForm):
class Meta:
model = Snippet
fields = ["name", "location", "themes", "snippet_type", "snippet"]
widgets = {"snippet": XthemeCodeEditorWidget()}
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request")
super().__init__(*args, **kwargs)
themes_choices = [(theme.identifier, theme.name) for theme in get_provide_objects("xtheme") if theme.identifier]
self.fields["themes"] = forms.MultipleChoiceField(
choices=themes_choices,
required=False,
help_text=_(
"Select the themes that will have this snippet injected. Leave the field blank to inject in all themes."
),
)
from shuup.xtheme.resources import LOCATION_INFO
location_choices = [(location_name, location["name"]) for location_name, location in LOCATION_INFO.items()]
self.fields["location"] = forms.ChoiceField(
choices=location_choices,
help_text=_("Select the location of the page to inject the snippet."),
)
def save(self, commit=True):
self.instance.shop = get_shop(self.request)
return super().save(commit)
def clean_themes(self):
return ",".join(self.cleaned_data["themes"])
[docs]
class SnippetEditView(CreateOrUpdateView):
model = Snippet
form_class = SnippetForm
template_name = "shuup/xtheme/admin/snippet_edit.jinja"
context_object_name = "snippet"
[docs]
def get_queryset(self):
return Snippet.objects.filter(shop=get_shop(self.request))
[docs]
class SnippetListView(PicotableListView):
url_identifier = "xtheme_snippet"
model = Snippet
default_columns = [
Column(
"name",
_("Name"),
filter_config=TextFilter(filter_field="name", placeholder=_("Filter by name...")),
),
Column(
"location",
_("Location"),
sort_field="location",
filter_config=TextFilter(filter_field="location", placeholder=_("Filter by location...")),
),
Column("snippet_type", _("Type"), sort_field="snippet_type"),
Column("themes", _("Themes"), display="get_themes"),
]
[docs]
def get_themes(self, value):
return ", ".join(
[force_text(theme.name) for theme in get_provide_objects("xtheme") if theme.identifier in value.themes]
)
[docs]
def get_queryset(self):
return Snippet.objects.filter(shop=get_shop(self.request))
[docs]
class SnippetDeleteView(BaseDeleteView):
model = Snippet
success_url = reverse_lazy("shuup_admin:xtheme_snippet.list")
[docs]
def get(self, request, *args, **kwargs):
return HttpResponseNotAllowed(["post", "delete"])
[docs]
def get_queryset(self):
return Snippet.objects.filter(shop=get_shop(self.request))
[docs]
def delete(self, request, *args, **kwargs):
response = super().delete(request, *args, **kwargs)
shop = get_shop(self.request)
cache_key = GLOBAL_SNIPPETS_CACHE_KEY.format(shop_id=shop.pk)
cache.bump_version(cache_key)
return response