Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Create category/collection images in populatedb, create collections by schema
Pacu21265c468acf1
diff --git a/saleor/core/management/commands/populatedb.py b/saleor/core/management/commands/populatedb.pyindex 4866163954..1ebc71b770 100644--- a/saleor/core/management/commands/populatedb.py+++ b/saleor/core/management/commands/populatedb.py@@ -5,10 +5,10 @@ from django.db import connectionfrom ...utils import create_superuserfrom ...utils.random_data import (- add_address_to_admin, create_collections, create_groups, create_menus,- create_orders, create_pages, create_product_sales,- create_products_by_schema, create_shipping_methods, create_users,- create_vouchers, set_featured_products)+ add_address_to_admin, create_collections, create_collections_by_schema,+ create_groups, create_menus, create_orders, create_pages,+ create_product_sales, create_products_by_schema, create_shipping_methods,+ create_users, create_vouchers, set_featured_products)class Command(BaseCommand):@@ -67,7 +67,7 @@ class Command(BaseCommand):self.stdout.write(msg)for msg in set_featured_products(16):self.stdout.write(msg)- for msg in create_collections(2):+ for msg in create_collections_by_schema(self.placeholders_dir):self.stdout.write(msg)for msg in create_pages():self.stdout.write(msg)diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex 98456cb7ba..f8adc7c347 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -29,13 +29,13 @@ from ...shipping.models import ANY_COUNTRY, ShippingMethodfake = Factory.create()STOCK_LOCATION = 'default'-DEFAULT_CATEGORY = 'Default'-DELIVERY_REGIONS = [ANY_COUNTRY, 'US', 'PL', 'DE', 'GB']PRODUCTS_LIST_DIR = 'products_list/'DEFAULT_SCHEMA = {'T-Shirt': {- 'category': 'Apparel',+ 'category': {+ 'name': 'Apparel',+ 'image_name': 'apparel.jpg'},'product_attributes': {'Color': ['Blue', 'White'],'Collar': ['Round', 'V-Neck', 'Polo'],@@ -45,14 +45,18 @@ DEFAULT_SCHEMA = {'images_dir': 't-shirts/','is_shipping_required': True},'Mugs': {- 'category': 'Accessories',+ 'category': {+ 'name': 'Accessories',+ 'image_name': 'accessories.jpg'},'product_attributes': {'Brand': ['Saleor']},'variant_attributes': {},'images_dir': 'mugs/','is_shipping_required': True},'Coffee': {- 'category': 'Groceries',+ 'category': {+ 'name': 'Groceries',+ 'image_name': 'groceries.jpg'},'product_attributes': {'Coffee Genre': ['Arabica', 'Robusta'],'Brand': ['Saleor']},@@ -62,7 +66,9 @@ DEFAULT_SCHEMA = {'images_dir': 'coffee/','is_shipping_required': True},'Candy': {- 'category': 'Groceries',+ 'category': {+ 'name': 'Groceries',+ 'image_name': 'groceries.jpg'},'product_attributes': {'Flavor': ['Sour', 'Sweet'],'Brand': ['Saleor']},@@ -71,7 +77,9 @@ DEFAULT_SCHEMA = {'images_dir': 'candy/','is_shipping_required': True},'E-books': {- 'category': 'Books',+ 'category': {+ 'name': 'Books',+ 'image_name': 'books.jpg'},'product_attributes': {'Author': ['John Doe', 'Milionare Pirate'],'Publisher': ['Mirumee Press', 'Saleor Publishing'],@@ -80,7 +88,9 @@ DEFAULT_SCHEMA = {'images_dir': 'books/','is_shipping_required': False},'Books': {- 'category': 'Books',+ 'category': {+ 'name': 'Books',+ 'image_name': 'books.jpg'},'product_attributes': {'Author': ['John Doe', 'Milionare Pirate'],'Publisher': ['Mirumee Press', 'Saleor Publishing'],@@ -89,6 +99,14 @@ DEFAULT_SCHEMA = {'Cover': ['Soft', 'Hard']},'images_dir': 'books/','is_shipping_required': True}}+COLLECTIONS_SCHEMA = [+ {+ 'name': 'Summer collection',+ 'image_name': 'summer.jpg'},+ {+ 'name': 'Sale',+ 'image_name': 'sale.jpg'}+]def create_attributes_and_values(attribute_data):@@ -186,7 +204,7 @@ def get_price_override(schema, combinations_num, current_price):def create_products_by_type(product_type, schema, placeholder_dir, how_many=10, create_images=True,stdout=None):- category = get_or_create_category(schema, placeholder_dir)+ category = get_or_create_category(schema['category'], placeholder_dir)for dummy in range(how_many):product = create_product(@@ -250,35 +268,28 @@ def get_email(first_name, last_name):_first.lower().decode('utf-8'), _last.lower().decode('utf-8'))-def get_or_create_category(schema, placeholder_dir):- category_name = schema.get('category') or DEFAULT_CATEGORY+def get_or_create_category(category_schema, placeholder_dir):+ category_name = category_schema['name']+ image_name = category_schema['image_name']image_dir = get_product_list_images_dir(placeholder_dir)-defaults = {'description': fake.text(),'slug': fake.slug(category_name),- 'background_image': get_product_list_image(image_dir)}-+ 'background_image': get_product_list_image(image_dir, image_name)}return Category.objects.get_or_create(name=category_name, defaults=defaults)[0]-def get_product_list_image(image_dir):- image_root_dir = os.path.join(settings.PROJECT_ROOT, image_dir)- random_img_path = random.choice(os.listdir(image_root_dir))- img_path = os.path.join(image_dir, random_img_path)- return File(open(img_path, 'rb'))--def get_or_create_product_type(name, **kwargs):return ProductType.objects.get_or_create(name=name, defaults=kwargs)[0]-def get_or_create_collection(name, placeholder_dir):+def get_or_create_collection(name, placeholder_dir, image_name):+ background_image = get_product_list_image(placeholder_dir, image_name)defaults = {'slug': fake.slug(name),- 'background_image': get_product_list_image(placeholder_dir)}- return Collection.objects.get_or_create(name=name, defaults=kwargs)[0]+ 'background_image': background_image}+ return Collection.objects.get_or_create(name=name, defaults=defaults)[0]def create_product(**kwargs):@@ -559,20 +570,19 @@ def add_address_to_admin(email):store_user_address(user, address, True, True)-def create_fake_collection(placeholder_dir):- image_dir = get_product_list_images_dir(- placeholder_dir)- collection_name = '%s collection' % fake.word()+def create_fake_collection(placeholder_dir, collection_data):+ image_dir = get_product_list_images_dir(placeholder_dir)collection = get_or_create_collection(- name=collection_name, placeholder_dir=image_dir)+ name=collection_data['name'], placeholder_dir=image_dir,+ image_name=collection_data['image_name'])products = Product.objects.order_by('?')[:4]collection.products.add(*products)return collection-def create_collections(placeholder_dir, how_many=2):- for dummy in range(how_many):- collection = create_fake_collection(placeholder_dir)+def create_collections_by_schema(placeholder_dir, schema=COLLECTIONS_SCHEMA):+ for collection_data in COLLECTIONS_SCHEMA:+ collection = create_fake_collection(placeholder_dir, collection_data)yield 'Collection: %s' % (collection,)@@ -627,3 +637,8 @@ 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_product_list_image(image_dir, image_name):+ img_path = os.path.join(image_dir, image_name)+ return File(open(img_path, 'rb'))diff --git a/tests/test_core.py b/tests/test_core.pyindex 62c90df4ae..05ef6f5f69 100644--- a/tests/test_core.py+++ b/tests/test_core.py@@ -15,7 +15,9 @@ from saleor.shipping.models import ShippingMethodtype_schema = {'Vegetable': {- 'category': 'Food',+ 'category': {+ 'name': 'Food',+ 'image_name': 'books.jpg'},'product_attributes': {'Sweetness': ['Sweet', 'Sour'],'Healthiness': ['Healthy', 'Not really']},@@ -116,7 +118,10 @@ def test_create_product_types_by_schema(db):assert product_type.is_shipping_required-def test_create_products_by_type(db):+def test_create_products_by_type(db, monkeypatch):+ monkeypatch.setattr(+ 'saleor.core.utils.random_data.get_product_list_image',+ Mock(return_value=''))assert Product.objects.all().count() == 0how_many = 5product_type = random_data.create_product_types_by_schema(@@ -127,7 +132,10 @@ def test_create_products_by_type(db):assert Product.objects.all().count() == how_many-def test_create_fake_order(db):+def test_create_fake_order(db, monkeypatch):+ monkeypatch.setattr(+ 'saleor.core.utils.random_data.get_product_list_image',+ Mock(return_value=''))for _ in random_data.create_shipping_methods():passfor _ in random_data.create_users(3):