Skip to content

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

Do not create a new instance on update

Adam Janik02fa9969ce8c
diff --git a/saleor/graphql/account/mutations.py b/saleor/graphql/account/mutations.py
index 3b51f68ee5..dd62dbf034 100644
--- a/saleor/graphql/account/mutations.py
+++ b/saleor/graphql/account/mutations.py
@@ -26,11 +26,14 @@ def send_user_password_reset_email(user, site):
class UserInput(graphene.InputObjectType):
- default_billing_address = AddressInput(
- description='Billing address of the customer.')
email = graphene.String(
description='The unique email address of the user.')
note = graphene.String(description='A note about the user.')
+
+
+class CustomerInput(UserInput):
+ default_billing_address = AddressInput(
+ description='Billing address of the customer.')
default_shipping_address = AddressInput(
description='Shipping address of the customer.')
@@ -79,9 +82,11 @@ class CustomerCreate(ModelMutation):
cleaned_input = super().clean_input(info, instance, input, errors)
if default_shipping_address:
- default_shipping_address = models.Address(**default_shipping_address)
+ default_shipping_address = models.Address(
+ **default_shipping_address)
cls.clean_instance(default_shipping_address, errors)
- cleaned_input['default_shipping_address'] = default_shipping_address
+ cleaned_input[
+ 'default_shipping_address'] = default_shipping_address
if default_billing_address:
default_billing_address = models.Address(**default_billing_address)
cls.clean_instance(default_billing_address, errors)
@@ -89,10 +94,10 @@ class CustomerCreate(ModelMutation):
return cleaned_input
-
@classmethod
def save(cls, info, instance, cleaned_input):
- default_shipping_address = cleaned_input.get('default_shipping_address')
+ default_shipping_address = cleaned_input.get(
+ 'default_shipping_address')
if default_shipping_address:
default_shipping_address.save()
instance.default_shipping_address = default_shipping_address
@@ -101,14 +106,13 @@ class CustomerCreate(ModelMutation):
default_billing_address.save()
instance.default_billing_address = default_billing_address
super().save(info, instance, cleaned_input)
- instance.save(update_fields=['default_billing_address', 'default_shipping_address'])
class CustomerUpdate(CustomerCreate):
class Arguments:
id = graphene.ID(
description='ID of a customer to update.', required=True)
- input = UserInput(
+ input = CustomerInput(
description='Fields required to update a customer.', required=True)
class Meta:
@@ -116,6 +120,27 @@ class CustomerUpdate(CustomerCreate):
exclude = ['password']
model = models.User
+ @classmethod
+ def clean_input(cls, info, instance, input, errors):
+ default_shipping_address = input.pop('default_shipping_address', None)
+ default_billing_address = input.pop('default_billing_address', None)
+ cleaned_input = super().clean_input(info, instance, input, errors)
+
+ if default_shipping_address:
+ instance.default_shipping_address = models.Address(
+ **default_shipping_address)
+ cls.clean_instance(instance.default_shipping_address, errors)
+ cleaned_input[
+ 'default_shipping_address'] = instance.default_shipping_address
+ if default_billing_address:
+ instance.default_billing_address = models.Address(
+ **default_billing_address)
+ cls.clean_instance(instance.default_billing_address, errors)
+ cleaned_input[
+ 'default_billing_address'] = instance.default_billing_address
+
+ return cleaned_input
+
class StaffCreate(ModelMutation):
class Arguments:
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index 22a4a90f68..18e9efac13 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -283,10 +283,13 @@ def test_customer_create(
assert 'token' in call_context
-def test_customer_update(admin_api_client, customer_user, user_api_client):
+def test_customer_update(
+ admin_api_client, customer_user, user_api_client, address):
query = """
- mutation UpdateCustomer($id: ID!, $note: String) {
- customerUpdate(id: $id, input: {note: $note}) {
+ mutation UpdateCustomer(
+ $id: ID!, $note: String, $billing: AddressInput) {
+ customerUpdate(id: $id, input: {note: $note,
+ defaultBillingAddress: $billing}) {
errors {
field
message
@@ -304,7 +307,8 @@ def test_customer_update(admin_api_client, customer_user, user_api_client):
id = graphene.Node.to_global_id('User', customer_user.id)
note = 'Test update note'
- variables = json.dumps({'id': id, 'note': note})
+ address_data = convert_dict_keys_to_camel_case(address.as_data())
+ variables = json.dumps({'id': id, 'note': note, 'billing': address_data})
# check unauthorized access
response = user_api_client.post(
@@ -314,6 +318,11 @@ def test_customer_update(admin_api_client, customer_user, user_api_client):
response = admin_api_client.post(
reverse('api'), {'query': query, 'variables': variables})
content = get_graphql_content(response)
+
+ User = get_user_model()
+ customer = User.objects.get(email=customer_user.email)
+
+ assert customer.default_billing_address == address
assert 'errors' not in content
data = content['data']['customerUpdate']
assert data['errors'] == []