Skip to content

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

Add bulk delete for stock using general BulkDeleteForm

Marcin Gębala0971d6de3919
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index 81d806cf4c..2ad4399ea5 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -102,17 +102,28 @@ class VariantAttributesForm(forms.ModelForm):
return super(VariantAttributesForm, self).save(commit=commit)
-class VariantsBulkDeleteForm(forms.Form):
- variants = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple())
+class BulkDeleteForm(forms.Form):
+ items = 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()]
+ choices = kwargs.pop('choices')
+ model = getattr(self, 'model', None)
+ if not model:
+ raise ValueError('BulkDeleteForm has no model specified')
+ super(BulkDeleteForm, self).__init__(*args, **kwargs)
+ self.fields['items'].choices = choices
def delete(self):
- variants = ProductVariant.objects.filter(pk__in=self.cleaned_data['variants'])
- variants.delete()
+ items = self.model.objects.filter(pk__in=self.cleaned_data['items'])
+ items.delete()
+
+
+class VariantsBulkDeleteForm(BulkDeleteForm):
+ model = ProductVariant
+
+
+class StockBulkDeleteForm(BulkDeleteForm):
+ model = Stock
def get_product_form(product):
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 82a4080e3f..5eef8f8714 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -15,7 +15,7 @@ from .forms import (ProductClassForm, get_product_form,
ProductImageForm, get_verbose_name, StockForm,
get_variant_cls, ProductAttributeForm,
AttributeChoiceValueFormset, VariantAttributesForm,
- VariantsBulkDeleteForm)
+ VariantsBulkDeleteForm, StockBulkDeleteForm)
@staff_member_required
@@ -47,9 +47,13 @@ def product_details(request, pk=None, product_cls=None):
variants = product.variants.select_subclasses()
stock_items = Stock.objects.filter(variant__in=variants)
+ variant_choices = [(variant.pk, variant.name) for variant in variants]
+ stock_choices = [(item.pk, item.pk) for item in stock_items]
+
form_cls = get_product_form(product)
form = form_cls(instance=product, initial=initial)
- variants_delete_form = VariantsBulkDeleteForm(product=product)
+ variants_delete_form = VariantsBulkDeleteForm(choices=variant_choices)
+ stock_delete_form = StockBulkDeleteForm(choices=stock_choices)
if 'product-form' in request.POST:
form = form_cls(request.POST, instance=product)
@@ -58,15 +62,23 @@ def product_details(request, pk=None, product_cls=None):
return redirect('dashboard:product-update', pk=product.pk)
if 'variants-bulk-delete-form' in request.POST:
- variants_delete_form = VariantsBulkDeleteForm(request.POST, product=product)
+ variants_delete_form = VariantsBulkDeleteForm(request.POST, choices=variant_choices)
if variants_delete_form.is_valid():
variants_delete_form.delete()
return redirect('dashboard:product-update', pk=product.pk)
+ if 'stock-bulk-delete-form' in request.POST:
+ stock_delete_form = StockBulkDeleteForm(request.POST, choices=stock_choices)
+ if stock_delete_form.is_valid():
+ stock_delete_form.delete()
+ return redirect('dashboard:product-update', pk=product.pk)
+ print stock_delete_form.errors
+
ctx = {
'attributes': attributes, 'title': title, 'product': product,
'images': images, 'product_form': form, 'stock_items': stock_items,
- 'variants': variants, 'variants_delete_form': variants_delete_form}
+ 'variants': variants, 'variants_delete_form': variants_delete_form,
+ 'stock_delete_form': stock_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 b62e0cd735..c9bca60d91 100644
--- a/saleor/dashboard/templates/dashboard/product/product_form.html
+++ b/saleor/dashboard/templates/dashboard/product/product_form.html
@@ -151,7 +151,7 @@
<tr>
{% if attributes %}
<td class="bulk-checkbox">
- <input id="id_variants_{{ variant.id }}" name="variants" type="checkbox" value="{{ variant.id }}" class="filled-in switch-actions">
+ <input id="id_variants_{{ variant.id }}" name="items" type="checkbox" value="{{ variant.id }}" class="filled-in switch-actions">
<label for="id_variants_{{ variant.id }}"></label>
</td>
{% endif %}
@@ -174,13 +174,14 @@
</div>
<div id="stock" class="card tab-content">
- <form>
+ <form method="POST">
+ {% csrf_token %}
<div class="data-table-header">
<a href="{% url 'dashboard:product-stock-add' product_pk=product.pk %}" class="btn-data-table btn-show-when-unchecked blue-text">
{% trans "Add" %}
</a>
- <button type="submit" class="btn-data-table btn-show-when-checked red-text" hidden>{% trans "Delete" %}</button>
+ <button name="stock-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">
{% if stock_items %}
@@ -200,8 +201,8 @@
{% for item in stock_items %}
<tr>
<td class="bulk-checkbox">
- <input name="variant_id" type="checkbox" id="{{ variant.id }}" class="filled-in switch-actions">
- <label for="{{ variant.id }}"></label>
+ <input name="items" type="checkbox" id="{{ item.id }}" class="filled-in switch-actions" value="{{ item.id }}">
+ <label for="{{ item.id }}"></label>
</td>
<td><a class="light-blue-text text-lighten-1" title="{% trans "Edit variant" %}"
href="{% url 'dashboard:product-stock-update' product_pk=product.pk stock_pk=item.pk %}">