Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Apply suggestions from code review
Filip Owczarek4f03b452b7b9
diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.pyindex 6e777d862a..f2ebce7107 100644--- a/saleor/graphql/account/mutations/account.py+++ b/saleor/graphql/account/mutations/account.py@@ -15,9 +15,7 @@ from .base import (class AccountRegisterInput(graphene.InputObjectType):- email = graphene.String(- description="The unique email address of the user.", required=True- )+ email = graphene.String(description="The email address of the user.", required=True)password = graphene.String(description="Password", required=True)@@ -210,7 +208,7 @@ class AccountSetDefaultAddress(BaseMutation):address = cls.get_node_or_error(info, data.get("id"), Address)user = info.context.user- if address not in user.addresses.all():+ if not user.addresses.filter(pk=address.pk).exists():raise ValidationError({"id": "The address doesn't belong to that user."})if data.get("type") == AddressTypeEnum.BILLING.value:diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.pyindex 22af70ab7f..b4e1955dbc 100644--- a/saleor/graphql/account/mutations/base.py+++ b/saleor/graphql/account/mutations/base.py@@ -2,6 +2,7 @@ import graphenefrom django.conf import settingsfrom django.contrib.auth.tokens import default_token_generatorfrom django.core.exceptions import ObjectDoesNotExist, ValidationError+from django.db import transactionfrom django.utils.encoding import force_bytesfrom django.utils.http import urlsafe_base64_encodefrom graphql_jwt.exceptions import PermissionDenied@@ -39,12 +40,13 @@ def can_edit_address(user, address):"""Determine whether the user can edit the given address.This method assumes that an address can be edited by:- - users with proper permission (staff)- - customers who "own" the given address.+ - users with proper permissions (staff)+ - customers associated to the given address."""- has_perm = user.has_perm("account.manage_users")- belongs_to_user = address in user.addresses.all()- return has_perm or belongs_to_user+ return (+ user.has_perm("account.manage_users")+ or user.addresses.filter(pk=address.pk).exists()+ )class SetPasswordInput(graphene.InputObjectType):@@ -65,8 +67,8 @@ class SetPassword(ModelMutation):class Meta:description = (- "Sets user password. Token sent by email "- "using RequestPasswordReset mutation."+ "Sets the user's password from the token sent by email "+ "using the RequestPasswordReset mutation.")model = models.User@@ -81,7 +83,7 @@ class SetPassword(ModelMutation):@classmethoddef save(cls, info, instance, cleaned_input):instance.set_password(cleaned_input["password"])- instance.save()+ instance.save(update_fields=["password"])account_events.customer_password_reset_event(user=instance)@@ -89,11 +91,11 @@ class RequestPasswordReset(BaseMutation):class Arguments:email = graphene.String(required=True,- description=("Email of the user that will be used for password recovery."),+ description="Email of the user that will be used for password recovery.",)class Meta:- description = "Sends an email with the account password change link."+ description = "Sends an email with the account password modification link."@classmethoddef perform_mutation(cls, _root, info, **data):@@ -111,13 +113,13 @@ class BaseAddressUpdate(ModelMutation):"""Base mutation for address update used by staff and account."""user = graphene.Field(- User, description="A user instance for which the address was edited."+ User, description="A user object for which the address was edited.")class Arguments:id = graphene.ID(description="ID of the address to update", required=True)input = AddressInput(- description="Fields required to update address", required=True+ description="Fields required to update the address", required=True)class Meta:@@ -140,7 +142,7 @@ class BaseAddressUpdate(ModelMutation):class BaseAddressDelete(ModelDeleteMutation):- """Base mutation for address delete used by staff and account."""+ """Base mutation for address delete used by staff and customers."""user = graphene.Field(User, description="A user instance for which the address was deleted."@@ -180,14 +182,14 @@ class BaseAddressDelete(ModelDeleteMutation):instance.delete()instance.id = db_id- response = cls.success_response(instance)-# Refresh the user instance to clear the default addresses. If the# deleted address was used as default, it would stay cached in the# user instance and the invalid ID returned in the response might cause# an error.user.refresh_from_db()+ response = cls.success_response(instance)+response.user = userreturn response@@ -251,6 +253,7 @@ class BaseCustomerCreate(ModelMutation, I18nMixin):return cleaned_input@classmethod+ @transaction.atomicdef save(cls, info, instance, cleaned_input):# FIXME: save address in user.addresses as welldefault_shipping_address = cleaned_input.get(SHIPPING_ADDRESS_FIELD)diff --git a/saleor/graphql/account/mutations/deprecated_account.py b/saleor/graphql/account/mutations/deprecated_account.pyindex d531a05961..2824d2fdb5 100644--- a/saleor/graphql/account/mutations/deprecated_account.py+++ b/saleor/graphql/account/mutations/deprecated_account.py@@ -38,7 +38,7 @@ class CustomerRegister(ModelMutation):class LoggedUserUpdate(BaseCustomerCreate):class Arguments:input = UserAddressInput(- description="Fields required to update logged in user.", required=True+ description="Fields required to update a logged in user.", required=True)class Meta:@@ -158,7 +158,10 @@ class CustomerPasswordReset(BaseMutation):)class Meta:- description = "Resets the customer's password."+ description = (+ "DEPRECATED: Use RequestPasswordReset instead."+ "Resets the customer's password."+ )@classmethoddef perform_mutation(cls, _root, info, **data):diff --git a/saleor/graphql/account/mutations/deprecated_staff.py b/saleor/graphql/account/mutations/deprecated_staff.pyindex 8fdef4e667..7a0f5e732e 100644--- a/saleor/graphql/account/mutations/deprecated_staff.py+++ b/saleor/graphql/account/mutations/deprecated_staff.py@@ -12,7 +12,7 @@ class PasswordReset(BaseMutation):class Meta:description = (- "DEPRECATED: Use CustomerRequestPasswordReset instead."+ "DEPRECATED: Use RequestPasswordReset instead.""Sends an email with the account password change link to customer.")permissions = ("account.manage_users",)diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.pyindex 35539de224..2e69ebc07d 100644--- a/saleor/graphql/account/mutations/staff.py+++ b/saleor/graphql/account/mutations/staff.py@@ -43,7 +43,7 @@ class StaffInput(UserInput):class StaffCreateInput(StaffInput):send_password_email = graphene.Boolean(- description="Send an email with a link to set a password"+ description="Send an email with a link to set the password")@@ -97,6 +97,7 @@ class CustomerUpdate(CustomerCreate):It overrides the `perform_mutation` base method of ModelMutation."""+# Retrieve the dataoriginal_instance = cls.get_instance(info, **data)data = data.get("input")@@ -299,7 +300,7 @@ class AddressSetDefault(BaseMutation):)user = cls.get_node_or_error(info, user_id, field="user_id", only_type=User)- if address not in user.addresses.all():+ if not user.addresses.filter(pk=address.pk).exists():raise ValidationError({"address_id": "The address doesn't belong to that user."})diff --git a/tests/api/test_account.py b/tests/api/test_account.pyindex c5d3a7c496..6c34243eae 100644--- a/tests/api/test_account.py+++ b/tests/api/test_account.py@@ -1011,7 +1011,7 @@ def test_account_delete(user_api_client):response = user_api_client.post_graphql(ACCOUNT_DELETE_MUTATION, variables)content = get_graphql_content(response)data = content["data"]["accountDelete"]- assert data["errors"] == []+ assert not data["errors"]assert not User.objects.filter(pk=user.id).exists()@@ -1022,7 +1022,7 @@ def test_account_delete_invalid_token(user_api_client):response = user_api_client.post_graphql(ACCOUNT_DELETE_MUTATION, variables)content = get_graphql_content(response)data = content["data"]["accountDelete"]- assert not data["errors"] == []+ assert len(data["errors"]) == 1assert data["errors"][0]["message"] == "Invalid or expired token."assert User.objects.filter(pk=user.id).exists()@@ -1041,7 +1041,7 @@ def test_account_delete_staff_user(staff_api_client):response = staff_api_client.post_graphql(ACCOUNT_DELETE_MUTATION, variables)content = get_graphql_content(response)data = content["data"]["accountDelete"]- assert not data["errors"] == []+ assert len(data["errors"]) == 1assert data["errors"][0]["message"] == "Cannot delete a staff account."assert User.objects.filter(pk=user.id).exists()@@ -1054,7 +1054,7 @@ def test_account_delete_other_customer_token(user_api_client):response = user_api_client.post_graphql(ACCOUNT_DELETE_MUTATION, variables)content = get_graphql_content(response)data = content["data"]["accountDelete"]- assert not data["errors"] == []+ assert len(data["errors"]) == 1assert data["errors"][0]["message"] == "Invalid or expired token."assert User.objects.filter(pk=user.id).exists()assert User.objects.filter(pk=other_user.id).exists()@@ -1989,17 +1989,16 @@ mutation($id: ID!, $type: AddressTypeEnum!) {(ACCOUNT_SET_DEFAULT_ADDRESS_MUTATION, "accountSetDefaultAddress"),],)-def test_customer_set_address_as_default(- user_api_client, address, query, mutation_name-):+def test_customer_set_address_as_default(user_api_client, query, mutation_name):user = user_api_client.useruser.default_billing_address = Noneuser.default_shipping_address = Noneuser.save()assert not user.default_billing_addressassert not user.default_shipping_address+ assert user.addresses.exists()- assert address in user.addresses.all()+ address = user.addresses.first()variables = {"id": graphene.Node.to_global_id("Address", address.id),