Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add avatar to superuser in populatedb
Paweł Zarębskic66a6e7581d1
diff --git a/saleor/core/management/commands/populatedb.py b/saleor/core/management/commands/populatedb.pyindex 0c61717fdf..7916eb09a3 100644--- a/saleor/core/management/commands/populatedb.py+++ b/saleor/core/management/commands/populatedb.py@@ -15,7 +15,8 @@ from ...utils.random_data import (class Command(BaseCommand):help = 'Populate database with test objects'- placeholders_dir = r'saleor/static/placeholders/'+ placeholders_dir = 'saleor/static/placeholders/'+ avatars_dir = 'saleor/static/images/avatars/'def add_arguments(self, parser):parser.add_argument(@@ -98,7 +99,7 @@ class Command(BaseCommand):if options['createsuperuser']:credentials = {'email': '[email protected]', 'password': 'admin'}- msg = create_superuser(credentials)+ msg = create_superuser(credentials, self.avatars_dir)self.stdout.write(msg)add_address_to_admin(credentials['email'])if not options['withoutsearch']:diff --git a/saleor/core/utils/__init__.py b/saleor/core/utils/__init__.pyindex 933648eeea..ef2c67a1ab 100644--- a/saleor/core/utils/__init__.py+++ b/saleor/core/utils/__init__.py@@ -1,5 +1,6 @@import decimalimport logging+import osfrom json import JSONEncoderfrom urllib.parse import urljoin@@ -9,6 +10,7 @@ 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@@ -134,13 +136,21 @@ def serialize_decimal(obj):return JSONEncoder().default(obj)-def create_superuser(credentials):+def create_superuser(credentials, avatars_dir):user, created = User.objects.get_or_create(email=credentials['email'], defaults={'is_active': True, 'is_staff': True, 'is_superuser': True})if created:+ avatar = get_image(avatars_dir, 'avatar1.png')+ user.avatar = avataruser.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' % credentialselse:msg = 'Superuser already exists - %(email)s' % credentials@@ -172,3 +182,8 @@ def get_country_name_by_code(country_code):(name for code, name in COUNTRY_CODE_CHOICES if code == country_code),country_code)return country_name+++def get_image(image_dir, image_name):+ img_path = os.path.join(image_dir, image_name)+ return File(open(img_path, 'rb'), name=image_name)diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex 061a13f078..6912e447e6 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -9,7 +9,6 @@ from unittest.mock import patchfrom django.conf import settingsfrom django.contrib.sites.models import Site-from django.core.files import Filefrom django_countries.fields import Countryfrom faker import Factoryfrom faker.providers import BaseProvider@@ -40,6 +39,7 @@ from ...product.thumbnails import (create_collection_background_image_thumbnails, create_product_thumbnails)from ...shipping.models import ShippingMethod, ShippingMethodType, ShippingZonefrom ...shipping.utils import get_taxed_shipping_price+from ..utils import get_imagefake = Factory.create()@@ -692,8 +692,3 @@ def get_product_list_images_dir(placeholder_dir):product_list_images_dir = os.path.join(placeholder_dir, PRODUCTS_LIST_DIR)return product_list_images_dir---def get_image(image_dir, image_name):- img_path = os.path.join(image_dir, image_name)- return File(open(img_path, 'rb'), name=image_name)diff --git a/tests/test_core.py b/tests/test_core.pyindex 94fed09771..b11f70f42b 100644--- a/tests/test_core.py+++ b/tests/test_core.py@@ -64,16 +64,20 @@ def test_get_currency_for_country(country, expected_currency, monkeypatch):assert currency == expected_currency-def test_create_superuser(db, client):+def test_create_superuser(db, client, monkeypatch, image):+ monkeypatch.setattr(+ 'saleor.core.utils.get_image',+ Mock(return_value=image)+ )credentials = {'email': '[email protected]', 'password': 'admin'}# Test admin creationassert User.objects.all().count() == 0- create_superuser(credentials)+ create_superuser(credentials, '')assert User.objects.all().count() == 1admin = User.objects.all().first()assert admin.is_superuser# Test duplicating- create_superuser(credentials)+ create_superuser(credentials, '')assert User.objects.all().count() == 1# Test logging inresponse = client.post(reverse('account:login'),