Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Implement schema-based product generator
Artur Smęt804f50903cee
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex d3c4dfd9dc..476ff7660e 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -6,10 +6,13 @@ import unicodedatafrom django.conf import settingsfrom django.core.files import File+from django.template.defaultfilters import slugifyfrom faker import Factoryfrom faker.providers import BaseProviderfrom prices import Price+from saleor.product.models import AttributeChoiceValue+from saleor.product.models import ProductAttributefrom ...order.models import DeliveryGroup, Order, OrderedItem, Paymentfrom ...product.models import (Category, Product, ProductClass, ProductImage,ProductVariant, Stock, StockLocation)@@ -21,6 +24,111 @@ STOCK_LOCATION = 'default'DELIVERY_REGIONS = [ANY_COUNTRY, 'US', 'PL', 'DE', 'GB']+REAL_DATA = {+ 'T-Shirt': {+ 'product_attributes': {+ 'Color': ['Blue', 'White'],+ 'Collar': ['Round', 'V-Neck', 'Polo']+ },+ 'variant_attributes': {+ 'Size': ['XS', 'S', 'M', 'L', 'XL', 'XXL']+ },+ 'images_dir': 't-shirts/'+ },+ 'Mugs': {+ 'product_attributes': {},+ 'variant_attributes': {},+ 'images_dir': 'mugs/'+ },+ 'Coffee': {+ 'product_attributes': {+ 'Coffee Genre': ['Arabica', 'Robusta']+ },+ 'variant_attributes': {+ 'Box Size': ['100g', '250g', '500g', '1kg']+ },+ 'different_variant_prices': True,+ 'images_dir': 'coffee/'+ }+}+++def create_attributes_and_values(schema, attribute_key):+ attributes = []+ attribute_data = schema.get(attribute_key, {})+ for attribute_name, attribute_values in attribute_data.items():+ attribute = create_attribute(+ name=slugify(attribute_name), display=attribute_name)+ for value in attribute_values:+ create_attribute_value(attribute, display=value)+ attributes.append(attribute)+ return attributes+++def create_product_class_with_attributes(name, schema):+ product_class = get_or_create_product_class(name=name)+ product_attributes = create_attributes_and_values(+ schema, 'product_attributes')+ variant_attributes = create_attributes_and_values(+ schema, 'variant_attributes')+ product_class.product_attributes.add(*product_attributes)+ product_class.variant_attributes.add(*variant_attributes)+ return product_class+++def create_real_product_classes(root_schema):+ results = []+ for product_class_name, schema in root_schema.items():+ product_class = create_product_class_with_attributes(+ product_class_name, schema)+ results.append((product_class, schema))+ return results+++def set_product_attributes(product, product_class):+ attr_dict = {}+ for product_attribute in product_class.product_attributes.all():+ value = random.choice(product_attribute.values.all())+ attr_dict[str(product_attribute.pk)] = str(value.pk)+ product.attributes = attr_dict+ product.save(update_fields=['attributes'])+++def set_variant_attributes(variant, product_class):+ attr_dict = {}+ for product_attribute in product_class.variant_attributes.all():+ value = random.choice(product_attribute.values.all())+ attr_dict[str(product_attribute.pk)] = str(value.pk)+ variant.attributes = attr_dict+ variant.save(update_fields=['attributes'])+++def create_items_by_class(product_class, schema,+ placeholder_dir, how_many=10, create_images=True):+ default_category = get_or_create_category('Default')++ for dummy in range(how_many):+ product = create_product(product_class=product_class)+ set_product_attributes(product, product_class)+ product.categories.add(default_category)+ if create_images:+ class_placeholders = os.path.join(+ placeholder_dir, schema['images_dir'])+ create_product_images(+ product, random.randrange(1, 5), class_placeholders)+ num_variants = random.randrange(3, 10)+ for _ in range(num_variants):+ variant = create_variant(product)+ set_variant_attributes(variant, product_class)+ print('Product: %s (%s), %s variant(s)' % (product, product_class.name, num_variants))+++def create_items_by_schema(schema, placeholder_dir, how_many, create_images):+ for product_class, class_schema in create_real_product_classes(schema):+ create_items_by_class(+ product_class, class_schema, placeholder_dir,+ how_many=how_many, create_images=create_images)+class SaleorProvider(BaseProvider):def price(self):@@ -79,7 +187,7 @@ def create_stock(variant, **kwargs):def create_variant(product, **kwargs):defaults = {'name': fake.word(),- 'sku': fake.random_int(1, 100000),+ 'sku': '%s-%s' % (product.pk, fake.random_int(1, 100000)),'product': product}defaults.update(kwargs)variant = ProductVariant.objects.create(**defaults)@@ -97,6 +205,25 @@ def create_product_image(product, placeholder_dir):return image+def create_attribute(**kwargs):+ name = fake.word()+ defaults = {+ 'name': name,+ 'display': name.title()}+ defaults.update(kwargs)+ attribute = ProductAttribute.objects.get_or_create(**defaults)[0]+ return attribute+++def create_attribute_value(attribute, **kwargs):+ defaults = {+ 'display': fake.word(),+ 'attribute': attribute}+ defaults.update(kwargs)+ attribute_value = AttributeChoiceValue.objects.get_or_create(**defaults)[0]+ return attribute_value++def create_product_images(product, how_many, placeholder_dir):for dummy in range(how_many):create_product_image(product, placeholder_dir)diff --git a/saleor/product/models/utils.py b/saleor/product/models/utils.pyindex 2e5aef5e93..08150d5cf5 100644--- a/saleor/product/models/utils.py+++ b/saleor/product/models/utils.py@@ -3,7 +3,7 @@ def get_attributes_display_map(variant, attributes):for attribute in attributes:value = variant.get_attribute(attribute.pk)if value:- choices = {a.pk: a for a in attribute.values.all()}+ choices = {str(a.pk): a for a in attribute.values.all()}attr = choices.get(value)if attr:display[attribute.pk] = attr