Skip to content

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

Fix duplicated customers

fowczrekb456c99df4cc
diff --git a/saleor/account/models.py b/saleor/account/models.py
index 28f2a95afa..5e4eed3943 100644
--- a/saleor/account/models.py
+++ b/saleor/account/models.py
@@ -12,6 +12,7 @@ from django.contrib.postgres.indexes import GinIndex
from django.db import models
from django.db.models import JSONField # type: ignore
from django.db.models import Q, QuerySet, Value
+from django.db.models.expressions import Exists, OuterRef
from django.forms.models import model_to_dict
from django.utils import timezone
from django.utils.crypto import get_random_string
@@ -22,6 +23,7 @@ from versatileimagefield.fields import VersatileImageField
from ..core.models import ModelWithMetadata
from ..core.permissions import AccountPermissions, BasePermissionEnum, get_permissions
from ..core.utils.json_serializer import CustomJsonEncoder
+from ..order.models import Order
from . import CustomerEvents
from .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.py
index 390b9943b2..a523708166 100644
--- a/saleor/account/tests/test_account.py
+++ b/saleor/account/tests/test_account.py
@@ -1,3 +1,4 @@
+import uuid
from urllib.parse import urlencode
import i18naddress
@@ -7,6 +8,7 @@ from django.http import QueryDict
from django.template import Context, Template
from django_countries.fields import Country
+from ...order.models import Order
from .. import forms, i18n
from ..models import User
from ..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() == 1
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.py
index 9d6adfe765..18d89a9a70 100644
--- a/saleor/checkout/models.py
+++ b/saleor/checkout/models.py
@@ -11,7 +11,6 @@ from django_countries.fields import Country, CountryField
from django_prices.models import MoneyField
from prices import Money
-from ..account.models import Address
from ..channel.models import Channel
from ..core.models import ModelWithMetadata
from ..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.py
index 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 Address
from ...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.BILLING
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 493e1c225a..9febdbd58e 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -16,7 +16,6 @@ from django_measurement.models import MeasurementField
from django_prices.models import MoneyField, TaxedMoneyField
from measurement.measures import Weight
-from ..account.models import Address
from ..channel.models import Channel
from ..core.models import ModelWithMetadata
from ..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(