Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move selecting variant images to separate view
Marcin Gębalabc60296e1bc1
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.pyindex f890840888..7146285197 100644--- a/saleor/dashboard/product/forms.py+++ b/saleor/dashboard/product/forms.py@@ -1,7 +1,6 @@from __future__ import unicode_literalsfrom django import forms-from django.db import transactionfrom django.db.models import Countfrom django.forms.models import ModelChoiceIterator, inlineformset_factoryfrom django.utils.encoding import smart_text@@ -243,29 +242,26 @@ class ProductImageForm(forms.ModelForm):def __init__(self, *args, **kwargs):super(ProductImageForm, self).__init__(*args, **kwargs)- show_variants = self.instance.product.product_class.has_variants- if self.instance.product and show_variants:- variants = self.fields['variants']- variants.queryset = self.instance.product.variants.all()- variants.initial = self.instance.variant_images.values_list(- 'variant', flat=True)if self.instance.image:self.fields['image'].widget = ImagePreviewWidget()- @transaction.atomic- def save_variant_images(self, instance):- variant_images = []- # Clean up old mapping- instance.variant_images.all().delete()- for variant in self.cleaned_data['variants']:- variant_images.append(- VariantImage(variant=variant, image=instance))- VariantImage.objects.bulk_create(variant_images)- def save(self, commit=True):- instance = super(ProductImageForm, self).save(commit=commit)- self.save_variant_images(instance)- return instance+class VariantImagesSelectForm(forms.Form):+ images = forms.ModelMultipleChoiceField(+ queryset=VariantImage.objects.none())++ def __init__(self, *args, **kwargs):+ self.variant = kwargs.pop('variant')+ super(VariantImagesSelectForm, self).__init__(*args, **kwargs)+ self.fields['images'].queryset = self.variant.product.images.all()+ self.fields['images'].initial = self.variant.images.all()++ def save(self):+ images = []+ self.variant.images.clear()+ for image in self.cleaned_data['images']:+ images.append(VariantImage(variant=self.variant, image=image))+ VariantImage.objects.bulk_create(images)class ProductAttributeForm(forms.ModelForm):diff --git a/saleor/dashboard/product/urls.py b/saleor/dashboard/product/urls.pyindex c1d6dac510..a68ac3ef60 100644--- a/saleor/dashboard/product/urls.py+++ b/saleor/dashboard/product/urls.py@@ -33,6 +33,8 @@ urlpatterns = [views.variant_edit, name='variant-update'),url(r'^(?P<product_pk>[0-9]+)/variants/(?P<variant_pk>[0-9]+)/delete/$',views.variant_delete, name='variant-delete'),+ url(r'^(?P<product_pk>[0-9]+)/variants/(?P<variant_pk>[0-9]+)/images/$',+ views.variant_images, name='variant-images'),url(r'^(?P<product_pk>[0-9]+)/variants/bulk_delete/',views.variants_bulk_delete, name='variant-bulk-delete'),diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex 6ac7cd8f4b..df671cdf61 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -1,7 +1,6 @@from __future__ import unicode_literalsfrom django.contrib import messages-from django.core.urlresolvers import reversefrom django.shortcuts import get_object_or_404, redirectfrom django.template.response import TemplateResponsefrom django.utils.http import is_safe_url@@ -347,14 +346,33 @@ def variant_edit(request, product_pk, variant_pk=None):@staff_member_requireddef variant_details(request, product_pk, variant_pk):product = get_object_or_404(Product, pk=product_pk)- variant = get_object_or_404(- product.variants.prefetch_related('stock'), pk=variant_pk)+ qs = product.variants.prefetch_related('images', 'stock')+ variant = get_object_or_404(qs, pk=variant_pk)stock = variant.stock.all()- ctx = {'product': product, 'variant': variant, 'stock': stock}+ images = variant.images.all()+ ctx = {+ 'images': images, 'product': product, 'stock': stock,+ 'variant': variant}return TemplateResponse(request, 'dashboard/product/variant_details.html', ctx)+@staff_member_required+def variant_images(request, product_pk, variant_pk):+ product = get_object_or_404(Product, pk=product_pk)+ qs = product.variants.prefetch_related('images')+ variant = get_object_or_404(qs, pk=variant_pk)+ form = forms.VariantImagesSelectForm(request.POST or None, variant=variant)+ if form.is_valid():+ form.save()+ return redirect(+ 'dashboard:variant-details', product_pk=product.pk,+ variant_pk=variant.pk)+ ctx = {'form': form, 'product': product, 'variant': variant}+ return TemplateResponse(+ request, 'dashboard/product/modal_select_variant_images.html', ctx)++@staff_member_requireddef variant_delete(request, product_pk, variant_pk):product = get_object_or_404(Product, pk=product_pk)diff --git a/templates/dashboard/product/modal_select_variant_images.html b/templates/dashboard/product/modal_select_variant_images.htmlnew file mode 100644index 0000000000..4513c9be0a--- /dev/null+++ b/templates/dashboard/product/modal_select_variant_images.html@@ -0,0 +1,22 @@+{% load i18n %}+{% load materializecss %}++<form action="{% url "dashboard:variant-images" product_pk=product.pk variant_pk=variant.pk %}" method="post" novalidate>+ {% csrf_token %}+ <div class="modal-content">+ <h5>+ {% trans "Select variant images" context "Dashboard variant images modal" %}+ </h5>+ <div class="row">+ {{ form|materializecss }}+ </div>+ </div>+ <div class="modal-footer">+ <button type="submit" class="modal-action btn-flat">+ {% trans "Save" context "Dashboard modal action" %}+ </button>+ <a href="#!" class="modal-action modal-close btn-flat">+ {% trans "Cancel" context "Dashboard cancel action" %}+ </a>+ </div>+</form>diff --git a/templates/dashboard/product/product_image_form.html b/templates/dashboard/product/product_image_form.htmlindex c70635e8af..94fda419a3 100644--- a/templates/dashboard/product/product_image_form.html+++ b/templates/dashboard/product/product_image_form.html@@ -74,13 +74,6 @@</p>{% endif %}</div>- <div class="col s12 m4">- {% if show_variants %}- <div class="row">- {{ form.variants|materializecss }}- </div>- {% endif %}- </div></div><div class="row">{{ form.alt|materializecss }}diff --git a/templates/dashboard/product/variant_details.html b/templates/dashboard/product/variant_details.htmlindex fd15142639..a1b93fb9e3 100644--- a/templates/dashboard/product/variant_details.html+++ b/templates/dashboard/product/variant_details.html@@ -40,10 +40,16 @@<div class="col m12 l7"><div class="card"><div class="card-content">- <span class="card-title">{{ variant }}</span>+ <span class="card-title">{{ product }} {{ variant }}</span>+ <p>+ {% trans "SKU" context "Dashboard variant details view" %} {{ variant.sku }}+ </p><p>{% trans "Sale price" context "Dashboard variant details view" %} {% gross variant.get_price %}</p>+ <p>+ {% trans "Cost price" context "Dashboard variant details view" %} {% if variant.get_cost_price %}{% gross variant.get_cost_price %}{% else %}-{% endif %}+ </p></div><div class="card-action"><a class="btn btn-flat" href="{% url 'dashboard:variant-update' product_pk=product.pk variant_pk=variant.pk %}">@@ -90,15 +96,28 @@<div class="col m12 l2"><div class="card"><div class="card-content">- <span>AVAILABLE</span>+ {% if variant.is_in_stock %}+ <span>{% trans "available" context "Dashboard variant details view" %}</span>+ {% else %}+ <span>{% trans "out of stock" context "Dashboard variant details view" %}</span>+ {% endif %}</div></div><div class="card"><div class="card-content"><span class="card-title">{% trans "Images" context "Dashboard variant details view" %}</span>+ <p>+ {% for image in images %}+ <a href="{% url "dashboard:product-image-update" product_pk=product.pk img_pk=image.pk %}">+ <img class="responsive-img" src="{{ image.image.crop.60x60 }}" srcset="{{ image.image.crop.60x60 }} 1x" alt="">+ </a>+ {% empty %}+ {% trans "No images" %}+ {% endfor %}+ </p></div><div class="card-action">- <a class="btn btn-flat" href="">+ <a class="btn btn-flat modal-trigger-custom" href="#base-modal" data-href="{% url 'dashboard:variant-images' product_pk=product.pk variant_pk=variant.pk %}">{% trans "Select images" context "Dashboard action" %}</a></div>