Skip to content

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

add BaseCustomerCreate

fowczarek7b82018ec1be
diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.py
index 7394dfcc9a..cdd4e8d028 100644
--- a/saleor/graphql/account/mutations/account.py
+++ b/saleor/graphql/account/mutations/account.py
@@ -6,8 +6,12 @@ from ....checkout import AddressType
from ...account.enums import AddressTypeEnum
from ...account.types import Address, AddressInput, User
from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
-from .base import INVALID_TOKEN, BaseAddressDelete, BaseAddressUpdate
-from .staff import CustomerCreate
+from .base import (
+ INVALID_TOKEN,
+ BaseAddressDelete,
+ BaseAddressUpdate,
+ BaseCustomerCreate,
+)
class AccountRegisterInput(graphene.InputObjectType):
@@ -47,7 +51,7 @@ class AccountInput(graphene.InputObjectType):
)
-class AccountUpdate(CustomerCreate):
+class AccountUpdate(BaseCustomerCreate):
class Arguments:
input = AccountInput(
description="Fields required to update the account of the logged-in user.",
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index 73a1ec85c3..22af70ab7f 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -7,6 +7,8 @@ from django.utils.http import urlsafe_base64_encode
from graphql_jwt.exceptions import PermissionDenied
from ....account import emails, events as account_events, models
+from ....dashboard.emails import send_set_password_customer_email
+from ...account.i18n import I18nMixin
from ...account.types import Address, AddressInput, User
from ...core.mutations import (
BaseMutation,
@@ -16,6 +18,8 @@ from ...core.mutations import (
UpdateMetaBaseMutation,
)
+BILLING_ADDRESS_FIELD = "default_billing_address"
+SHIPPING_ADDRESS_FIELD = "default_shipping_address"
INVALID_TOKEN = "Invalid or expired token."
@@ -188,6 +192,87 @@ class BaseAddressDelete(ModelDeleteMutation):
return response
+class UserInput(graphene.InputObjectType):
+ first_name = graphene.String(description="Given name.")
+ last_name = graphene.String(description="Family name.")
+ email = graphene.String(description="The unique email address of the user.")
+ is_active = graphene.Boolean(required=False, description="User account is active.")
+ note = graphene.String(description="A note about the user.")
+
+
+class UserAddressInput(graphene.InputObjectType):
+ default_billing_address = AddressInput(
+ description="Billing address of the customer."
+ )
+ default_shipping_address = AddressInput(
+ description="Shipping address of the customer."
+ )
+
+
+class CustomerInput(UserInput, UserAddressInput):
+ pass
+
+
+class UserCreateInput(CustomerInput):
+ send_password_email = graphene.Boolean(
+ description="Send an email with a link to set a password"
+ )
+
+
+class BaseCustomerCreate(ModelMutation, I18nMixin):
+ """Base mutation for customer create used by staff and account."""
+
+ class Arguments:
+ input = UserCreateInput(
+ description="Fields required to create a customer.", required=True
+ )
+
+ class Meta:
+ abstract = True
+
+ @classmethod
+ def clean_input(cls, info, instance, data):
+ shipping_address_data = data.pop(SHIPPING_ADDRESS_FIELD, None)
+ billing_address_data = data.pop(BILLING_ADDRESS_FIELD, None)
+ cleaned_input = super().clean_input(info, instance, data)
+
+ if shipping_address_data:
+ shipping_address = cls.validate_address(
+ shipping_address_data,
+ instance=getattr(instance, SHIPPING_ADDRESS_FIELD),
+ )
+ cleaned_input[SHIPPING_ADDRESS_FIELD] = shipping_address
+
+ if billing_address_data:
+ billing_address = cls.validate_address(
+ billing_address_data, instance=getattr(instance, BILLING_ADDRESS_FIELD)
+ )
+ cleaned_input[BILLING_ADDRESS_FIELD] = billing_address
+ return cleaned_input
+
+ @classmethod
+ def save(cls, info, instance, cleaned_input):
+ # FIXME: save address in user.addresses as well
+ default_shipping_address = cleaned_input.get(SHIPPING_ADDRESS_FIELD)
+ if default_shipping_address:
+ default_shipping_address.save()
+ instance.default_shipping_address = default_shipping_address
+ default_billing_address = cleaned_input.get(BILLING_ADDRESS_FIELD)
+ if default_billing_address:
+ default_billing_address.save()
+ instance.default_billing_address = default_billing_address
+
+ is_creation = instance.pk is None
+ super().save(info, instance, cleaned_input)
+
+ # The instance is a new object in db, create an event
+ if is_creation:
+ account_events.customer_account_created_event(user=instance)
+
+ if cleaned_input.get("send_password_email"):
+ send_set_password_customer_email.delay(instance.pk)
+
+
class UserUpdateMeta(UpdateMetaBaseMutation):
class Meta:
description = "Updates metadata for user."
diff --git a/saleor/graphql/account/mutations/deprecated_account.py b/saleor/graphql/account/mutations/deprecated_account.py
index b13d0f5dee..d531a05961 100644
--- a/saleor/graphql/account/mutations/deprecated_account.py
+++ b/saleor/graphql/account/mutations/deprecated_account.py
@@ -6,8 +6,7 @@ from ....checkout import AddressType
from ...account.enums import AddressTypeEnum
from ...account.types import Address, AddressInput, User
from ...core.mutations import BaseMutation, ModelMutation
-from .base import send_user_password_reset_email
-from .staff import CustomerCreate, UserAddressInput
+from .base import BaseCustomerCreate, UserAddressInput, send_user_password_reset_email
class CustomerRegisterInput(graphene.InputObjectType):
@@ -36,7 +35,7 @@ class CustomerRegister(ModelMutation):
account_events.customer_account_created_event(user=user)
-class LoggedUserUpdate(CustomerCreate):
+class LoggedUserUpdate(BaseCustomerCreate):
class Arguments:
input = UserAddressInput(
description="Fields required to update logged in user.", required=True
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index 37bd01ff15..1ba9ef51ef 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -10,13 +10,9 @@ from ....account.thumbnails import create_user_avatar_thumbnails
from ....account.utils import get_random_avatar
from ....checkout import AddressType
from ....core.permissions import get_permissions
-from ....dashboard.emails import (
- send_set_password_customer_email,
- send_set_password_staff_email,
-)
+from ....dashboard.emails import send_set_password_staff_email
from ....dashboard.staff.utils import remove_staff_member
from ...account.enums import AddressTypeEnum
-from ...account.i18n import I18nMixin
from ...account.types import Address, AddressInput, User
from ...core.enums import PermissionEnum
from ...core.mutations import (
@@ -29,37 +25,13 @@ from ...core.mutations import (
from ...core.types import Upload
from ...core.utils import validate_image_file
from ..utils import CustomerDeleteMixin, StaffDeleteMixin, UserDeleteMixin
-from .base import BaseAddressDelete, BaseAddressUpdate
-
-BILLING_ADDRESS_FIELD = "default_billing_address"
-SHIPPING_ADDRESS_FIELD = "default_shipping_address"
-
-
-class UserInput(graphene.InputObjectType):
- first_name = graphene.String(description="Given name.")
- last_name = graphene.String(description="Family name.")
- email = graphene.String(description="The unique email address of the user.")
- is_active = graphene.Boolean(required=False, description="User account is active.")
- note = graphene.String(description="A note about the user.")
-
-
-class UserAddressInput(graphene.InputObjectType):
- default_billing_address = AddressInput(
- description="Billing address of the customer."
- )
- default_shipping_address = AddressInput(
- description="Shipping address of the customer."
- )
-
-
-class CustomerInput(UserInput, UserAddressInput):
- pass
-
-
-class UserCreateInput(CustomerInput):
- send_password_email = graphene.Boolean(
- description="Send an email with a link to set a password"
- )
+from .base import (
+ BaseAddressDelete,
+ BaseAddressUpdate,
+ BaseCustomerCreate,
+ CustomerInput,
+ UserInput,
+)
class StaffInput(UserInput):
@@ -75,60 +47,13 @@ class StaffCreateInput(StaffInput):
)
-class CustomerCreate(ModelMutation, I18nMixin):
- class Arguments:
- input = UserCreateInput(
- description="Fields required to create a customer.", required=True
- )
-
+class CustomerCreate(BaseCustomerCreate):
class Meta:
description = "Creates a new customer."
exclude = ["password"]
model = models.User
permissions = ("account.manage_users",)
- @classmethod
- def clean_input(cls, info, instance, data):
- shipping_address_data = data.pop(SHIPPING_ADDRESS_FIELD, None)
- billing_address_data = data.pop(BILLING_ADDRESS_FIELD, None)
- cleaned_input = super().clean_input(info, instance, data)
-
- if shipping_address_data:
- shipping_address = cls.validate_address(
- shipping_address_data,
- instance=getattr(instance, SHIPPING_ADDRESS_FIELD),
- )
- cleaned_input[SHIPPING_ADDRESS_FIELD] = shipping_address
-
- if billing_address_data:
- billing_address = cls.validate_address(
- billing_address_data, instance=getattr(instance, BILLING_ADDRESS_FIELD)
- )
- cleaned_input[BILLING_ADDRESS_FIELD] = billing_address
- return cleaned_input
-
- @classmethod
- def save(cls, info, instance, cleaned_input):
- # FIXME: save address in user.addresses as well
- default_shipping_address = cleaned_input.get(SHIPPING_ADDRESS_FIELD)
- if default_shipping_address:
- default_shipping_address.save()
- instance.default_shipping_address = default_shipping_address
- default_billing_address = cleaned_input.get(BILLING_ADDRESS_FIELD)
- if default_billing_address:
- default_billing_address.save()
- instance.default_billing_address = default_billing_address
-
- is_creation = instance.pk is None
- super().save(info, instance, cleaned_input)
-
- # The instance is a new object in db, create an event
- if is_creation:
- account_events.customer_account_created_event(user=instance)
-
- if cleaned_input.get("send_password_email"):
- send_set_password_customer_email.delay(instance.pk)
-
class CustomerUpdate(CustomerCreate):
class Arguments: