Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Optimize order filtering by ready to fulfill status (#9113)
Tomasz Szymański34bf03ce99d4
diff --git a/saleor/graphql/order/filters.py b/saleor/graphql/order/filters.pyindex 33f4216487..ae8c836ed9 100644--- a/saleor/graphql/order/filters.py+++ b/saleor/graphql/order/filters.py@@ -1,5 +1,5 @@import django_filters-from django.db.models import Exists, IntegerField, OuterRef, Q, Sum+from django.db.models import Exists, IntegerField, OuterRef, Qfrom django.db.models.functions import Castfrom django.utils import timezonefrom graphene_django.filter import GlobalIDMultipleChoiceFilter@@ -44,9 +44,6 @@ def filter_status(qs, _, value):query_objects |= qs.filter(status__in=value)if OrderStatusFilter.READY_TO_FULFILL in value:- # to use & between queries both of them need to have applied the same- # annotate- qs = qs.annotate(amount_paid=Sum("payments__captured_amount"))query_objects |= qs.ready_to_fulfill()if OrderStatusFilter.READY_TO_CAPTURE in value:diff --git a/saleor/order/models.py b/saleor/order/models.pyindex 78c212b7cc..7e779bdee7 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -9,7 +9,7 @@ from django.contrib.postgres.indexes import GinIndexfrom django.core.validators import MinValueValidatorfrom django.db import modelsfrom django.db.models import JSONField # type: ignore-from django.db.models import F, Max, Sum+from django.db.models import F, Maxfrom django.db.models.expressions import Exists, OuterReffrom django.utils.timezone import nowfrom django_measurement.models import MeasurementField@@ -58,11 +58,10 @@ class OrderQueryset(models.QuerySet):"""statuses = {OrderStatus.UNFULFILLED, OrderStatus.PARTIALLY_FULFILLED}payments = Payment.objects.filter(is_active=True).values("id")- qs = self.annotate(amount_paid=Sum("payments__captured_amount"))- return qs.filter(+ return self.filter(Exists(payments.filter(order_id=OuterRef("id"))),status__in=statuses,- total_gross_amount__lte=F("amount_paid"),+ total_gross_amount__lte=F("total_paid_amount"),)def ready_to_capture(self):diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.pyindex 2018916894..85b120017c 100644--- a/saleor/order/tests/test_order.py+++ b/saleor/order/tests/test_order.py@@ -482,10 +482,16 @@ def test_order_queryset_to_ship(settings, channel_USD):total = TaxedMoney(net=Money(10, "USD"), gross=Money(15, "USD"))orders_to_ship = [Order.objects.create(- status=OrderStatus.UNFULFILLED, total=total, channel=channel_USD+ status=OrderStatus.UNFULFILLED,+ total=total,+ total_paid_amount=total.gross.amount,+ channel=channel_USD,),Order.objects.create(- status=OrderStatus.PARTIALLY_FULFILLED, total=total, channel=channel_USD+ status=OrderStatus.PARTIALLY_FULFILLED,+ total=total,+ total_paid_amount=total.gross.amount,+ channel=channel_USD,),]for order in orders_to_ship: