Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Create products by schema instead of creating completely random ones
Artur Smęt69c967fd51fb
diff --git a/saleor/core/management/commands/populatedb.py b/saleor/core/management/commands/populatedb.pyindex b7094f51b2..5fa353dc12 100644--- a/saleor/core/management/commands/populatedb.py+++ b/saleor/core/management/commands/populatedb.py@@ -3,8 +3,8 @@ from django.db import connectionfrom ....userprofile.models import Userfrom ...utils.random_data import (- create_items, create_orders, create_users, create_shipping_methods,- create_items_by_schema, REAL_DATA)+ create_orders, create_users, create_shipping_methods,+ create_items_by_schema)class Command(BaseCommand):@@ -24,12 +24,6 @@ class Command(BaseCommand):dest='withoutimages',default=False,help='Don\'t create product images')- parser.add_argument(- '--real-data',- action='store_true',- dest='real_data',- default=False,- help='Create almost real data')def make_database_faster(self):'''Sacrifices some of the safeguards of sqlite3 for speed@@ -47,11 +41,8 @@ class Command(BaseCommand):create_images = not options['withoutimages']for msg in create_shipping_methods():self.stdout.write(msg)- if options['real_data']:- create_items_by_schema(REAL_DATA, self.placeholders_dir, 10, create_images, stdout=self.stdout)- else:- for msg in create_items(self.placeholders_dir, 40, create_images):- self.stdout.write(msg)+ create_items_by_schema(self.placeholders_dir, 10, create_images,+ stdout=self.stdout)for msg in create_users(20):self.stdout.write(msg)for msg in create_orders(20):diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex 11420b0c5e..be35d628db 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -27,7 +27,7 @@ STOCK_LOCATION = 'default'DELIVERY_REGIONS = [ANY_COUNTRY, 'US', 'PL', 'DE', 'GB']-REAL_DATA = {+DEFAULT_SCHEMA = {'T-Shirt': {'product_attributes': {'Color': ['Blue', 'White'],@@ -79,7 +79,7 @@ def create_product_class_with_attributes(name, schema):return product_class-def create_real_product_classes(root_schema):+def create_product_classes_by_schema(root_schema):results = []for product_class_name, schema in root_schema.items():product_class = create_product_class_with_attributes(@@ -133,6 +133,11 @@ def get_variant_combinations(product):for attr_value in combination]+def get_price_override(schema):+ if schema.get('different_variant_prices'):+ return fake.price()++def create_items_by_class(product_class, schema,placeholder_dir, how_many=10, create_images=True,stdout=None):@@ -149,7 +154,8 @@ def create_items_by_class(product_class, schema,product, random.randrange(1, 5), class_placeholders)variant_combinations = get_variant_combinations(product)for attr_combination in variant_combinations:- create_variant(product, attributes=attr_combination)+ create_variant(product, attributes=attr_combination,+ price_override=get_price_override(schema))if not variant_combinations:# Create min one variant for products without variant level attrscreate_variant(product)@@ -158,8 +164,9 @@ def create_items_by_class(product_class, schema,product, product_class.name, len(variant_combinations) or 1))-def create_items_by_schema(schema, placeholder_dir, how_many, create_images, stdout):- for product_class, class_schema in create_real_product_classes(schema):+def create_items_by_schema(placeholder_dir, how_many, create_images, stdout,+ schema=DEFAULT_SCHEMA):+ for product_class, class_schema in create_product_classes_by_schema(schema):create_items_by_class(product_class, class_schema, placeholder_dir,how_many=how_many, create_images=create_images, stdout=stdout)@@ -264,22 +271,6 @@ def create_product_images(product, how_many, placeholder_dir):create_product_image(product, placeholder_dir)-def create_items(placeholder_dir, how_many=10, create_images=True):- default_category = get_or_create_category('Default')- default_product_class = get_or_create_product_class('Default')-- for dummy in range(how_many):- product = create_product(product_class=default_product_class)- product.categories.add(default_category)- if create_images:- create_product_images(- product, random.randrange(1, 5), placeholder_dir)- num_variants = random.randrange(1, 5)- for _ in range(num_variants):- create_variant(product)- yield 'Product: %s, %s variant(s)' % (product, num_variants)--def create_address():address = Address.objects.create(first_name=fake.first_name(),