Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use GenericProduct model in populatedb script
Marcin Gębala6391b8ac1ac9
diff --git a/saleor/product/management/commands/populatedb.py b/saleor/product/management/commands/populatedb.pyindex 73be4d5554..3492474fbb 100644--- a/saleor/product/management/commands/populatedb.py+++ b/saleor/product/management/commands/populatedb.py@@ -1,22 +1,19 @@-from os.path import exists, join+from os.path import existsfrom django.core.management.base import BaseCommand, CommandError-from django.core.management import call_commandfrom utils.create_random_data import create_items, create_usersclass Command(BaseCommand):help = 'Populate database with test objects'- BASE_DIR = r'saleor/static/placeholders/'- required_dirs = [join(BASE_DIR, 'shirts'), join(BASE_DIR, 'bags')]+ placeholders_dir = r'saleor/static/placeholders/'def handle(self, *args, **options):- if not all(exists(path) for path in self.required_dirs):- msg = 'Directories %s with images are required.' % ', '.join(- self.required_dirs)+ if not exists(self.placeholders_dir):+ msg = 'Directory %s with images is required.' % self.placeholders_dirraise CommandError(msg)- for msg in create_items(self.BASE_DIR, 10):+ for msg in create_items(self.placeholders_dir, 10):self.stdout.write(msg)for msg in create_users(10):self.stdout.write(msg)diff --git a/utils/create_random_data.py b/utils/create_random_data.pyindex 86ec41a177..5bde76340f 100644--- a/utils/create_random_data.py+++ b/utils/create_random_data.py@@ -1,33 +1,19 @@from __future__ import unicode_literals-import randomimport os+import randomimport unicodedatafrom faker import Factoryfrom django.core.files import File-from saleor.product.models import (Shirt, ShirtVariant,- Bag, BagVariant, ProductImage)-from saleor.product.models import Category, Color+from saleor.product.models import (GenericProduct, GenericVariant, ProductImage)+from saleor.product.models import Categoryfrom saleor.userprofile.models import User, Address-fake = Factory.create()PRODUCT_COLLECTIONS = fake.words(10)-def create_color(**kwargs):- r = lambda: random.randint(0, 255)-- defaults = {- 'name': fake.word(),- 'color': '%02X%02X%02X' % (r(), r(), r())- }- defaults.update(kwargs)-- return Color.objects.create(**defaults)--def get_or_create_category(name, **kwargs):defaults = {'description': fake.text()@@ -38,7 +24,7 @@ def get_or_create_category(name, **kwargs):return Category.objects.get_or_create(name=name, defaults=defaults)[0]-def create_product(product_type, **kwargs):+def create_product(**kwargs):if random.choice([True, False]):collection = random.choice(PRODUCT_COLLECTIONS)else:@@ -48,32 +34,24 @@ def create_product(product_type, **kwargs):'name': fake.company(),'price': fake.pyfloat(2, 2, positive=True),'collection': collection,- 'color': Color.objects.order_by('?')[0],'weight': fake.random_digit(),'description': '\n\n'.join(fake.paragraphs(5))}defaults.update(kwargs)- return product_type.objects.create(**defaults)+ return GenericProduct.objects.create(**defaults)def create_variant(product, **kwargs):defaults = {+ 'name': fake.word(),'stock': fake.random_int(),'sku': fake.random_int(1, 100000),'product': product}- if isinstance(product, Shirt):- if not 'size' in kwargs:- defaults['size'] = random.choice(ShirtVariant.SIZE_CHOICES)[0]- variant_class = ShirtVariant- elif isinstance(product, Bag):- variant_class = BagVariant- else:- raise NotImplementeddefaults.update(kwargs)+ return GenericVariant.objects.create(**defaults)- return variant_class.objects.create(**defaults)def create_product_image(product, placeholder_dir):@@ -92,45 +70,18 @@ def create_product_images(product, how_many, placeholder_dir):create_product_image(product, placeholder_dir)-def create_shirt(**kwargs):- return create_product(Shirt, **kwargs)---def create_bag(**kwargs):- return create_product(Bag, **kwargs)--def create_items(placeholder_dir, how_many=10):- # Create few colors- [create_color() for i in range(5)]-- shirt_category = get_or_create_category('Shirts')- bag_category = get_or_create_category('Grocery bags')+ default_category = get_or_create_category('Default')for i in range(how_many):- # Shirt- shirt = create_shirt()- shirt.categories.add(shirt_category)- create_product_images(shirt, random.randrange(1, 5),- placeholder_dir + "shirts")- # Bag- bag = create_bag(collection='')- bag.categories.add(bag_category)- create_product_images(bag, random.randrange(1, 5),- placeholder_dir + "bags")- # chance to generate couple of sizes- for size in ShirtVariant.SIZE_CHOICES:- # Create min. one size- if shirt.variants.count() == 0:- create_variant(shirt, size=size[0])- continue- if random.choice([True, False]):- create_variant(shirt, size=size[0])-- create_variant(bag)-- yield "Shirt - %s %s Variants" % (shirt, shirt.variants.count())- yield "Bag - %s %s Variants" % (bag, bag.variants.count())+ product = create_product(collection='')+ product.categories.add(default_category)+ create_product_images(product, random.randrange(1, 5), placeholder_dir)++ if random.choice([True, False]):+ create_variant(product)++ yield "Product - %s %s Variants" % (product, product.variants.count())def create_fake_user():