Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move recalculate order to utils
Anna Siwikb3c938d0b15b
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 8348ee7e74..ddee1c5f72 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -16,7 +16,7 @@ from ...order import OrderStatusfrom ...order.models import DeliveryGroup, Order, OrderLine, OrderNotefrom ...order.utils import (add_variant_to_delivery_group, cancel_order, cancel_delivery_group,- change_order_line_quantity, merge_duplicated_lines)+ change_order_line_quantity, merge_duplicated_lines, order_recalculate)from ...product.models import Product, ProductVariant, Stock@@ -141,7 +141,7 @@ class CancelLinesForm(forms.Form):order = self.line.delivery_group.orderself.line.quantity = 0OrderLine.objects.remove_empty_groups(self.line)- Order.objects.recalculate_order(order)+ order_recalculate(order)class ChangeQuantityForm(forms.ModelForm):@@ -180,7 +180,7 @@ class ChangeQuantityForm(forms.ModelForm):delta = quantity - self.initial_quantityStock.objects.allocate_stock(stock, delta)change_order_line_quantity(self.instance, quantity)- Order.objects.recalculate_order(self.instance.delivery_group.order)+ order_recalculate(self.instance.delivery_group.order)return self.instance@@ -268,7 +268,7 @@ class RemoveVoucherForm(forms.Form):voucher = self.order.voucherVoucher.objects.decrease_usage(voucher)self.order.voucher = None- Order.objects.recalculate_order(self.order)+ order_recalculate(self.order)ORDER_STATUS_CHOICES = [@@ -365,4 +365,4 @@ class AddVariantToDeliveryGroupForm(forms.Form):variant = self.cleaned_data.get('variant')quantity = self.cleaned_data.get('quantity')add_variant_to_delivery_group(self.group, variant, quantity)- Order.objects.recalculate_order(self.group.order)+ order_recalculate(self.group.order)diff --git a/saleor/order/models.py b/saleor/order/models.pyindex d560fba9ce..00a7bfcb19 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -24,24 +24,6 @@ from ..product.models import Productfrom ..userprofile.models import Address-class OrderManager(models.Manager):- def recalculate_order(self, order):- prices = [group.get_total() for group in order- if group.status != OrderStatus.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]- if shipping:- total_shipping = sum(shipping[1:], shipping[0])- else:- total_shipping = Price(0, currency=settings.DEFAULT_CURRENCY)- total = Price(net=total_net, gross=total_gross,- currency=settings.DEFAULT_CURRENCY)- total += total_shipping- order.total = total- order.save()--@python_2_unicode_compatibleclass Order(models.Model, ItemSet):status = models.CharField(@@ -94,8 +76,6 @@ class Order(models.Model, ItemSet):verbose_name=pgettext_lazy('Order field', 'discount name'),max_length=255, default='', blank=True)- objects = OrderManager()-class Meta:ordering = ('-last_status_change',)permissions = (diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 4e8e9942bf..80c6b647cd 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -3,11 +3,13 @@ from __future__ import unicode_literalsimport loggingfrom functools import wraps+from django.conf import settingsfrom django.db.models import Ffrom django.dispatch import receiverfrom django.shortcuts import get_object_or_404, redirectfrom django.utils.translation import pgettext_lazyfrom payments.signals import status_changed+from prices import Pricefrom satchless.item import InsufficientStockfrom ..core import analytics@@ -49,6 +51,24 @@ def order_status_change(sender, instance, **kwargs):logger.exception('Recording order in analytics failed')+def order_recalculate(order):+ prices = [+ group.get_total() for group in order+ if group.status != OrderStatus.CANCELLED]+ total_net = sum(p.net for p in prices)+ total_gross = sum(p.gross for p in prices)+ total = Price(+ net=total_net, gross=total_gross,+ currency=settings.DEFAULT_CURRENCY)+ shipping = [group.shipping_price for group in order]+ total_shipping = (+ sum(shipping[1:], shipping[0]) if shipping+ else Price(0, currency=settings.DEFAULT_CURRENCY))+ total += total_shipping+ order.total = total+ order.save()++def attach_order_to_user(order, user):order.user = userstore_user_address(user, order.billing_address, billing=True)diff --git a/tests/conftest.py b/tests/conftest.pyindex e8b1705022..af15b85e0d 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -20,10 +20,10 @@ from saleor.cart.models import Cartfrom saleor.checkout.core import Checkoutfrom saleor.discount.models import Voucher, Salefrom saleor.order.models import Order, OrderLine, DeliveryGroup-from saleor.product.models import (AttributeChoiceValue, Category, Product,- ProductAttribute, ProductClass,- ProductVariant, ProductImage, Stock,- StockLocation)+from saleor.order.utils import order_recalculate+from saleor.product.models import (+ AttributeChoiceValue, Category, Product, ProductAttribute, ProductClass,+ ProductVariant, ProductImage, Stock, StockLocation)from saleor.shipping.models import ShippingMethodfrom saleor.site.models import SiteSettings, AuthorizationKeyfrom saleor.userprofile.models import Address, User@@ -441,7 +441,7 @@ def order_with_lines_and_stock(order, product_class):stock=stock,stock_location=stock.location.name)- Order.objects.recalculate_order(order)+ order_recalculate(order)order.refresh_from_db()return order