Skip to content

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

Split views for editing orderline

Marcin Gębala210151229650
diff --git a/saleor/dashboard/order/urls.py b/saleor/dashboard/order/urls.py
index 1abcfe4e76..d21346617b 100644
--- a/saleor/dashboard/order/urls.py
+++ b/saleor/dashboard/order/urls.py
@@ -11,10 +11,10 @@ urlpatterns = [
views.address_view, name='address-edit'),
url(r'^payment/(?P<pk>[0-9]+)/(?P<action>capture|refund|release)/$',
views.manage_payment, name='manage-payment'),
- url(r'^line/(?P<pk>[0-9]+)/$', views.edit_order_line,
- name='order-line-edit'),
- url(r'^line/(?P<pk>[0-9]+)/(?P<action>change_quantity|move_items)/$',
- views.edit_order_line, name='order-line-edit'),
+ url(r'^line/change/(?P<pk>[0-9]+)/$', views.orderline_change_quantity,
+ name='orderline-change-quantity'),
+ url(r'^line/split/(?P<pk>[0-9]+)/$', views.orderline_split,
+ name='orderline-split'),
url(r'^ship/(?P<pk>[0-9]+)/$', views.ship_delivery_group,
name='ship-delivery-group')
]
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index f3db7c12f4..248ee972c8 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -110,53 +110,52 @@ def manage_payment(request, pk, action):
@staff_member_required
-def edit_order_line(request, pk, action=None):
+def orderline_change_quantity(request, pk):
item = get_object_or_404(OrderedItem, pk=pk)
order = item.delivery_group.order
- quantity_form = ChangeQuantityForm(request.POST or None, instance=item)
- move_items_form = MoveItemsForm(request.POST or None, item=item)
+ form = ChangeQuantityForm(request.POST or None, instance=item)
+ if form.is_valid():
+ old_quantity = item.quantity
+ with transaction.atomic():
+ form.save()
+ msg = _(
+ 'Changed quantity for product %(product)s from'
+ ' %(old_quantity)s to %(new_quantity)s') % {
+ 'product': item.product, 'old_quantity': old_quantity,
+ 'new_quantity': item.quantity}
+ messages.success(request, msg)
+ order.create_history_entry(comment=msg, user=request.user)
+ return redirect('dashboard:order-details', pk=order.pk)
+ ctx = {'object': item, 'form': form}
+ ctx.update(csrf(request))
+ template = 'dashboard/order/modal_change_quantity.html'
+ return TemplateResponse(request, template, ctx)
- if not action:
- ctx = {'object': item, 'change_quantity_form': quantity_form,
- 'move_items_form': move_items_form}
- ctx.update(csrf(request))
- template = 'dashboard/order/modal_order_line_edit.html'
- return TemplateResponse(request, template, ctx)
- if action == 'change_quantity':
- old_quantity = item.quantity
- if quantity_form.is_valid():
- with transaction.atomic():
- quantity_form.save()
- msg = _(
- 'Changed quantity for product %(product)s from'
- ' %(old_quantity)s to %(new_quantity)s') % {
- 'product': item.product, 'old_quantity': old_quantity,
- 'new_quantity': item.quantity}
- messages.success(request, msg)
- order.create_history_entry(comment=msg, user=request.user)
- else:
- errors = quantity_form.errors.as_text()
- messages.error(request, _('Failed to change quantity: %s') % errors)
- elif action == 'move_items':
- if move_items_form.is_valid():
- old_group = item.delivery_group
- how_many = move_items_form.cleaned_data['how_many']
- with transaction.atomic():
- target_group = move_items_form.move_items()
- if not old_group.pk:
- old_group = _('removed group')
- msg = _(
- 'Moved %(how_many)s items %(item)s from %(old_group)s'
- ' to %(new_group)s') % {
- 'how_many': how_many, 'item': item, 'old_group': old_group,
- 'new_group': target_group}
- messages.success(request, msg)
- order.create_history_entry(comment=msg, user=request.user)
- else:
- errors = move_items_form.errors.as_text()
- messages.error(request, _('Failed to move items: %s') % errors)
- return redirect('dashboard:order-details', pk=order.pk)
+@staff_member_required
+def orderline_split(request, pk):
+ item = get_object_or_404(OrderedItem, pk=pk)
+ order = item.delivery_group.order
+ form = MoveItemsForm(request.POST or None, item=item)
+ if form.is_valid():
+ old_group = item.delivery_group
+ how_many = form.cleaned_data['how_many']
+ with transaction.atomic():
+ target_group = form.move_items()
+ if not old_group.pk:
+ old_group = _('removed group')
+ msg = _(
+ 'Moved %(how_many)s items %(item)s from %(old_group)s'
+ ' to %(new_group)s') % {
+ 'how_many': how_many, 'item': item, 'old_group': old_group,
+ 'new_group': target_group}
+ messages.success(request, msg)
+ order.create_history_entry(comment=msg, user=request.user)
+ return redirect('dashboard:order-details', pk=order.pk)
+ ctx = {'object': item, 'form': form}
+ ctx.update(csrf(request))
+ template = 'dashboard/order/modal_split_order_line.html'
+ return TemplateResponse(request, template, ctx)
@staff_member_required
diff --git a/saleor/dashboard/templates/dashboard/order/detail.html b/saleor/dashboard/templates/dashboard/order/detail.html
index 4e53528722..c6029162e8 100644
--- a/saleor/dashboard/templates/dashboard/order/detail.html
+++ b/saleor/dashboard/templates/dashboard/order/detail.html
@@ -101,8 +101,12 @@
<td class="right-align">{{ item.quantity }}</td>
<td class="right-align">{% gross item.get_total %}</td>
<td class="right-align">
- <a href="#base-modal" data-href="{% url 'dashboard:order-line-edit' pk=item.pk %}" class="modal-trigger-custom"
- {% if group.status != 'new' %} disabled{% endif %} title="{% trans "Edit" %}">
+ <a href="#base-modal" data-href="{% url 'dashboard:orderline-change-quantity' pk=item.pk %}" class="modal-trigger-custom"
+ {% if group.status != 'new' %} disabled{% endif %} title="{% trans "Change quantity" %}">
+ <i class="mdi-content-create"></i>
+ </a>
+ <a href="#base-modal" data-href="{% url 'dashboard:orderline-split' pk=item.pk %}" class="modal-trigger-custom"
+ {% if group.status != 'new' %} disabled{% endif %} title="{% trans "Split line" %}">
<i class="mdi-content-create"></i>
</a>
</td>
diff --git a/saleor/dashboard/templates/dashboard/order/modal_change_quantity.html b/saleor/dashboard/templates/dashboard/order/modal_change_quantity.html
new file mode 100644
index 0000000000..96596c9277
--- /dev/null
+++ b/saleor/dashboard/templates/dashboard/order/modal_change_quantity.html
@@ -0,0 +1,16 @@
+{% load i18n %}
+{% load materializecss %}
+
+<div class="modal-content">
+ <h5>{% trans "Change quantity" %}</h5>
+
+ <form class="form-inline" method="post" role="form" action="{% url 'dashboard:orderline-change-quantity' pk=object.pk %}">
+ {% csrf_token %}
+ {{ form|materializecss }}
+ <button type="submit" class="btn-flat teal-text">OK</button></span>
+ </form>
+</div>
+
+<div class="modal-footer">
+ <a href="#!" aria-hidden="true" class="modal-action modal-close btn-flat blue-text">{% trans "Cancel" %}</a>
+</div>
diff --git a/saleor/dashboard/templates/dashboard/order/modal_order_line_edit.html b/saleor/dashboard/templates/dashboard/order/modal_order_line_edit.html
deleted file mode 100644
index 549e3a6439..0000000000
--- a/saleor/dashboard/templates/dashboard/order/modal_order_line_edit.html
+++ /dev/null
@@ -1,24 +0,0 @@
-{% load i18n %}
-{% load bootstrap %}
-{% load materializecss %}
-
-<div class="modal-content">
- <h5>{% trans "Edit order line " %}{{ object.product }} ({{ object }})</h5>
-
- <form class="form-inline" method="post" role="form" action="{% url 'dashboard:order-line-edit' pk=object.pk action='change_quantity' %}">
- {% csrf_token %}
- <span>{% trans "Change quantity: " %}{% render_widget change_quantity_form.quantity class="form-control input-sm" %}
- <button type="submit" class="btn-flat teal-text">OK</button></span>
- </form>
-
-
- <form class="form-inline" method="post" role="form" action="{% url 'dashboard:order-line-edit' pk=object.pk action='move_items' %}">
- {% csrf_token %}
- <span>Move {% render_widget move_items_form.how_many class="form-control input-sm" %} items to delivery group: {% render_widget move_items_form.groups class="form-control input-sm" %}
- <button type="submit" class="btn-flat teal-text">OK</button></span>
- </form>
-</div>
-
-<div class="modal-footer">
- <a href="#!" aria-hidden="true" class="modal-action modal-close btn-flat blue-text">{% trans "Cancel" %}</a>
-</div>
diff --git a/saleor/dashboard/templates/dashboard/order/modal_split_order_line.html b/saleor/dashboard/templates/dashboard/order/modal_split_order_line.html
new file mode 100644
index 0000000000..82360582c2
--- /dev/null
+++ b/saleor/dashboard/templates/dashboard/order/modal_split_order_line.html
@@ -0,0 +1,17 @@
+{% load i18n %}
+{% load materializecss %}
+
+<div class="modal-content">
+ <h5>{% trans "Edit order line " %}</h5>
+
+ <form class="form-inline" method="post" role="form" action="{% url 'dashboard:orderline-split' pk=object.pk %}">
+ {% csrf_token %}
+{# <span>Move {% render_widget move_items_form.how_many class="form-control input-sm" %} items to delivery group: {% render_widget move_items_form.groups class="form-control input-sm" %}#}
+ {{ form|materializecss }}
+ <button type="submit" class="btn-flat teal-text">OK</button></span>
+ </form>
+</div>
+
+<div class="modal-footer">
+ <a href="#!" aria-hidden="true" class="modal-action modal-close btn-flat blue-text">{% trans "Cancel" %}</a>
+</div>