Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix duplicated customers
fowczrekb456c99df4cc
diff --git a/saleor/account/models.py b/saleor/account/models.pyindex 28f2a95afa..5e4eed3943 100644--- a/saleor/account/models.py+++ b/saleor/account/models.py@@ -12,6 +12,7 @@ from django.contrib.postgres.indexes import GinIndexfrom django.db import modelsfrom django.db.models import JSONField # type: ignorefrom django.db.models import Q, QuerySet, Value+from django.db.models.expressions import Exists, OuterReffrom django.forms.models import model_to_dictfrom django.utils import timezonefrom django.utils.crypto import get_random_string@@ -22,6 +23,7 @@ from versatileimagefield.fields import VersatileImageFieldfrom ..core.models import ModelWithMetadatafrom ..core.permissions import AccountPermissions, BasePermissionEnum, get_permissionsfrom ..core.utils.json_serializer import CustomJsonEncoder+from ..order.models import Orderfrom . import CustomerEventsfrom .validators import validate_possible_number@@ -126,8 +128,10 @@ class UserManager(BaseUserManager):)def customers(self):+ orders = Order.objects.values("user_id")return self.get_queryset().filter(- Q(is_staff=False) | (Q(is_staff=True) & Q(orders__isnull=False))+ Q(is_staff=False)+ | (Q(is_staff=True) & (Exists(orders.filter(user_id=OuterRef("pk"))))))def staff(self):diff --git a/saleor/account/tests/test_account.py b/saleor/account/tests/test_account.pyindex 390b9943b2..a523708166 100644--- a/saleor/account/tests/test_account.py+++ b/saleor/account/tests/test_account.py@@ -1,3 +1,4 @@+import uuidfrom urllib.parse import urlencodeimport i18naddress@@ -7,6 +8,7 @@ from django.http import QueryDictfrom django.template import Context, Templatefrom django_countries.fields import Country+from ...order.models import Orderfrom .. import forms, i18nfrom ..models import Userfrom ..templatetags.i18n_address_tags import format_address@@ -334,3 +336,31 @@ def test_requestor_is_staff_member_or_app_superuser(superuser):def test_requestor_is_staff_member_or_app_customer_user(customer_user):assert requestor_is_staff_member_or_app(customer_user) is False+++def test_customers_doesnt_return_duplicates(customer_user, channel_USD):+ Order.objects.bulk_create(+ [+ Order(+ user=customer_user,+ channel=channel_USD,+ token=str(uuid.uuid4()),+ ),+ Order(+ user=customer_user,+ channel=channel_USD,+ token=str(uuid.uuid4()),+ ),+ ]+ )+ assert User.objects.customers().count() == 1+++def test_customers_show_staff_with_order(admin_user, channel_USD):+ assert User.objects.customers().count() == 0+ Order.objects.create(+ user=admin_user,+ channel=channel_USD,+ token=str(uuid.uuid4()),+ )+ assert User.objects.customers().count() == 1diff --git a/saleor/checkout/models.py b/saleor/checkout/models.pyindex 9d6adfe765..18d89a9a70 100644--- a/saleor/checkout/models.py+++ b/saleor/checkout/models.py@@ -11,7 +11,6 @@ from django_countries.fields import Country, CountryFieldfrom django_prices.models import MoneyFieldfrom prices import Money-from ..account.models import Addressfrom ..channel.models import Channelfrom ..core.models import ModelWithMetadatafrom ..core.permissions import CheckoutPermissions@@ -53,10 +52,18 @@ class Checkout(ModelWithMetadata):on_delete=models.PROTECT,)billing_address = models.ForeignKey(- Address, related_name="+", editable=False, null=True, on_delete=models.SET_NULL+ "account.Address",+ related_name="+",+ editable=False,+ null=True,+ on_delete=models.SET_NULL,)shipping_address = models.ForeignKey(- Address, related_name="+", editable=False, null=True, on_delete=models.SET_NULL+ "account.Address",+ related_name="+",+ editable=False,+ null=True,+ on_delete=models.SET_NULL,)shipping_method = models.ForeignKey(ShippingMethod,diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex 92fbac1e8b..c60933da05 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -56,6 +56,7 @@ ERROR_DOES_NOT_SHIP = "This checkout doesn't need shipping"if TYPE_CHECKING:+ from ...account.models import Addressfrom ...checkout.fetch import CheckoutInfo@@ -274,7 +275,7 @@ class CheckoutCreate(ModelMutation, I18nMixin):return variants, quantities@classmethod- def retrieve_shipping_address(cls, user, data: dict) -> Optional[models.Address]:+ def retrieve_shipping_address(cls, user, data: dict) -> Optional["Address"]:if data.get("shipping_address") is not None:return cls.validate_address(data["shipping_address"], address_type=AddressType.SHIPPING@@ -284,7 +285,7 @@ class CheckoutCreate(ModelMutation, I18nMixin):return None@classmethod- def retrieve_billing_address(cls, user, data: dict) -> Optional[models.Address]:+ def retrieve_billing_address(cls, user, data: dict) -> Optional["Address"]:if data.get("billing_address") is not None:return cls.validate_address(data["billing_address"], address_type=AddressType.BILLINGdiff --git a/saleor/order/models.py b/saleor/order/models.pyindex 493e1c225a..9febdbd58e 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -16,7 +16,6 @@ from django_measurement.models import MeasurementFieldfrom django_prices.models import MoneyField, TaxedMoneyFieldfrom measurement.measures import Weight-from ..account.models import Addressfrom ..channel.models import Channelfrom ..core.models import ModelWithMetadatafrom ..core.permissions import OrderPermissions@@ -100,10 +99,18 @@ class Order(ModelWithMetadata):)tracking_client_id = models.CharField(max_length=36, blank=True, editable=False)billing_address = models.ForeignKey(- Address, related_name="+", editable=False, null=True, on_delete=models.SET_NULL+ "account.Address",+ related_name="+",+ editable=False,+ null=True,+ on_delete=models.SET_NULL,)shipping_address = models.ForeignKey(- Address, related_name="+", editable=False, null=True, on_delete=models.SET_NULL+ "account.Address",+ related_name="+",+ editable=False,+ null=True,+ on_delete=models.SET_NULL,)user_email = models.EmailField(blank=True, default="")original = models.ForeignKey(