random_data.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:planet-b-saleor 作者: planet-b 项目源码 文件源码
def create_products_by_class(product_class, schema,
                             placeholder_dir, how_many=10, create_images=True,
                             stdout=None):
    category_name = schema.get('category') or DEFAULT_CATEGORY
    category = get_or_create_category(category_name)

    for dummy in range(how_many):
        product = create_product(product_class=product_class)
        set_product_attributes(product, product_class)
        product.categories.add(category)
        if create_images:
            class_placeholders = os.path.join(
                placeholder_dir, schema['images_dir'])
            create_product_images(
                product, random.randrange(1, 5), class_placeholders)
        variant_combinations = get_variant_combinations(product)

        prices = get_price_override(
            schema, len(variant_combinations), product.price)
        variants_with_prices = moves.zip_longest(
            variant_combinations, prices)

        for i, variant_price in enumerate(variants_with_prices, start=1337):
            attr_combination, price = variant_price
            sku = '%s-%s' % (product.pk, i)
            create_variant(
                product, attributes=attr_combination, sku=sku,
                price_override=price)

        if not variant_combinations:
            # Create min one variant for products without variant level attrs
            sku = '%s-%s' % (product.pk, fake.random_int(1000, 100000))
            create_variant(product, sku=sku)
        if stdout is not None:
            stdout.write('Product: %s (%s), %s variant(s)' % (
                product, product_class.name, len(variant_combinations) or 1))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号