Skip to content

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

Handling product form and variants formset separately

Marcin Gębaladbc4e513adf8
diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.py
index 1c2c1b45eb..6e39ae967e 100644
--- a/saleor/dashboard/product/views.py
+++ b/saleor/dashboard/product/views.py
@@ -6,6 +6,7 @@ from django.db import transaction
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.translation import ugettext_lazy as _
+from django.views.decorators.http import require_POST
from django.views.generic import DeleteView
from ...product.models import Product, ProductImage
@@ -30,42 +31,64 @@ def product_list(request):
@staff_member_required
def product_details(request, pk=None, product_cls=None):
- if pk:
- product = get_object_or_404(Product.objects.select_subclasses(), pk=pk)
- title = product.name
- else:
+ creating = pk is None
+ if creating:
product = get_product_cls_by_name(product_cls)()
title = _('Add new %s') % product_cls
+ else:
+ product = get_object_or_404(Product.objects.select_subclasses(), pk=pk)
+ title = product.name
images = product.images.all()
form_cls = get_product_form(product)
+ form = form_cls(instance=product)
variant_formset_cls = get_variant_formset(product)
- form = form_cls(request.POST or None, instance=product)
- variant_formset = variant_formset_cls(
- request.POST or None, instance=product)
- forms = {'form': form, 'variant_formset': variant_formset}
- if all([f.is_valid() for f in forms.values()]):
- with transaction.atomic():
- product = form.save()
- variant_formset.save()
- if pk:
- msg = _('Updated product %s') % product
- else:
- msg = _('Added product %s') % product
- messages.success(request, msg)
- return redirect('dashboard:products')
- else:
- if any([f.errors for f in forms.values()]):
- messages.error(request, _('Your submitted data was not valid - '
- 'please correct the errors below'))
- ctx = {'title': title, 'product': product, 'images': images}
- ctx.update(forms)
+ variant_formset = variant_formset_cls(instance=product)
+
+ if 'product-form' in request.POST:
+ form = form_cls(request.POST, instance=product)
+ product = save_product_form(request, form, creating)
+ if creating and product:
+ return redirect('dashboard:product-update', pk=product.pk)
+
+ if 'variants-formset' in request.POST:
+ variant_formset = variant_formset_cls(request.POST, instance=product)
+ save_variants_formset(request, variant_formset)
+
+ ctx = {'title': title, 'product': product, 'images': images,
+ 'product_form': form, 'variant_formset': variant_formset}
if pk:
images_reorder_url = reverse_lazy('dashboard:product-images-reorder',
- kwargs={'product_pk': pk})
+ kwargs={'product_pk': pk})
ctx['images_reorder_url'] = images_reorder_url
return TemplateResponse(request, 'dashboard/product/product_form.html', ctx)
+def save_product_form(request, form, creating):
+ product = None
+ if form.is_valid():
+ product = form.save()
+ if creating:
+ msg = _('Added product %s') % product
+ else:
+ msg = _('Updated product %s') % product
+ messages.success(request, msg)
+ else:
+ if form.errors:
+ messages.error(request, _('Your submitted data was not valid - '
+ 'please correct the errors below'))
+ return product
+
+
+def save_variants_formset(request, formset):
+ if formset.is_valid():
+ formset.save()
+ messages.success(request, _('Variants has been saved'))
+ else:
+ if formset.errors:
+ messages.error(request, _('Your submitted data was not valid - '
+ 'please correct the errors below'))
+
+
class ProductDeleteView(StaffMemberOnlyMixin, DeleteView):
model = Product
template_name = 'dashboard/product/product_confirm_delete.html'
diff --git a/saleor/dashboard/templates/dashboard/product/product_form.html b/saleor/dashboard/templates/dashboard/product/product_form.html
index b74a81db3d..533fcaf729 100644
--- a/saleor/dashboard/templates/dashboard/product/product_form.html
+++ b/saleor/dashboard/templates/dashboard/product/product_form.html
@@ -36,97 +36,107 @@
<div class="col l8 offset-l2">
<div class="row">
{% csrf_token %}
- {{ form|materializecss }}
+ {{ product_form|materializecss }}
</div>
<div class="row">
<div class="col s12 right-align">
<a href="{% url 'dashboard:products' %}" class="btn-flat"><i class="mdi-content-clear"></i> {% trans "Cancel" %}</a>
{% if product.pk %}
- <button type="submit" class="btn"><i class="mdi-content-save"></i> {% trans "Update" %}</button>
+ <button type="submit" name="product-form" class="btn"><i class="mdi-content-save"></i> {% trans "Update" %}</button>
{% else %}
- <button type="submit" class="btn"><i class="mdi-content-create"></i> {% trans "Create" %}</button>
+ <button type="submit" name="product-form" class="btn"><i class="mdi-content-create"></i> {% trans "Create" %}</button>
{% endif %}
</div>
</div>
</div>
</div>
+</form>
{% if product.pk %}
- <div class="row" id="variants">
- <div class="col l12">
- <div class="row">
- {{ variant_formset.management_form }}
- <table class="striped">
- <thead>
- <tr>
- <th>{% trans "SKU" %}</th>
- {% for field in variant_formset.0.visible_fields %}
- {% if field.name != 'sku' and field.name != 'DELETE' %}
- <th>{{ field.label }}</th>
- {% endif %}
- {% endfor %}
- <th class="text-right"><i class="glyphicon glyphicon-trash"></i></th>
- </tr>
- </thead>
- <tbody>
- {% for form in variant_formset.initial_forms %}
- <tr>
- <td>
- {% for field in form.hidden_fields %}
- {{ field }}
+ <form method="post">
+ <div class="row" id="variants">
+ <div class="col l12">
+ <div class="row">
+ {% csrf_token %}
+ {{ variant_formset.management_form }}
+ <table class="striped">
+ <thead>
+ <tr>
+ <th>{% trans "SKU" %}</th>
+ {% for field in variant_formset.0.visible_fields %}
+ {% if field.name != 'sku' and field.name != 'DELETE' %}
+ <th>{{ field.label }}</th>
+ {% endif %}
{% endfor %}
- {{ form.sku|materializecss }}
- </td>
- {% for field in form.visible_fields %}
- {% if field.name != 'sku' and field.name != 'DELETE' %}
- <td>
- <div class="{% if field.errors %} has-error{% endif %}{% if field.required %} required{% endif %}">
- {{ field|materializecss }}
- {% if field.errors %}
- {% for error in field.errors %}
- <span class="label label-danger">{{ error }}</span>
- {% endfor %}
- {% endif %}
- </div>
- </td>
- {% endif %}
- {% endfor %}
- <td class="text-right">
- {{ form.DELETE }}
- </td>
- </tr>
- {% endfor %}
- {% for form in variant_formset.extra_forms %}
- <tr>
- <td>
- {% for field in form.hidden_fields %}
- {{ field }}
+ <th class="text-right"><i class="glyphicon glyphicon-trash"></i></th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for form in variant_formset.initial_forms %}
+ <tr>
+ <td>
+ {% for field in form.hidden_fields %}
+ {{ field }}
+ {% endfor %}
+ {{ form.sku|materializecss }}
+ </td>
+ {% for field in form.visible_fields %}
+ {% if field.name != 'sku' and field.name != 'DELETE' %}
+ <td>
+ <div class="{% if field.errors %} has-error{% endif %}{% if field.required %} required{% endif %}">
+ {{ field|materializecss }}
+ {% if field.errors %}
+ {% for error in field.errors %}
+ <span class="label label-danger">{{ error }}</span>
+ {% endfor %}
+ {% endif %}
+ </div>
+ </td>
+ {% endif %}
{% endfor %}
- {{ form.sku|materializecss }}
- </td>
- {% for field in form.visible_fields %}
- {% if field.name != 'sku' and field.name != 'DELETE' %}
- <td>
- <div class="{% if field.errors %} has-error{% endif %}{% if field.required %} required{% endif %}">
- {{ field|materializecss }}
- {% if field.errors %}
- {% for error in field.errors %}
- <span class="label label-danger">{{ error }}</span>
- {% endfor %}
- {% endif %}
- </div>
- </td>
- {% endif %}
- {% endfor %}
- <td></td>
- </tr>
- {% endfor %}
- </tbody>
- </table>
+ <td class="text-right">
+ {{ form.DELETE }}
+ </td>
+ </tr>
+ {% endfor %}
+ {% for form in variant_formset.extra_forms %}
+ <tr>
+ <td>
+ {% for field in form.hidden_fields %}
+ {{ field }}
+ {% endfor %}
+ {{ form.sku|materializecss }}
+ </td>
+ {% for field in form.visible_fields %}
+ {% if field.name != 'sku' and field.name != 'DELETE' %}
+ <td>
+ <div class="{% if field.errors %} has-error{% endif %}{% if field.required %} required{% endif %}">
+ {{ field|materializecss }}
+ {% if field.errors %}
+ {% for error in field.errors %}
+ <span class="label label-danger">{{ error }}</span>
+ {% endfor %}
+ {% endif %}
+ </div>
+ </td>
+ {% endif %}
+ {% endfor %}
+ <td></td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+ </div>
+ <div class="row">
+ <div class="col s12 right-align">
+ <a href="{% url 'dashboard:products' %}" class="btn-flat"><i class="mdi-content-clear"></i> {% trans "Cancel" %}</a>
+ <button type="submit" name="variants-formset" class="btn"><i class="mdi-content-save"></i> {% trans "Update" %}</button>
+ </div>
+ </div>
</div>
</div>
- </div>
+ </form>
<div id="images">
<div class="row">
@@ -163,5 +173,5 @@
</div>
</div>
{% endif %}
-</form>
+
{% endblock %}