Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Implement updating order quantities
Marcin Gębalaacd58deb94d9
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 33e9797d72..c81465bbed 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -1,7 +1,11 @@from django import formsfrom django.utils.translation import ugettext_lazy as _+from django.forms.formsets import BaseFormSet, DEFAULT_MAX_NUM+from satchless.item import InsufficientStock+from ...cart.forms import QuantityFieldfrom ...order.models import OrderNote+from ...product.models import Productclass OrderNoteForm(forms.ModelForm):@@ -20,3 +24,60 @@ class OrderNoteForm(forms.ModelForm):class ManagePaymentForm(forms.Form):amount = forms.DecimalField(min_value=0, decimal_places=2)+++class OrderLineForm(forms.Form):+ quantity = QuantityField()++ def __init__(self, *args, **kwargs):+ self.item = kwargs.pop('item')+ super(OrderLineForm, self).__init__(*args, **kwargs)++ def get_variant(self):+ p = Product.objects.select_subclasses().get(pk=self.item.product.pk)+ return p.variants.get(name=self.item.product_name)++ def clean_quantity(self):+ quantity = self.cleaned_data['quantity']+ variant = self.get_variant()+ try:+ variant.check_quantity(quantity)+ except InsufficientStock as e:+ msg = _('Only %(remaining)d remaining in stock.')+ raise forms.ValidationError(msg % {'remaining': e.item.stock})+ return quantity++ def save(self):+ quantity = self.cleaned_data['quantity']+ if quantity > 0:+ self.item.quantity = quantity+ self.item.save()+ else:+ self.item.delete()+++class OrderContentFormset(BaseFormSet):+ absolute_max = 9999+ can_delete = False+ can_order = False+ extra = 0+ form = OrderLineForm+ max_num = DEFAULT_MAX_NUM+ validate_max = False+ min_num = None+ validate_min = False++ def __init__(self, *args, **kwargs):+ self.order = kwargs.pop('order')+ kwargs['initial'] = [{'quantity': item.quantity}+ for item in self.order.get_items()]+ super(OrderContentFormset, self).__init__(*args, **kwargs)++ def _construct_form(self, i, **kwargs):+ kwargs['item'] = self.order.get_items()[i]+ return super(OrderContentFormset, self)._construct_form(i, **kwargs)++ def save(self):+ for form in self.forms:+ if form.is_valid():+ form.save()diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex c36b134a38..4207862d9b 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -3,12 +3,13 @@ from django.utils.translation import ugettext_lazy as _from django.views.generic import ListView, DetailView, UpdateViewfrom django.core.urlresolvers import reversefrom django.shortcuts import get_object_or_404+from django.core.exceptions import ValidationErrorfrom payments import PaymentErrorfrom ...order.models import Order, Addressfrom ...userprofile.forms import AddressFormfrom ..views import StaffMemberOnlyMixin, FilterByStatusMixin-from .forms import OrderNoteForm, ManagePaymentForm+from .forms import OrderNoteForm, ManagePaymentForm, OrderContentFormsetclass OrderListView(StaffMemberOnlyMixin, FilterByStatusMixin, ListView):@@ -26,6 +27,7 @@ class OrderDetails(StaffMemberOnlyMixin, DetailView):context_object_name = 'order'payment_form_class = ManagePaymentFormnote_form_class = OrderNoteForm+ order_formset_class = OrderContentFormsetdef get_queryset(self):qs = super(OrderDetails, self).get_queryset()@@ -56,6 +58,10 @@ class OrderDetails(StaffMemberOnlyMixin, DetailView):else:ctx['can_capture'] = ctx['can_release'] = ctx['can_refund'] = Falsectx['payment_form'] = self.payment_form_class()++ ctx['order_formset'] = self.order_formset_class(+ order=self.object)+return ctxdef get_delivery_info(self):@@ -73,6 +79,11 @@ class OrderDetails(StaffMemberOnlyMixin, DetailView):elif 'note_form' in request.POST:form = self.note_form_class(request.POST)self.handle_note_form(form)+ elif 'order_formset' in request.POST:+ formset = self.order_formset_class(+ request.POST or None,+ order=self.get_object())+ self.handle_order_formset(formset)return self.get(request, *args, **kwargs)def handle_release_action(self):@@ -124,6 +135,13 @@ class OrderDetails(StaffMemberOnlyMixin, DetailView):else:messages.error(self.request, _('Form has errors'))+ def handle_order_formset(self, formset):+ if formset.is_valid():+ formset.save()+ messages.success(self.request, _('Quantities updated'))+ else:+ messages.error(self.request, _('Problem with updating quantities'))+class AddressView(StaffMemberOnlyMixin, UpdateView):model = Addressdiff --git a/saleor/dashboard/templates/dashboard/order/detail.html b/saleor/dashboard/templates/dashboard/order/detail.htmlindex 49f45c58f0..f98e5fc632 100644--- a/saleor/dashboard/templates/dashboard/order/detail.html+++ b/saleor/dashboard/templates/dashboard/order/detail.html@@ -8,45 +8,57 @@{% block content %}<div class="row"><h1 class="page-header">Order #{{ order.id }}</h1>-<div class="col-lg-8"><div class="panel panel-default"><div class="panel-heading"><span class="panel-title">Order contents</span></div><div class="panel-body">- <table class="table">- <thead>- <tr>- <th class="col-sm-3">{% trans "Name" %}</th>- <th class="col-sm-3">{% trans "Variant" %}</th>- <th class="col-sm-3">{% trans "SKU" %}</th>- <th class="col-sm-3">{% trans "Price" %}</th>- </tr>- </thead>- <tbody>- {% for item in order.get_items %}+ <form method="post" action="">+ {{ order_formset.management_form }}+ <table class="table">+ <thead><tr>- <td class="col-sm-3"><a href="{% url "dashboard:product-update" pk=item.product_id %}">{{ item.product }}</a></td>- <td class="col-sm-3">{{ item.product_name }}</td>- <td class="col-sm-3">{{ item.product_sku }}</td>- <td class="col-sm-3">{{ item.quantity }} x {% gross item.get_price_per_item %}</td>+ <th class="col-sm-3">{% trans "Name" %}</th>+ <th class="col-sm-3">{% trans "Variant" %}</th>+ <th class="col-sm-3">{% trans "SKU" %}</th>+ <th class="col-sm-3">{% trans "Price" %}</th>+ <th class="col-sm-3">{% trans "Quantity" %}</th></tr>- {% endfor %}- </tbody>-- <tr>- <td colspan="4"><strong>{% trans "Order totals" %}</strong></td>- </tr>- <tr>- <td colspan="3">{% trans "Delivery" %}</td>- <td>{% gross order.get_delivery_total %}</td>- </tr>- <tr>- <td colspan="3">{% trans "Total" %}</td>- <td>{% gross order.get_total %}</td>- </tr>- </table>+ </thead>+ <tbody>+ {% for form in order_formset %}+ <tr>+ <td class="col-sm-3"><a href="{% url "dashboard:product-update" pk=form.item.product_id %}">{{ form.item.product }}</a></td>+ <td>{{ form.item.product_name }}</td>+ <td>{{ form.item.product_sku }}</td>+ <td>{% gross form.item.get_price_per_item %}</td>+ <td>{{ form.quantity }}</td>+ </tr>+ {% endfor %}+ </tbody>+ <tr>+ <td colspan="4"></td>+ <td>+ <button class="btn btn-default" type="submit" name="order_formset">+ {% csrf_token %}+ {% trans "Update quantities" %}+ </button>+ </td>+ </tr>+ <tr>+ <td colspan="5"><strong>{% trans "Order totals" %}</strong></td>+ </tr>+ <tr>+ <td colspan="4">{% trans "Delivery" %}</td>+ <td>{% gross order.get_delivery_total %}</td>+ </tr>+ <tr>+ <td colspan="4">{% trans "Total" %}</td>+ <td>{% gross order.get_total %}</td>+ </tr>+ </table>+ </form></div></div><div class="panel panel-default">