Source code for shuup.admin.modules.product_types.views.edit
# -*- coding: utf-8 -*-
# This file is part of Shuup.
#
# Copyright (c) 2012-2021, Shuup Commerce Inc. All rights reserved.
#
# This source code is licensed under the OSL-3.0 license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _
from shuup.admin.forms.fields import Select2MultipleField
from shuup.admin.toolbar import PostActionButton, get_default_edit_toolbar
from shuup.admin.utils.views import CreateOrUpdateView
from shuup.core.models import Attribute, ProductType
from shuup.utils.django_compat import reverse_lazy
from shuup.utils.multilanguage_model_form import MultiLanguageModelForm
[docs]class ProductTypeForm(MultiLanguageModelForm):
attributes = Select2MultipleField(
model=Attribute,
required=False,
help_text=_(
"Select attributes that go with your product type. These are defined in Products Settings - Attributes."
),
)
def __init__(self, **kwargs):
super(ProductTypeForm, self).__init__(**kwargs)
if self.instance.pk:
choices = [(a.pk, a.name) for a in self.instance.attributes.all()]
self.fields["attributes"].initial = [pk for pk, name in choices]
[docs] def clean_attributes(self):
attributes = [int(a_id) for a_id in self.cleaned_data.get("attributes", [])]
return Attribute.objects.filter(pk__in=attributes).all()
[docs] def save(self, commit=True):
obj = super(ProductTypeForm, self).save(commit=commit)
obj.attributes.clear()
obj.attributes.set(self.cleaned_data["attributes"])
return self.instance
[docs]class ProductTypeEditView(CreateOrUpdateView):
model = ProductType
form_class = ProductTypeForm
template_name = "shuup/admin/product_types/edit.jinja"
context_object_name = "product_type"
[docs] def get_toolbar(self):
product_type = self.get_object()
save_form_id = self.get_save_form_id()
delete_url = (
reverse_lazy("shuup_admin:product_type.delete", kwargs={"pk": product_type.pk}) if product_type.pk else None
)
toolbar = get_default_edit_toolbar(self, save_form_id)
if not delete_url:
return toolbar
toolbar.append(
PostActionButton(
post_url=delete_url,
text=_("Delete"),
icon="fa fa-trash",
extra_css_class="btn-danger",
confirm=_(
"Are you sure you wish to delete %s? Warrning: all related products will disappear "
"from storefront until new value for product type is set!"
)
% product_type, # noqa
required_permissions=(),
)
)
return toolbar