Skip to content

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

Move adding history entries to view

Marcin Gębala71ed729a609b
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index f817b85902..2846ddcbc6 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -28,12 +28,13 @@ class ManagePaymentForm(forms.Form):
super(ManagePaymentForm, self).__init__(*args, **kwargs)
def handle_action(self, action, user):
+ amount = self.cleaned_data['amount']
if action == 'capture' and self.payment.status == 'preauth':
- self.payment.capture(amount=self.cleaned_data['amount'], user=user)
+ self.payment.capture(amount)
elif action == 'refund' and self.payment.status == 'confirmed':
- self.payment.refund(amount=self.cleaned_data['amount'], user=user)
+ self.payment.refund(amount)
elif action == 'release' and self.payment.status == 'preauth':
- self.payment.release(user=user)
+ self.payment.release()
class MoveItemsForm(forms.ModelForm):
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index 589fe6dc5a..0c88509c6d 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -77,12 +77,20 @@ def manage_payment(request, pk, action):
if form.is_valid():
try:
form.handle_action(action, request.user)
- except PaymentError as e:
- messages.error(request, _('Payment gateway error: ') + e.message)
- except ValueError as e:
+ except (PaymentError, ValueError) as e:
messages.error(request, _('Payment gateway error: ') + e.message)
else:
- messages.success(request, _('Success'))
+ amount = form.cleaned_data['amount']
+ currency = payment.currency
+ if action in ['capture', 'refund']:
+ comment = _('%(action)sed %(amount)s %(currency)s' % {
+ 'action': action.capitalize(), 'amount': amount,
+ 'currency': currency})
+ elif action == 'release':
+ comment = _('Released payment')
+ payment.order.create_history_entry(comment=comment,
+ user=request.user)
+ messages.success(request, comment)
return redirect('dashboard:order-details', pk=payment.order.pk)
if request.is_ajax():
amount = 0
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 64fc96350a..81afc01e25 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -320,41 +320,6 @@ class Payment(BasePayment):
for item in self.order.get_items()]
return items
- def capture(self, user=None, **kwargs):
- result = super(Payment, self).capture(**kwargs)
- amount = kwargs.get('amount', self.total)
- comment = pgettext_lazy(
- 'Order history',
- 'Captured %(amount)s %(currency)s'
- % {'amount': amount, 'currency': self.currency})
- self.order.history.create(
- status=self.order.status,
- comment=comment,
- user=user)
- return result
-
- def release(self, user=None, **kwargs):
- result = super(Payment, self).release(**kwargs)
- comment = pgettext_lazy('Order history', 'Released funds')
- self.order.history.create(
- status=self.order.status,
- comment=comment,
- user=user)
- return result
-
- def refund(self, user=None, **kwargs):
- result = super(Payment, self).refund(**kwargs)
- amount = kwargs.get('amount', self.total)
- comment = pgettext_lazy(
- 'Order history',
- 'Refunded %(amount)s %(currency)s'
- % {'amount': amount, 'currency': self.currency})
- self.order.history.create(
- status=self.order.status,
- comment=comment,
- user=user)
- return result
-
@python_2_unicode_compatible
class OrderHistoryEntry(models.Model):