Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add bulk deleting for variants

Marcin Gębala7e62b12cb009
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index 97b1e1855c..81d806cf4c 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -102,6 +102,19 @@ class VariantAttributesForm(forms.ModelForm):
return super(VariantAttributesForm, self).save(commit=commit)
+class VariantsBulkDeleteForm(forms.Form):
+ variants = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple())
+
+ def __init__(self, *args, **kwargs):
+ product = kwargs.pop('product')
+ super(VariantsBulkDeleteForm, self).__init__(*args, **kwargs)
+ self.fields['variants'].choices = [(variant.pk, variant.name) for variant in product.variants.all()]
+
+ def delete(self):
+ variants = ProductVariant.objects.filter(pk__in=self.cleaned_data['variants'])
+ variants.delete()
+
+
def get_product_form(product):
if isinstance(product, Product):
return ProductForm
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 4e4ae2359b..82a4080e3f 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -14,7 +14,8 @@ from .forms import (ProductClassForm, get_product_form,
get_product_cls_by_name, get_variant_form,
ProductImageForm, get_verbose_name, StockForm,
get_variant_cls, ProductAttributeForm,
- AttributeChoiceValueFormset, VariantAttributesForm)
+ AttributeChoiceValueFormset, VariantAttributesForm,
+ VariantsBulkDeleteForm)
@staff_member_required
@@ -48,6 +49,7 @@ def product_details(request, pk=None, product_cls=None):
form_cls = get_product_form(product)
form = form_cls(instance=product, initial=initial)
+ variants_delete_form = VariantsBulkDeleteForm(product=product)
if 'product-form' in request.POST:
form = form_cls(request.POST, instance=product)
@@ -55,9 +57,16 @@ def product_details(request, pk=None, product_cls=None):
if creating and product:
return redirect('dashboard:product-update', pk=product.pk)
- ctx = {'title': title, 'product': product, 'attributes': attributes,
- 'images': images, 'product_form': form, 'stock_items': stock_items,
- 'variants': variants}
+ if 'variants-bulk-delete-form' in request.POST:
+ variants_delete_form = VariantsBulkDeleteForm(request.POST, product=product)
+ if variants_delete_form.is_valid():
+ variants_delete_form.delete()
+ return redirect('dashboard:product-update', pk=product.pk)
+
+ ctx = {
+ 'attributes': attributes, 'title': title, 'product': product,
+ 'images': images, 'product_form': form, 'stock_items': stock_items,
+ 'variants': variants, 'variants_delete_form': variants_delete_form}
if pk:
images_reorder_url = reverse_lazy('dashboard:product-images-reorder',
kwargs={'product_pk': pk})
diff --git a/saleor/dashboard/templates/dashboard/product/product_form.html b/saleor/dashboard/templates/dashboard/product/product_form.html
index 5d762febba..b62e0cd735 100644
--- a/saleor/dashboard/templates/dashboard/product/product_form.html
+++ b/saleor/dashboard/templates/dashboard/product/product_form.html
@@ -115,7 +115,8 @@
{% if product.pk %}
<div id="variants" class="card tab-content">
- <form>
+ <form method="POST">
+ {% csrf_token %}
<div class="data-table-header">
{% if attributes %}
<a href="{% url 'dashboard:variant-add' product_pk=product.pk %}" class="btn-data-table btn-show-when-unchecked blue-text">
@@ -125,7 +126,7 @@
<span>{% trans "You need to add some attributes to product to be able to add more variants." %}</span>
{% endif %}
- <button type="submit" class="btn-data-table btn-show-when-checked red-text" hidden>{% trans "Delete" %}</button>
+ <button name="variants-bulk-delete-form" type="submit" class="btn-data-table btn-show-when-checked red-text" hidden>{% trans "Delete" %}</button>
</div>
<table class="bordered hoverable data-table">
<thead>
@@ -150,8 +151,8 @@
<tr>
{% if attributes %}
<td class="bulk-checkbox">
- <input name="variant_id" type="checkbox" id="{{ variant.id }}" class="filled-in switch-actions">
- <label for="{{ variant.id }}"></label>
+ <input id="id_variants_{{ variant.id }}" name="variants" type="checkbox" value="{{ variant.id }}" class="filled-in switch-actions">
+ <label for="id_variants_{{ variant.id }}"></label>
</td>
{% endif %}
<td><a class="light-blue-text text-lighten-1" title="{% trans "Edit variant" %}"