Skip to content

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

Move order recalculation to manager

Artur Smęt5752f6cff4cb
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 793178ed99..70b399b99f 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -3,8 +3,8 @@ from __future__ import unicode_literals
from django import forms
from django.conf import settings
from django.shortcuts import get_object_or_404
-from django.utils.translation import pgettext_lazy
from django.utils.translation import ugettext_lazy as _
+from django.utils.translation import pgettext_lazy
from django_prices.forms import PriceField
from payments import PaymentError
from payments.models import PAYMENT_STATUS_CHOICES
@@ -12,7 +12,7 @@ from satchless.item import InsufficientStock
from ...cart.forms import QuantityField
from ...order import Status
-from ...order.models import DeliveryGroup, OrderedItem, OrderNote
+from ...order.models import DeliveryGroup, Order, OrderedItem, OrderNote
from ...product.models import ProductVariant, Stock
@@ -154,7 +154,7 @@ class CancelItemsForm(forms.Form):
if order.groups.exclude(pk=delivery_group.pk).exists():
# we have another delivery groups as well
delivery_group.delete()
- order.recalculate()
+ Order.objects.recalculate_order(order)
class ChangeQuantityForm(forms.ModelForm):
@@ -188,7 +188,7 @@ class ChangeQuantityForm(forms.ModelForm):
delta = quantity - self.initial_quantity
Stock.objects.allocate_stock(stock, delta)
self.instance.change_quantity(quantity)
- self.instance.delivery_group.order.recalculate()
+ Order.objects.recalculate_order(self.instance.delivery_group.order)
class ShipGroupForm(forms.ModelForm):
@@ -268,7 +268,7 @@ class RemoveVoucherForm(forms.Form):
voucher.used -= 1
voucher.save(update_fields=['used'])
self.order.voucher = None
- self.order.recalculate()
+ Order.objects.recalculate_order(self.order)
ORDER_STATUS_CHOICES = [('', pgettext_lazy('Order status field value',
'All'))] + Status.CHOICES
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 3729a03780..46dd6167da 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -25,6 +25,22 @@ from ..userprofile.models import Address
from . import Status
+class OrderManager(models.Manager):
+
+ def recalculate_order(self, order):
+ prices = [group.get_total() for group in order
+ if group.status != Status.CANCELLED]
+ total_net = sum(p.net for p in prices)
+ total_gross = sum(p.gross for p in prices)
+ shipping = [group.shipping_price for group in order]
+ total_shipping = sum(shipping[1:], shipping[0])
+ total = Price(net=total_net, gross=total_gross,
+ currency=settings.DEFAULT_CURRENCY)
+ total += total_shipping
+ order.total = total
+ order.save()
+
+
@python_2_unicode_compatible
class Order(models.Model, ItemSet):
status = models.CharField(
@@ -64,6 +80,8 @@ class Order(models.Model, ItemSet):
blank=True, null=True)
discount_name = models.CharField(max_length=255, default='', blank=True)
+ objects = OrderManager()
+
class Meta:
ordering = ('-last_status_change',)
@@ -171,19 +189,6 @@ class Order(models.Model, ItemSet):
def can_cancel(self):
return self.status not in {Status.CANCELLED, Status.SHIPPED}
- def recalculate(self):
- prices = [group.get_total() for group in self
- if group.status != Status.CANCELLED]
- total_net = sum(p.net for p in prices)
- total_gross = sum(p.gross for p in prices)
- shipping = [group.shipping_price for group in self]
- total_shipping = sum(shipping[1:], shipping[0])
- total = Price(net=total_net, gross=total_gross,
- currency=settings.DEFAULT_CURRENCY)
- total += total_shipping
- self.total = total
- self.save()
-
class DeliveryGroup(models.Model, ItemSet):
status = models.CharField(