Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Rename form to add variant to order
Anna Siwikc42a9e69c1b8
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 6b4b391f45..fe9a352062 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -355,8 +355,8 @@ class ChangeStockForm(forms.ModelForm):return self.instance-class AddVariantToDeliveryGroupForm(forms.Form):- """ Adds variant in given quantity to delivery group. """+class AddVariantToOrderForm(forms.Form):+ """ Adds variant in given quantity to existing or new group in order. """variant = AjaxSelect2ChoiceField(queryset=ProductVariant.objects.filter(product__in=Product.objects.get_available_products()),@@ -375,15 +375,14 @@ class AddVariantToDeliveryGroupForm(forms.Form):def __init__(self, *args, **kwargs):self.order = kwargs.pop('order')- super(AddVariantToDeliveryGroupForm, self).__init__(*args, **kwargs)- self.fields['target_group'].queryset = (- self.order.groups.exclude(- status__in={OrderStatus.CANCELLED, OrderStatus.SHIPPED})- )+ super(AddVariantToOrderForm, self).__init__(*args, **kwargs)+ excluded_statuses = {OrderStatus.CANCELLED, OrderStatus.SHIPPED}+ queryset = self.order.groups.exclude(status__in=excluded_statuses)+ self.fields['target_group'].queryset = querysetdef clean(self):""" Checks if given quantity is available in stocks. """- cleaned_data = super(AddVariantToDeliveryGroupForm, self).clean()+ cleaned_data = super(AddVariantToOrderForm, self).clean()variant = cleaned_data.get('variant')quantity = cleaned_data.get('quantity')if variant and quantity is not None:@@ -400,7 +399,7 @@ class AddVariantToDeliveryGroupForm(forms.Form):return cleaned_datadef save(self):- """ Adds variant to delivery group. Updates stocks and order. """+ """ Adds variant to target group. Updates stocks and order. """variant = self.cleaned_data.get('variant')quantity = self.cleaned_data.get('quantity')target_group = self.cleaned_data.get('target_group')diff --git a/saleor/dashboard/order/urls.py b/saleor/dashboard/order/urls.pyindex 7aa9a1d0b4..fc1f546ded 100644--- a/saleor/dashboard/order/urls.py+++ b/saleor/dashboard/order/urls.py@@ -35,9 +35,8 @@ urlpatterns = [views.ship_delivery_group, name='ship-delivery-group'),url(r'^(?P<order_pk>\d+)/shipment/(?P<group_pk>\d+)/cancel/$',views.cancel_delivery_group, name='cancel-delivery-group'),- url(r'^(?P<order_pk>\d+)/add-item/$',- views.add_variant_to_delivery_group,- name='add-variant-delivery-group'),+ url(r'^(?P<order_pk>\d+)/add-variant/$',+ views.add_variant_to_order, name='add-variant-to-order'),url(r'^(?P<order_pk>\d+)/invoice/$',views.order_invoice, name='order-invoice'),url(r'^(?P<group_pk>\d+)/packing-slips/$',diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex 3f40dbc71f..5df47fcff6 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -14,7 +14,7 @@ from prices import Pricefrom satchless.item import InsufficientStockfrom .forms import (- AddVariantToDeliveryGroupForm, CancelGroupForm, CancelLinesForm,+ AddVariantToOrderForm, CancelGroupForm, CancelLinesForm,CancelOrderForm, CapturePaymentForm, ChangeStockForm, ChangeQuantityForm,MoveLinesForm, OrderNoteForm, RefundPaymentForm, ReleasePaymentForm,RemoveVoucherForm, ShipGroupForm)@@ -308,9 +308,10 @@ def cancel_delivery_group(request, order_pk, group_pk):@staff_member_required@permission_required('order.edit_order')-def add_variant_to_delivery_group(request, order_pk):+def add_variant_to_order(request, order_pk):+ """ Adds variant in given quantity to existing or new group in order. """order = get_object_or_404(Order, pk=order_pk)- form = AddVariantToDeliveryGroupForm(request.POST or None, order=order)+ form = AddVariantToOrderForm(request.POST or None, order=order)status = 200if form.is_valid():msg_dict = {@@ -340,7 +341,7 @@ def add_variant_to_delivery_group(request, order_pk):elif form.errors:status = 400ctx = {'order': order, 'form': form}- template = 'dashboard/order/modal/add_variant_to_delivery_group.html'+ template = 'dashboard/order/modal/add_variant_to_order.html'return TemplateResponse(request, template, ctx, status=status)diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex dc18af8575..8b075cd3e5 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -89,7 +89,7 @@ def add_variant_to_delivery_group(if quantity_left > stock.quantity_availableelse quantity_left)- item = group.items.create(+ group.items.create(product=variant.product,product_name=variant.display_product(),product_sku=variant.sku,@@ -114,20 +114,21 @@ def add_variant_to_existing_lines(group, variant, total_quantity):Returns quantity that could not be fulfilled with existing lines."""- items = group.items.filter(+ # order descending by lines' stock available quantity+ lines = group.items.filter(product=variant.product, product_sku=variant.sku,stock__isnull=False).order_by(F('stock__quantity_allocated') - F('stock__quantity'))quantity_left = total_quantity- for item in items:+ for line in lines:quantity_added = (- item.stock.quantity_available- if quantity_left > item.stock.quantity_available+ line.stock.quantity_available+ if quantity_left > line.stock.quantity_availableelse quantity_left)- item.quantity += quantity_added- item.save()- Stock.objects.allocate_stock(item.stock, quantity_added)+ line.quantity += quantity_added+ line.save()+ Stock.objects.allocate_stock(line.stock, quantity_added)quantity_left -= quantity_addedif quantity_left == 0:breakdiff --git a/templates/dashboard/order/detail.html b/templates/dashboard/order/detail.htmlindex 894f8d91c0..b83cdd4421 100644--- a/templates/dashboard/order/detail.html+++ b/templates/dashboard/order/detail.html@@ -184,33 +184,33 @@</table></div>{% if perms.order.edit_order %}- {% if can_capture or can_refund or can_release %}<div class="data-table-action">- {% if can_capture %}- <a href="#base-modal" data-href="{% url 'dashboard:capture-payment' order_pk=order.pk payment_pk=payment.pk %}"- class="btn btn-flat modal-trigger-custom">- {% trans "Capture" context "Order summary card action" %}- </a>- {% elif can_refund %}- <a href="#base-modal" data-href="{% url 'dashboard:refund-payment' order_pk=order.pk payment_pk=payment.pk %}"- class="btn btn-flat modal-trigger-custom">- {% trans "Refund" context "Order summary card action" %}- </a>- {% endif %}- {% if can_release %}- <a href="#base-modal" data-href="{% url 'dashboard:release-payment' order_pk=order.pk payment_pk=payment.pk %}"- class="btn btn-flat modal-trigger-custom">- {% trans "Release" context "Order summary card action" %}+ {% if can_capture or can_refund or can_release %}+ {% if can_capture %}+ <a href="#base-modal" data-href="{% url 'dashboard:capture-payment' order_pk=order.pk payment_pk=payment.pk %}"+ class="btn btn-flat modal-trigger-custom">+ {% trans "Capture" context "Order summary card action" %}+ </a>+ {% elif can_refund %}+ <a href="#base-modal" data-href="{% url 'dashboard:refund-payment' order_pk=order.pk payment_pk=payment.pk %}"+ class="btn btn-flat modal-trigger-custom">+ {% trans "Refund" context "Order summary card action" %}+ </a>+ {% endif %}+ {% if can_release %}+ <a href="#base-modal" data-href="{% url 'dashboard:release-payment' order_pk=order.pk payment_pk=payment.pk %}"+ class="btn btn-flat modal-trigger-custom">+ {% trans "Release" context "Order summary card action" %}+ </a>+ {% endif %}+ <a href="{% url 'dashboard:order-invoice' order_pk=order.pk %}" data-href="" class="btn btn-flat" target="_blank">+ {% trans "Invoice" context "Order detail action" %}</a>{% endif %}- <a href="{% url 'dashboard:order-invoice' order_pk=order.pk %}" data-href="" class="btn btn-flat" target="_blank">- {% trans "Invoice" context "Order detail action" %}- </a>- <a href="#base-modal" data-href="{% url 'dashboard:add-variant-delivery-group' order_pk=order.pk %}" class="btn btn-flat modal-trigger-custom" title="{% trans "Add item to group" context "Shipment group table action" %}">- {% trans "Add item" context "Shipment group table action" %}+ <a href="#base-modal" data-href="{% url 'dashboard:add-variant-to-order' order_pk=order.pk %}" class="btn btn-flat modal-trigger-custom" title="{% trans "Add variant to order" context "Order summary card action title" %}">+ {% trans "Add variant" context "Order summary card action" %}</a></div>- {% endif %}{% endif %}</div>{% with order.shipping_address as address %}diff --git a/templates/dashboard/order/modal/add_variant_to_delivery_group.html b/templates/dashboard/order/modal/add_variant_to_order.htmlsimilarity index 62%rename from templates/dashboard/order/modal/add_variant_to_delivery_group.htmlrename to templates/dashboard/order/modal/add_variant_to_order.htmlindex c25795b975..80fbfc836c 100644--- a/templates/dashboard/order/modal/add_variant_to_delivery_group.html+++ b/templates/dashboard/order/modal/add_variant_to_order.html@@ -1,11 +1,11 @@{% load i18n %}{% load materializecss %}-<form id="add-variant-delivery-group-form" method="post" role="form" action="{% url 'dashboard:add-variant-delivery-group' order_pk=order.pk %}" class="form-async" novalidate>+<form id="add-variant-to-order-form" method="post" role="form" action="{% url 'dashboard:add-variant-to-order' order_pk=order.pk %}" class="form-async" novalidate>{% csrf_token %}<div class="modal-content"><h5>- {% trans "Add item to delivery group" context "Modal delivery group add item title" %}+ {% trans "Add variant to order" context "Modal order add variant title" %}</h5><div class="row">{{ form.variant|materializecss:"s12 m6" }}@@ -15,7 +15,7 @@</div><div class="modal-footer"><button type="submit" class="modal-action btn-flat">- {% trans "Add" context "Modal delivery group add item primary action" %}+ {% trans "Add" context "Modal order add variant primary action" %}</button><a href="#!" aria-hidden="true" class="modal-action modal-close btn-flat">{% trans "Cancel" context "Dashboard cancel action" %}diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.pyindex cc0b21ba32..6d1102251f 100644--- a/tests/dashboard/test_order.py+++ b/tests/dashboard/test_order.py@@ -514,10 +514,8 @@ def test_view_add_variant_to_delivery_group(line = OrderedItem.objects.get(product_sku='SKU_A', stock_location='Warehouse 2')url = reverse(- 'dashboard:add-variant-delivery-group', kwargs={- 'order_pk': order.pk, 'group_pk': group.pk- })- data = {'variant': variant.pk, 'quantity': 2}+ 'dashboard:add-variant-to-order', kwargs={'order_pk': order.pk})+ data = {'variant': variant.pk, 'quantity': 2, 'target_group': group.pk}response = admin_client.post(url, data)