Skip to content

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

Add product directly to chosen delivery group

Anna Siwikcb5007428d06
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 8c83e7b9d3..23f73a1156 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -124,7 +124,6 @@ class MoveLinesForm(forms.Form):
'New shipment'),
label=pgettext_lazy('Move lines form label', 'Target shipment'))
-
def __init__(self, *args, **kwargs):
self.line = kwargs.pop('line')
super(MoveLinesForm, self).__init__(*args, **kwargs)
@@ -349,8 +348,8 @@ class ChangeStockForm(forms.ModelForm):
return self.instance
-class AddVariantToOrderForm(forms.Form):
- """ Adds variant in given quantity to existing or new group in order. """
+class AddVariantToDeliveryGroupForm(forms.Form):
+ """ Adds variant in given quantity to delivery group. """
variant = AjaxSelect2ChoiceField(
queryset=ProductVariant.objects.filter(
product__in=Product.objects.get_available_products()),
@@ -359,24 +358,14 @@ class AddVariantToOrderForm(forms.Form):
label=pgettext_lazy(
'Add variant to delivery group form label', 'Quantity'),
validators=[MinValueValidator(1)])
- target_group = forms.ModelChoiceField(
- queryset=DeliveryGroup.objects.none(), required=False,
- empty_label=pgettext_lazy(
- 'Delivery group value for `target_group` field',
- 'New shipment'),
- label=pgettext_lazy(
- 'Add variant to delivery group form label', 'Target shipment'))
def __init__(self, *args, **kwargs):
- self.order = kwargs.pop('order')
- 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 = queryset
+ self.group = kwargs.pop('group')
+ super(AddVariantToDeliveryGroupForm, self).__init__(*args, **kwargs)
def clean(self):
""" Checks if given quantity is available in stocks. """
- cleaned_data = super(AddVariantToOrderForm, self).clean()
+ cleaned_data = super(AddVariantToDeliveryGroupForm, self).clean()
variant = cleaned_data.get('variant')
quantity = cleaned_data.get('quantity')
if variant and quantity is not None:
@@ -396,8 +385,5 @@ class AddVariantToOrderForm(forms.Form):
""" 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')
- if not target_group:
- target_group = self.order.groups.create(status=OrderStatus.NEW)
- add_variant_to_delivery_group(target_group, variant, quantity)
- Order.objects.recalculate_order(self.order)
+ add_variant_to_delivery_group(self.group, variant, quantity)
+ Order.objects.recalculate_order(self.group.order)
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index 7ebc58bef0..72e4f9a0b3 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -14,7 +14,7 @@ from prices import Price
from satchless.item import InsufficientStock
from .forms import (
- AddVariantToOrderForm, CancelGroupForm, CancelLinesForm,
+ AddVariantToDeliveryGroupForm, CancelGroupForm, CancelLinesForm,
CancelOrderForm, CapturePaymentForm, ChangeStockForm, ChangeQuantityForm,
MoveLinesForm, OrderNoteForm, RefundPaymentForm, ReleasePaymentForm,
RemoveVoucherForm, ShipGroupForm)
@@ -312,27 +312,23 @@ def add_variant_to_group(request, order_pk, group_pk):
""" Adds variant in given quantity to existing or new group in order. """
order = get_object_or_404(Order, pk=order_pk)
group = get_object_or_404(order.groups.all(), pk=group_pk)
- form = AddVariantToOrderForm(request.POST or None, order=order)
+ form = AddVariantToDeliveryGroupForm(request.POST or None, group=group)
status = 200
if form.is_valid():
msg_dict = {
'quantity': form.cleaned_data.get('quantity'),
'variant': form.cleaned_data.get('variant'),
- 'group': form.cleaned_data.get('target_group')
+ 'group': group
}
try:
with transaction.atomic():
form.save()
- if form.cleaned_data.get('target_group') is None:
- msg_dict['group'] = order.groups.last()
msg = pgettext_lazy(
'Dashboard message related to a delivery group',
'Added %(quantity)d x %(variant)s to %(group)s') % msg_dict
order.create_history_entry(comment=msg, user=request.user)
messages.success(request, msg)
except InsufficientStock:
- if form.cleaned_data.get('target_group') is None:
- msg_dict['group'] = 'new shipment'
msg = pgettext_lazy(
'Dashboard message related to a delivery group',
'Insufficient stock: could not add %(quantity)d x '
diff --git a/templates/dashboard/order/modal/add_variant_to_order.html b/templates/dashboard/order/modal/add_variant_to_order.html
index f76d094cf8..b3f417fc98 100644
--- a/templates/dashboard/order/modal/add_variant_to_order.html
+++ b/templates/dashboard/order/modal/add_variant_to_order.html
@@ -14,10 +14,9 @@
</div>
</div>
<div class="row">
- <div class="col s12 m8 center-align">
- {{ form.variant|materializecss }}
- {{ form.quantity|materializecss }}
- {{ form.target_group|materializecss }}
+ <div class="col s12 m12 center-align">
+ {{ form.variant|materializecss:"s12 m8" }}
+ {{ form.quantity|materializecss:"s12 m4" }}
</div>
</div>
</div>
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index e2508c3343..c6a23fa1e9 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -517,7 +517,7 @@ def test_view_add_variant_to_delivery_group(
url = reverse(
'dashboard:add-variant-to-group', kwargs={
'order_pk': order.pk, 'group_pk': group.pk})
- data = {'variant': variant.pk, 'quantity': 2, 'target_group': group.pk}
+ data = {'variant': variant.pk, 'quantity': 2}
response = admin_client.post(url, data)