Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Apply changes after review
Maciej Korycinski55e9db491f7c
diff --git a/saleor/account/utils.py b/saleor/account/utils.pyindex e773701650..99c2ac6479 100644--- a/saleor/account/utils.py+++ b/saleor/account/utils.py@@ -1,5 +1,17 @@+import os+import random++from django.conf import settings+from django.core.files import File+from ..checkout import AddressType+from ..core.utils import create_thumbnailsfrom ..extensions.manager import get_extensions_manager+from .models import User++AVATARS_PATH = os.path.join(+ settings.PROJECT_ROOT, "saleor", "static", "images", "avatars"+)def store_user_address(user, address, address_type):@@ -63,3 +75,29 @@ def get_user_last_name(user):if user.default_billing_address:return user.default_billing_address.last_namereturn None+++def create_superuser(credentials):++ user, created = User.objects.get_or_create(+ email=credentials["email"],+ defaults={"is_active": True, "is_staff": True, "is_superuser": True},+ )+ if created:+ user.avatar = get_random_avatar()+ user.set_password(credentials["password"])+ user.save()+ create_thumbnails(+ pk=user.pk, model=User, size_set="user_avatars", image_attr="avatar"+ )+ msg = "Superuser - %(email)s/%(password)s" % credentials+ else:+ msg = "Superuser already exists - %(email)s" % credentials+ return msg+++def get_random_avatar():+ """Return random avatar picked from a pool of static avatars."""+ avatar_name = random.choice(os.listdir(AVATARS_PATH))+ avatar_path = os.path.join(AVATARS_PATH, avatar_name)+ return File(open(avatar_path, "rb"), name=avatar_name)diff --git a/saleor/core/management/commands/populatedb.py b/saleor/core/management/commands/populatedb.pyindex ac7fa6cfdf..b75e68755e 100644--- a/saleor/core/management/commands/populatedb.py+++ b/saleor/core/management/commands/populatedb.py@@ -6,7 +6,7 @@ from django.core.management import call_commandfrom django.core.management.base import BaseCommandfrom django.db import connection-from ...utils import create_superuser+from ....account.utils import create_superuserfrom ...utils.random_data import (add_address_to_admin,create_gift_card,diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.pyindex bf7296561e..568eacb95f 100644--- a/saleor/core/utils/__init__.py+++ b/saleor/core/utils/__init__.py@@ -1,7 +1,5 @@import decimalimport logging-import os-import randomimport socketfrom json import JSONEncoderfrom urllib.parse import urljoin@@ -11,7 +9,6 @@ from django import formsfrom django.conf import settingsfrom django.contrib.sites.models import Sitefrom django.core import serializers-from django.core.files import Filefrom django.core.paginator import InvalidPage, Paginatorfrom django.http import Http404from django.utils.encoding import iri_to_uri, smart_text@@ -31,11 +28,6 @@ georeader = geolite2.reader()logger = logging.getLogger(__name__)-AVATARS_PATH = os.path.join(- settings.PROJECT_ROOT, "saleor", "static", "images", "avatars"-)--class CategoryChoiceField(forms.ModelChoiceField):def label_from_instance(self, obj):# pylint: disable=W0212@@ -170,26 +162,6 @@ def serialize_decimal(obj):return JSONEncoder().default(obj)-def create_superuser(credentials):- from ...account.models import User-- user, created = User.objects.get_or_create(- email=credentials["email"],- defaults={"is_active": True, "is_staff": True, "is_superuser": True},- )- if created:- user.avatar = get_random_avatar()- user.set_password(credentials["password"])- user.save()- create_thumbnails(- pk=user.pk, model=User, size_set="user_avatars", image_attr="avatar"- )- msg = "Superuser - %(email)s/%(password)s" % credentials- else:- msg = "Superuser already exists - %(email)s" % credentials- return msg--def create_thumbnails(pk, model, size_set, image_attr=None):instance = model.objects.get(pk=pk)if not image_attr:@@ -219,10 +191,3 @@ def get_country_name_by_code(country_code):def get_company_address():return Site.objects.get_current().settings.company_address---def get_random_avatar():- """Return random avatar picked from a pool of static avatars."""- avatar_name = random.choice(os.listdir(AVATARS_PATH))- avatar_path = os.path.join(AVATARS_PATH, avatar_name)- return File(open(avatar_path, "rb"), name=avatar_name)diff --git a/saleor/extensions/plugins/anonymize/plugin.py b/saleor/extensions/plugins/anonymize/plugin.pyindex efc2dd934b..44d85bf8c1 100644--- a/saleor/extensions/plugins/anonymize/plugin.py+++ b/saleor/extensions/plugins/anonymize/plugin.py@@ -43,8 +43,8 @@ class AnonymizePlugin(BasePlugin):defaults = {"name": cls.PLUGIN_NAME,"description": (- "Hide all user data like shipping and billing addresses, email and "- "phone number"+ "Anonymize customer's personal data in the checkout, such as shipping "+ "or billing address, email and phone number."),"active": True,"configuration": None,diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.pyindex cdb6859ef7..af8aa31bf9 100644--- a/saleor/graphql/account/mutations/staff.py+++ b/saleor/graphql/account/mutations/staff.py@@ -8,9 +8,9 @@ from graphql_jwt.exceptions import PermissionDeniedfrom ....account import events as account_events, models, utilsfrom ....account.error_codes import AccountErrorCodefrom ....account.thumbnails import create_user_avatar_thumbnails+from ....account.utils import get_random_avatarfrom ....checkout import AddressTypefrom ....core.permissions import get_permissions-from ....core.utils import get_random_avatarfrom ....core.utils.url import validate_storefront_urlfrom ....dashboard.emails import send_set_password_email_with_urlfrom ....dashboard.staff.utils import remove_staff_memberdiff --git a/tests/api/test_account.py b/tests/api/test_account.pyindex f1496b84d8..758484ae15 100644--- a/tests/api/test_account.py+++ b/tests/api/test_account.py@@ -16,8 +16,8 @@ from prices import Moneyfrom saleor.account import events as account_eventsfrom saleor.account.error_codes import AccountErrorCodefrom saleor.account.models import Address, User+from saleor.account.utils import get_random_avatarfrom saleor.checkout import AddressType-from saleor.core.utils import get_random_avatarfrom saleor.graphql.account.mutations.base import INVALID_TOKENfrom saleor.graphql.account.mutations.staff import (CustomerDelete,diff --git a/tests/test_account.py b/tests/test_account.pyindex b091d8b4aa..1d25de5e81 100644--- a/tests/test_account.py+++ b/tests/test_account.py@@ -19,9 +19,12 @@ from saleor.account import forms, i18nfrom saleor.account.forms import FormWithReCaptcha, NameFormfrom saleor.account.models import Userfrom saleor.account.templatetags.i18n_address_tags import format_address-from saleor.account.utils import get_user_first_name, get_user_last_name+from saleor.account.utils import (+ get_random_avatar,+ get_user_first_name,+ get_user_last_name,+)from saleor.account.validators import validate_possible_number-from saleor.core.utils import get_random_avatar@pytest.mark.parametrize("country", ["CN", "PL", "US", "IE"])diff --git a/tests/test_core.py b/tests/test_core.pyindex 76bb1cbc80..9f8ac67acd 100644--- a/tests/test_core.py+++ b/tests/test_core.py@@ -12,11 +12,11 @@ from measurement.measures import Weightfrom prices import Moneyfrom saleor.account.models import Address, User+from saleor.account.utils import create_superuserfrom saleor.core.storages import S3MediaStoragefrom saleor.core.utils import (Country,build_absolute_uri,- create_superuser,create_thumbnails,format_money,get_client_ip,