Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add ability to create test products without images
Adam Bogdał1bf582429748
diff --git a/saleor/product/management/commands/populatedb.py b/saleor/product/management/commands/populatedb.pyindex 3492474fbb..5c2dfedd3d 100644--- a/saleor/product/management/commands/populatedb.py+++ b/saleor/product/management/commands/populatedb.py@@ -1,19 +1,17 @@-from os.path import exists+from os.path import exists, joinfrom 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'- placeholders_dir = r'saleor/static/placeholders/'+ BASE_DIR = r'saleor/static/placeholders/'def handle(self, *args, **options):- if not exists(self.placeholders_dir):- msg = 'Directory %s with images is required.' % self.placeholders_dir- raise CommandError(msg)- for msg in create_items(self.placeholders_dir, 10):+ for msg in create_items(self.BASE_DIR, 10):self.stdout.write(msg)for msg in create_users(10):self.stdout.write(msg)diff --git a/saleor/product/templates/product/details.html b/saleor/product/templates/product/details.htmlindex 83a061b21e..5ab783046e 100644--- a/saleor/product/templates/product/details.html+++ b/saleor/product/templates/product/details.html@@ -11,11 +11,7 @@{% block breadcrumb %}{{ block.super }}- {% with product.get_first_category as category %}- {% if category %}- <li><a href="{{ category.get_absolute_url }}">{{ category }}</a></li>- {% endif %}- {% endwith %}+ <li><a href="{{ product.category.get_absolute_url }}">{{ product.category }}</a></li><li><a href="{{ product.get_absolute_url }}">{{ product }}</a></li>{% endblock breadcrumb %}@@ -27,7 +23,7 @@{{ product }}</h1>- {% if product.is_in_stock %}+ {% if product.is_available %}{% with price_range=product|discounted_price_range:request.discounts undiscounted=product.get_price_range %}{% with discount=undiscounted.min_price|price_difference:price_range.min_price %}<p class="lead" itemprop="offers" itemscope itemtype="http://schema.org/Offer"@@ -57,7 +53,7 @@<hr></div>- {% if product.is_in_stock %}+ {% if product.is_available %}{% block orderform %}<form role="form" class="product-form form-vertical" method="post" action=".">{% csrf_token %}@@ -80,6 +76,28 @@{{ product.description|markdown }}</div>+ {% if related_products %}+ <hr>++ <h4>{% trans "Also in this collection" %}</h4>++ <div class="row product-related">+ {% for related in related_products %}+ {% if related.id != product.id %}+ <a href="{{ related.get_absolute_url }}" class="col-xs-3">+ {% if related.images.first %}+ <img src="{{ related.images.first.image.crop.280x280 }}" alt=""+ class="thumbnail img-responsive">+ {% else %}+ <img src="http://placehold.it/280x280/&text=Product Image" alt=""+ class="thumbnail img-responsive">+ {% endif %}+ </a>+ {% endif %}+ {% endfor %}+ </div>+ {% endif %}+</div>{% with images=product.images.all %}diff --git a/saleor/static/placeholders/bags/mirumee-bag.jpg b/saleor/static/placeholders/bags/mirumee-bag.jpgdeleted file mode 100644index e99959404c..0000000000Binary files a/saleor/static/placeholders/bags/mirumee-bag.jpg and /dev/null differdiff --git a/saleor/static/placeholders/shirts/mirumee-shirt.jpg b/saleor/static/placeholders/shirts/mirumee-shirt.jpgdeleted file mode 100644index e4176433ba..0000000000Binary files a/saleor/static/placeholders/shirts/mirumee-shirt.jpg and /dev/null differdiff --git a/saleor/static/placeholders/shirts/mirumee-shirt2.jpg b/saleor/static/placeholders/shirts/mirumee-shirt2.jpgdeleted file mode 100644index 35ded0f450..0000000000Binary files a/saleor/static/placeholders/shirts/mirumee-shirt2.jpg and /dev/null differdiff --git a/utils/create_random_data.py b/utils/create_random_data.pyindex 7e5020c85c..dba7f7c6fe 100644--- a/utils/create_random_data.py+++ b/utils/create_random_data.py@@ -1,17 +1,31 @@from __future__ import unicode_literals-import osimport random+import osimport unicodedatafrom faker import Factoryfrom django.core.files import File-from saleor.product.models import (Product, ProductVariant, ProductImage, Stock)-from saleor.product.models import Category+from saleor.product.models import (Shirt, ShirtVariant,+ Bag, BagVariant, ProductImage)+from saleor.product.models import Category, Colorfrom saleor.userprofile.models import User, Address+fake = Factory.create()-STOCK_LOCATION = 'default'+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):@@ -24,40 +38,43 @@ def get_or_create_category(name, **kwargs):return Category.objects.get_or_create(name=name, defaults=defaults)[0]-def create_product(**kwargs):+def create_product(product_type, **kwargs):+ if random.choice([True, False]):+ collection = random.choice(PRODUCT_COLLECTIONS)+ else:+ collection = ''+defaults = {'name': fake.company(),'price': fake.pyfloat(2, 2, positive=True),+ 'category': Category.objects.order_by('?')[0],+ 'collection': collection,+ 'color': Color.objects.order_by('?')[0],'weight': fake.random_digit(),'description': '\n\n'.join(fake.paragraphs(5))}defaults.update(kwargs)- return Product.objects.create(**defaults)++ return product_type.objects.create(**defaults)def create_variant(product, **kwargs):defaults = {- 'name': fake.word(),+ 'stock': fake.random_int(),'sku': fake.random_int(1, 100000),- 'product': product,+ '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 ProductVariant.objects.create(**defaults)--def create_stock(product, **kwargs):- for variant in product.variants.all():- _create_stock(variant, **kwargs)---def _create_stock(variant, **kwargs):- defaults = {- 'variant': variant,- 'location': STOCK_LOCATION,- 'quantity': fake.random_int(1, 50)- }- defaults.update(kwargs)- return Stock.objects.create(**defaults)+ return variant_class.objects.create(**defaults)def create_product_image(product, placeholder_dir):@@ -76,22 +93,47 @@ 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):- default_category = get_or_create_category('Default')+ # Create few colors+ [create_color() for i in range(5)]- for i in range(how_many):- product = create_product()- product.categories.add(default_category)+ shirt_category = get_or_create_category('Shirts')+ bag_category = get_or_create_category('Grocery bags')- create_variant(product) # ensure at least one variant- create_product_images(product, random.randrange(1, 5), placeholder_dir)+ create_images = os.path.exists(placeholder_dir)- for _ in range(random.randrange(1, 5)):+ for i in range(how_many):+ # Shirt+ shirt = create_shirt(category=shirt_category)+ if create_images:+ create_product_images(shirt, random.randrange(1, 5),+ placeholder_dir + "shirts")+ # Bag+ bag = create_bag(category=bag_category, collection='')+ if create_images:+ 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])+ continueif random.choice([True, False]):- create_variant(product)+ create_variant(shirt, size=size[0])++ create_variant(bag)- create_stock(product)- yield "Product - %s %s Variants" % (product, product.variants.count())+ yield "Shirt - %s %s Variants" % (shirt, shirt.variants.count())+ yield "Bag - %s %s Variants" % (bag, bag.variants.count())def create_fake_user():