Skip to content

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

Fix modal forms in order details

Paweł Kowalewski53d2102bc32e
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index a2ad41e147..fb682e53b5 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -1,12 +1,13 @@
from __future__ import unicode_literals
from django import forms
+from django.conf import settings
from django.utils.translation import ugettext_lazy as _, pgettext_lazy
+from django_prices.forms import PriceField
from satchless.item import InsufficientStock
from ...cart.forms import QuantityField
from ...order.models import DeliveryGroup, OrderedItem, OrderNote, Order
-from ...product.models import Product
class OrderNoteForm(forms.ModelForm):
@@ -21,7 +22,8 @@ class OrderNoteForm(forms.ModelForm):
class ManagePaymentForm(forms.Form):
- amount = forms.DecimalField(min_value=0, decimal_places=2, required=False)
+ amount = PriceField(max_digits=12, decimal_places=2,
+ currency=settings.DEFAULT_CURRENCY)
def __init__(self, *args, **kwargs):
self.payment = kwargs.pop('payment')
@@ -30,9 +32,9 @@ class ManagePaymentForm(forms.Form):
def handle_action(self, action, user):
amount = self.cleaned_data['amount']
if action == 'capture' and self.payment.status == 'preauth':
- self.payment.capture(amount)
+ self.payment.capture(amount.gross)
elif action == 'refund' and self.payment.status == 'confirmed':
- self.payment.refund(amount)
+ self.payment.refund(amount.gross)
elif action == 'release' and self.payment.status == 'preauth':
self.payment.release()
else:
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index 7d9a1984d5..4f74b1cebd 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -3,10 +3,12 @@ from __future__ import unicode_literals
from django.contrib import messages
from django.core.context_processors import csrf
from django.db import transaction
+from django.http import Http404
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.generic import ListView
+from django_prices.templatetags.prices_i18n import gross
from payments import PaymentError
from prices import Price
@@ -78,7 +80,20 @@ def order_add_note(request, order_pk):
def manage_payment(request, order_pk, payment_pk, action):
order = get_object_or_404(Order, pk=order_pk)
payment = get_object_or_404(order.payments.all(), pk=payment_pk)
- form = ManagePaymentForm(request.POST or None, payment=payment)
+ if action == 'release':
+ amount = 0
+ template = 'dashboard/order/modal_release.html'
+ elif action == 'refund':
+ amount = payment.captured_amount
+ template = 'dashboard/order/modal_capture_refund.html'
+ elif action == 'capture':
+ amount = payment.order.get_total().quantize('0.01').gross
+ template = 'dashboard/order/modal_capture_refund.html'
+ else:
+ raise Http404()
+ initial = {'amount': amount, 'action': action}
+ form = ManagePaymentForm(request.POST or None, payment=payment,
+ initial=initial)
status = 200
if form.is_valid():
try:
@@ -87,36 +102,28 @@ def manage_payment(request, order_pk, payment_pk, action):
messages.error(request, _('Payment gateway error: %s') % e.message)
else:
amount = form.cleaned_data['amount']
+ comment = None
currency = payment.currency
if action == 'capture':
- comment = _('Captured %(amount)s %(currency)s') % {
- 'amount': amount, 'currency': currency}
+ comment = _('Captured %(amount)s') % {
+ 'amount': gross(amount)}
payment.order.create_history_entry(comment=comment,
user=request.user)
elif action == 'refund':
- comment = _('Refunded %(amount)s %(currency)s') % {
- 'amount': amount, 'currency': currency}
+ comment = _('Refunded %(amount)s') % {
+ 'amount': gross(amount)}
payment.order.create_history_entry(comment=comment,
user=request.user)
elif action == 'release':
comment = _('Released payment')
payment.order.create_history_entry(comment=comment,
user=request.user)
+ messages.success(request, comment)
elif form.errors:
status = 400
- amount = 0
- if action == 'release':
- template = 'dashboard/order/modal_release.html'
- else:
- if action == 'refund':
- amount = payment.captured_amount
- elif action == 'capture':
- amount = payment.order.get_total().gross
- template = 'dashboard/order/modal_capture_refund.html'
- initial = {'amount': amount, 'action': action}
- form = ManagePaymentForm(payment=payment, initial=initial)
ctx = {'form': form, 'action': action, 'currency': payment.currency,
- 'captured': payment.captured_amount, 'payment': payment}
+ 'captured': payment.captured_amount, 'order': order,
+ 'payment': payment}
return TemplateResponse(request, template, ctx, status=status)
@@ -151,6 +158,9 @@ def orderline_split(request, order_pk, line_pk):
item = get_object_or_404(OrderedItem.objects.filter(
delivery_group__order=order), pk=line_pk)
form = MoveItemsForm(request.POST or None, item=item)
+ line_pk = None
+ if item:
+ line_pk = item.pk
status = 200
if form.is_valid():
old_group = item.delivery_group
@@ -168,7 +178,7 @@ def orderline_split(request, order_pk, line_pk):
messages.success(request, msg)
elif form.errors:
status = 400
- ctx = {'order': order, 'object': item, 'form': form}
+ ctx = {'order': order, 'object': item, 'form': form, 'line_pk': line_pk}
template = 'dashboard/order/modal_split_order_line.html'
return TemplateResponse(request, template, ctx, status=status)
diff --git a/saleor/dashboard/templates/dashboard/order/detail.html b/saleor/dashboard/templates/dashboard/order/detail.html
index b95cf18777..8d0fab083a 100644
--- a/saleor/dashboard/templates/dashboard/order/detail.html
+++ b/saleor/dashboard/templates/dashboard/order/detail.html
@@ -217,14 +217,14 @@
{% if can_capture or can_refund or can_release %}
<div class="card-action">
{% if can_capture %}
- <a href="#base-modal" data-href="{% url 'dashboard:manage-payment' pk=payment.pk action='capture' %}"
+ <a href="#base-modal" data-href="{% url 'dashboard:manage-payment' order_pk=order.pk payment_pk=payment.pk action='capture' %}"
class="modal-trigger-custom">{% trans "Capture" %}</a>
{% elif can_refund %}
- <a href="#base-modal" data-href="{% url 'dashboard:manage-payment' pk=payment.pk action='refund' %}"
+ <a href="#base-modal" data-href="{% url 'dashboard:manage-payment' order_pk=order.pk payment_pk=payment.pk action='refund' %}"
class="modal-trigger-custom">{% trans "Refund" %}</a>
{% endif %}
{% if can_release %}
- <a href="#base-modal" data-href="{% url 'dashboard:manage-payment' pk=payment.pk action='release' %}"
+ <a href="#base-modal" data-href="{% url 'dashboard:manage-payment' order_pk=order.pk payment_pk=payment.pk action='release' %}"
class="modal-trigger-custom">{% trans "Release" %}</a>
{% endif %}
</div>
diff --git a/saleor/dashboard/templates/dashboard/order/modal_capture_refund.html b/saleor/dashboard/templates/dashboard/order/modal_capture_refund.html
index 3fc2cabfb5..da7eb4c71d 100644
--- a/saleor/dashboard/templates/dashboard/order/modal_capture_refund.html
+++ b/saleor/dashboard/templates/dashboard/order/modal_capture_refund.html
@@ -1,22 +1,24 @@
{% load i18n %}
{% load babel %}
{% load bootstrap %}
+{% load materializecss %}
{% load prices_i18n %}
-<form role="form" method="post" action="{% url 'dashboard:manage-payment' pk=payment.pk action=action %}">
+<form role="form" method="post" action="{% url 'dashboard:manage-payment' order_pk=order.pk payment_pk=payment.pk action=action %}" class="form-async">
{% csrf_token %}
<div class="modal-content">
<h5>{{ action|capfirst }} {% trans "payment" %}</h5>
- <label class="control-label" for="amount">{% blocktrans %}Amount to {{ action }}:{% endblocktrans %}</label>
- {% render_widget form.amount class="form-control" %} {{ currency }}
+ <div class="row">
+ {{ form.amount|materializecss }}
+ </div>
- <p>{% trans "Before this action" %}</p>
+ <h5>{% trans "Before this action" %}</h5>
- <ul>
- <li>{% trans "Order total: " %}{% gross payment.order.get_total %}</li>
- <li>{% trans "Captured amount: " %}{{ captured|currencyfmt:currency }} </li>
+ <ul class="list">
+ <li class="list-item">{% trans "Order total: " %}{% gross payment.order.get_total %}</li>
+ <li class="list-item">{% trans "Captured amount: " %}{{ captured|currencyfmt:currency }} </li>
</ul>
</div>
diff --git a/saleor/dashboard/templates/dashboard/order/modal_release.html b/saleor/dashboard/templates/dashboard/order/modal_release.html
index 2f3c3992b1..684d27eefd 100644
--- a/saleor/dashboard/templates/dashboard/order/modal_release.html
+++ b/saleor/dashboard/templates/dashboard/order/modal_release.html
@@ -1,5 +1,5 @@
{% load i18n %}
-{% load bootstrap %}
+{% load materializecss %}
<div class="modal-content">
<h5>{% trans "Release payment" %}</h5>
@@ -8,8 +8,10 @@
</div>
<div class="modal-footer">
- <form id="release-form" method="post" role="form" action="{% url 'dashboard:manage-payment' pk=payment.pk action=action %}">
+ <form id="release-form" method="post" role="form" class="form-async"
+ action="{% url 'dashboard:manage-payment' order_pk=order.pk payment_pk=payment.pk action=action %}">
{% csrf_token %}
+ <input type="hidden" name="{{ form.amount.name }}" value="0">
<button type="submit" class="modal-action btn-flat red-text">{% trans "Release" %}</button>
<a href="#!" aria-hidden="true" class="modal-action modal-close btn-flat blue-text">{% trans "Cancel" %}</a>
diff --git a/saleor/dashboard/templates/dashboard/order/modal_split_order_line.html b/saleor/dashboard/templates/dashboard/order/modal_split_order_line.html
index 7d65deb3b2..41f6c70dd0 100644
--- a/saleor/dashboard/templates/dashboard/order/modal_split_order_line.html
+++ b/saleor/dashboard/templates/dashboard/order/modal_split_order_line.html
@@ -1,13 +1,13 @@
{% load i18n %}
{% load materializecss %}
-<form method="post" role="form" action="{% url 'dashboard:orderline-split' order_pk=order.pk line_pk=object.pk %}" class="form-async">
+<form method="post" role="form" class="form-async"
+ action="{% url 'dashboard:orderline-split' order_pk=order.pk line_pk=line_pk %}">
{% csrf_token %}
<div class="modal-content">
<h5>{% trans "Split order line " %}</h5>
<div class="row">
-{# <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:"s12 m6" }}
</div>
</div>