Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Order subtotal performance (#7174)
Tomasz Szymański18e25b92e0bd
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f5f9423ce4..d65b344024 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -59,6 +59,7 @@ All notable, unreleased changes to this project will be documented in this file.- Fix incorrect payment data for klarna - #7150 by @IKarbowiak- Drop deleted images from storage - #7129 by @IKarbowiak- Fix core sorting on related fields - #7195 by @tomaszszymanski129+- Performance upgrade on orders query with `subtotal` field - #7174 by @tomaszszymanski129- Performance upgrade on orders query with `actions` field - #7175 by @tomaszszymanski129- Performance upgrade on orders query with `totalAuthorized` field - #7170 by @tomaszszymanski129- Fix export with empty assignment values - #7207 by @IKarbowiakdiff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex a06241ecb8..f0d052933a 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -25,6 +25,7 @@ from ...payment import ChargeStatusfrom ...payment.dataloaders import PaymentsByOrderIdLoaderfrom ...payment.model_helpers import (get_last_payment,+ get_subtotal,get_total_authorized,get_total_captured,)@@ -799,8 +800,15 @@ class Order(CountableDjangoObjectType):@staticmethod@traced_resolver- def resolve_subtotal(root: models.Order, _info):- return root.get_subtotal()+ def resolve_subtotal(root: models.Order, info):+ def _resolve_subtotal(order_lines):+ return get_subtotal(order_lines, root.currency)++ return (+ OrderLinesByOrderIdLoader(info.context)+ .load(root.id)+ .then(_resolve_subtotal)+ )@staticmethoddef resolve_total(root: models.Order, _info):diff --git a/saleor/order/models.py b/saleor/order/models.pyindex 330a9d2b62..8df6d78cb4 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -26,7 +26,11 @@ from ..discount import DiscountValueTypefrom ..discount.models import Voucherfrom ..giftcard.models import GiftCardfrom ..payment import ChargeStatus, TransactionKind-from ..payment.model_helpers import get_total_authorized, get_total_captured+from ..payment.model_helpers import (+ get_subtotal,+ get_total_authorized,+ get_total_captured,+)from ..shipping.models import ShippingMethodfrom . import FulfillmentStatus, OrderEvents, OrderStatus@@ -286,8 +290,7 @@ class Order(ModelWithMetadata):return any(line.is_shipping_required for line in self.lines.all())def get_subtotal(self):- subtotal_iterator = (line.total_price for line in self.lines.all())- return sum(subtotal_iterator, zero_taxed_money(currency=self.currency))+ return get_subtotal(self.lines.all(), self.currency)def get_total_quantity(self):return sum([line.quantity for line in self.lines.all()])diff --git a/saleor/payment/model_helpers.py b/saleor/payment/model_helpers.pyindex 92aabf1e5a..d69e268be3 100644--- a/saleor/payment/model_helpers.py+++ b/saleor/payment/model_helpers.py@@ -1,12 +1,15 @@from operator import attrgetter-from typing import List+from typing import TYPE_CHECKING, Listfrom prices import Money-from ..core.taxes import zero_money+from ..core.taxes import zero_money, zero_taxed_moneyfrom . import ChargeStatusfrom .models import Payment+if TYPE_CHECKING:+ from ..order.models import OrderLine+def get_last_payment(payments: List[Payment]):return max(payments, default=None, key=attrgetter("pk"))@@ -34,3 +37,8 @@ def get_total_authorized(payments: List[Payment], fallback_currency: str):if last_payment.is_active:return last_payment.get_authorized_amount()return zero_money(fallback_currency)+++def get_subtotal(order_lines: List["OrderLine"], fallback_currency: str):+ subtotal_iterator = (line.total_price for line in order_lines)+ return sum(subtotal_iterator, zero_taxed_money(currency=fallback_currency))