33 lines
1.5 KiB
Python
33 lines
1.5 KiB
Python
from decimal import Decimal
|
|
|
|
from django.utils.timezone import now
|
|
|
|
from shop.models import Product, ProductAttribute, ProductAttributeValue, ProductPrice, ProductVariant, Tax
|
|
|
|
|
|
class CreateProductsMixin:
|
|
def create_product(
|
|
self, sku='000001', name='Producto 1', description='Descripción', price=Decimal('10.00'), is_shipping=False
|
|
) -> Product:
|
|
tax, created = Tax.objects.get_or_create(code='IVA', value=21)
|
|
product = Product.objects.create(sku=sku, name=name, description=description, is_shipping_method=is_shipping)
|
|
ProductPrice.objects.create(price=price, product=product, date=now(), tax=tax, current=True)
|
|
return product
|
|
|
|
def create_attribute_value(self, attribute_name='Talla', value='M') -> ProductAttributeValue:
|
|
attribute, created = ProductAttribute.objects.get_or_create(name=attribute_name)
|
|
attribute_value, created = ProductAttributeValue.objects.get_or_create(attribute=attribute, value=value)
|
|
return attribute_value
|
|
|
|
def create_product_variant(
|
|
self, product: Product, sku='000001-V1', price=Decimal('10.00'), stock=Decimal('10'), attribute_values=None
|
|
) -> ProductVariant:
|
|
tax, created = Tax.objects.get_or_create(code='IVA', value=21)
|
|
variant = ProductVariant.objects.create(product=product, sku=sku, stock=stock)
|
|
|
|
if attribute_values:
|
|
variant.attribute_values.set(attribute_values)
|
|
|
|
ProductPrice.objects.create(price=price, variant=variant, date=now(), tax=tax, current=True)
|
|
return variant
|