Source code for shuup.testing.modules.sample_data.factories

import decimal
import os
import random
from datetime import datetime, timedelta

import factory.fuzzy as fuzzy
from django.conf import settings
from PIL import Image
from six import BytesIO

from shuup.core.models import (
    Category,
    CategoryStatus,
    MediaFile,
    PersonContact,
    Product,
    ProductMedia,
    ProductMediaKind,
    SalesUnit,
    ShopProduct,
    ShopProductVisibility,
)
from shuup.testing.factories import get_default_product_type, get_default_supplier, get_default_tax_class
from shuup.testing.modules.sample_data import SAMPLE_IMAGES_BASE_DIR
from shuup.utils.filer import filer_image_from_data


[docs] def create_sample_category(name, description, business_segment, image_file, shop): category = Category.objects.create(name=name, description=description, status=CategoryStatus.VISIBLE) image_file_path = os.path.join(SAMPLE_IMAGES_BASE_DIR, image_file) path = f"ProductCategories/Samples/{business_segment.capitalize()}" filer_image = _filer_image_from_file_path(image_file_path, path) category.image = filer_image category.shops.add(shop) category.save() return category
[docs] def create_sample_product(name, description, business_segment, image_file, shop): product = Product.objects.create( name=name, description=description, type=get_default_product_type(), tax_class=get_default_tax_class(), sales_unit=SalesUnit.objects.first(), sku=fuzzy.FuzzyText(length=10).fuzz(), ) image_file_path = os.path.join(SAMPLE_IMAGES_BASE_DIR, image_file) path = f"ProductImages/Samples/{business_segment.capitalize()}" filer_image = _filer_image_from_file_path(image_file_path, path) media_file = MediaFile.objects.create(file=filer_image) media_file.shops.add(shop) media = ProductMedia.objects.create(product=product, kind=ProductMediaKind.IMAGE, file=filer_image) media.save() media.shops.add(shop) product.primary_image = media product.save() # create the price and round it to the number of decimals of the currency price = shop.create_price(decimal.Decimal(random.random() * random.randrange(0, 500))).as_rounded() sp = ShopProduct.objects.create( product=product, purchasable=True, visibility=ShopProductVisibility.ALWAYS_VISIBLE, default_price_value=price, shop=shop, shop_primary_image=media, ) supplier = get_default_supplier(shop) supplier.stock_managed = False supplier.save() sp.categories.set(shop.categories.all()) sp.suppliers.add(supplier) # configure prices if "shuup.customer_group_pricing" in settings.INSTALLED_APPS: from shuup.customer_group_pricing.models import CgpPrice CgpPrice.objects.create( product=product, price_value=random.randint(15, 340), shop=shop, group=PersonContact.get_default_group(), ) return product
def _filer_image_from_file_path(image_file_path, path): _, full_file_name = os.path.split(image_file_path) file_name, _ = os.path.splitext(full_file_name) image = Image.open(image_file_path) sio = BytesIO() image.save(sio, format="JPEG") return filer_image_from_data( request=None, path=path, file_name=f"{file_name}.jpeg", file_data=sio.getvalue(), sha1=True, )