def get_product_availability( product: Product, discounts: Iterable[DiscountInfo] = None, taxes=None, local_currency: str = None, ) -> ProductAvailability: discounted = product.get_price_range(discounts=discounts, taxes=taxes) undiscounted = product.get_price_range(taxes=taxes) discount = _get_total_discount(undiscounted, discounted) price_range_local, discount_local_currency = _get_product_price_range( discounted, undiscounted, local_currency) is_on_sale = product.is_visible and discount is not None return ProductAvailability( available=product.is_available, on_sale=is_on_sale, price_range=discounted, price_range_undiscounted=undiscounted, discount=discount, price_range_local_currency=price_range_local, discount_local_currency=discount_local_currency, )
def test_product_objects_bulk_create_sets_default_minimal_variant_price( product_type, category ): [product1, product2] = Product.objects.bulk_create( [ Product( name="Test product 1", price=Money("10.00", "USD"), category=category, product_type=product_type, is_published=True, ), Product( name="Test product 2", price=Money("10.00", "USD"), minimal_variant_price=Money("20.00", "USD"), category=category, product_type=product_type, is_published=True, ), ] ) assert product1.minimal_variant_price assert product1.price == product1.minimal_variant_price == Money("10", "USD") assert product2.minimal_variant_price assert product2.price != product2.minimal_variant_price assert product2.minimal_variant_price == Money("20", "USD")
def product_list(product_type, category): product_attr = product_type.product_attributes.first() attr_value = product_attr.values.first() attributes = {smart_text(product_attr.pk): smart_text(attr_value.pk)} products = Product.objects.bulk_create([ Product(pk=1486, name='Test product 1', price=Money('10.00', 'USD'), category=category, product_type=product_type, attributes=attributes, is_published=True), Product(pk=1487, name='Test product 2', price=Money('20.00', 'USD'), category=category, product_type=product_type, attributes=attributes, is_published=False), Product(pk=1489, name='Test product 3', price=Money('20.00', 'USD'), category=category, product_type=product_type, attributes=attributes, is_published=True) ]) return products
def product_list(product_type, category): product_attr = product_type.product_attributes.first() attr_value = product_attr.values.first() attributes = {smart_text(product_attr.pk): smart_text(attr_value.pk)} products = Product.objects.bulk_create([ Product( pk=1486, name="Test product 1", price=Money("10.00", "USD"), category=category, product_type=product_type, attributes=attributes, is_published=True, ), Product( pk=1487, name="Test product 2", price=Money("20.00", "USD"), category=category, product_type=product_type, attributes=attributes, is_published=False, ), Product( pk=1489, name="Test product 3", price=Money("20.00", "USD"), category=category, product_type=product_type, attributes=attributes, is_published=True, ), ]) return products
def product_list(product_type, category): product_attr = product_type.product_attributes.first() attr_value = product_attr.values.first() attributes = {smart_text(product_attr.pk): smart_text(attr_value.pk)} products = Product.objects.bulk_create( [ Product( pk=1486, name="Test product 1", price=Money("10.00", "USD"), category=category, product_type=product_type, attributes=attributes, is_published=True, ), Product( pk=1487, name="Test product 2", price=Money("20.00", "USD"), category=category, product_type=product_type, attributes=attributes, is_published=False, ), Product( pk=1489, name="Test product 3", price=Money("20.00", "USD"), category=category, product_type=product_type, attributes=attributes, is_published=True, ), ] ) ProductVariant.objects.bulk_create( [ ProductVariant( product=products[0], sku=str(uuid.uuid4()).replace("-", ""), track_inventory=True, quantity=100, ), ProductVariant( product=products[1], sku=str(uuid.uuid4()).replace("-", ""), track_inventory=True, quantity=100, ), ProductVariant( product=products[2], sku=str(uuid.uuid4()).replace("-", ""), track_inventory=True, quantity=100, ), ] ) return products
def product_list(product_type, category): product_attr = product_type.product_attributes.first() attr_value = product_attr.values.first() products = list( Product.objects.bulk_create([ Product( pk=1486, name="Test product 1", price=Money(10, "USD"), category=category, product_type=product_type, is_published=True, ), Product( pk=1487, name="Test product 2", price=Money(20, "USD"), category=category, product_type=product_type, is_published=False, ), Product( pk=1489, name="Test product 3", price=Money(20, "USD"), category=category, product_type=product_type, is_published=True, ), ])) ProductVariant.objects.bulk_create([ ProductVariant( product=products[0], sku=str(uuid.uuid4()).replace("-", ""), track_inventory=True, quantity=100, ), ProductVariant( product=products[1], sku=str(uuid.uuid4()).replace("-", ""), track_inventory=True, quantity=100, ), ProductVariant( product=products[2], sku=str(uuid.uuid4()).replace("-", ""), track_inventory=True, quantity=100, ), ]) for product in products: associate_attribute_values_to_instance(product, product_attr, attr_value) return products
def categories_for_pagination(product_type): categories = Category.tree.build_tree_nodes({ "id": 1, "name": "Category2", "slug": "cat1", "children": [ { "parent_id": 1, "name": "CategoryCategory1", "slug": "cat_cat1" }, { "parent_id": 1, "name": "CategoryCategory2", "slug": "cat_cat2" }, { "parent_id": 1, "name": "Category1", "slug": "cat2" }, { "parent_id": 1, "name": "Category3", "slug": "cat3" }, ], }) categories = Category.objects.bulk_create(categories) Product.objects.bulk_create([ Product( name="Prod1", slug="prod1", product_type=product_type, price=Money("10.00", "USD"), category=categories[4], ), Product( name="Prod2", slug="prod2", product_type=product_type, price=Money("10.00", "USD"), category=categories[4], ), Product( name="Prod3", slug="prod3", product_type=product_type, price=Money("10.00", "USD"), category=categories[2], ), ]) return categories
def get_product_availability( product: Product, discounts: Iterable[DiscountInfo] = None, country=None, local_currency=None, extensions=None, ) -> ProductAvailability: if not extensions: extensions = get_extensions_manager() discounted_net_range = product.get_price_range(discounts=discounts) undiscounted_net_range = product.get_price_range() discounted = TaxedMoneyRange( start=extensions.apply_taxes_to_product( product, discounted_net_range.start, country ), stop=extensions.apply_taxes_to_product( product, discounted_net_range.stop, country ), ) undiscounted = TaxedMoneyRange( start=extensions.apply_taxes_to_product( product, undiscounted_net_range.start, country ), stop=extensions.apply_taxes_to_product( product, undiscounted_net_range.stop, country ), ) discount = _get_total_discount(undiscounted, discounted) price_range_local, discount_local_currency = _get_product_price_range( discounted, undiscounted, local_currency ) is_on_sale = product.is_visible and discount is not None return ProductAvailability( available=product.is_available, on_sale=is_on_sale, price_range=discounted, price_range_undiscounted=undiscounted, discount=discount, price_range_local_currency=price_range_local, discount_local_currency=discount_local_currency, )
def get_product_availability( product: Product, discounts: Iterable[DiscountInfo] = None, country: Optional[str] = None, local_currency: Optional[str] = None, extensions: Optional["ExtensionsManager"] = None, ) -> ProductAvailability: if not extensions: extensions = get_extensions_manager() discounted_net_range = product.get_price_range(discounts=discounts) undiscounted_net_range = product.get_price_range() discounted = TaxedMoneyRange( start=extensions.apply_taxes_to_product(product, discounted_net_range.start, country), stop=extensions.apply_taxes_to_product(product, discounted_net_range.stop, country), ) undiscounted = TaxedMoneyRange( start=extensions.apply_taxes_to_product(product, undiscounted_net_range.start, country), stop=extensions.apply_taxes_to_product(product, undiscounted_net_range.stop, country), ) discount = _get_total_discount_from_range(undiscounted, discounted) price_range_local, discount_local_currency = _get_product_price_range( discounted, undiscounted, local_currency) is_on_sale = product.is_visible and discount is not None country = country if country is not None else settings.DEFAULT_COUNTRY is_available = product.is_visible and is_product_in_stock(product, country) return ProductAvailability( on_sale=is_on_sale, price_range=discounted, price_range_undiscounted=undiscounted, discount=discount, price_range_local_currency=price_range_local, discount_local_currency=discount_local_currency, )
def get_product_availability( product: Product, discounts: Iterable[DiscountInfo] = None, country: Optional[str] = None, local_currency: Optional[str] = None, plugins: Optional["PluginsManager"] = None, ) -> ProductAvailability: if not plugins: plugins = get_plugins_manager() discounted_net_range = product.get_price_range(discounts=discounts) undiscounted_net_range = product.get_price_range() discounted = TaxedMoneyRange( start=plugins.apply_taxes_to_product(product, discounted_net_range.start, country), stop=plugins.apply_taxes_to_product(product, discounted_net_range.stop, country), ) undiscounted = TaxedMoneyRange( start=plugins.apply_taxes_to_product(product, undiscounted_net_range.start, country), stop=plugins.apply_taxes_to_product(product, undiscounted_net_range.stop, country), ) discount = _get_total_discount_from_range(undiscounted, discounted) price_range_local, discount_local_currency = _get_product_price_range( discounted, undiscounted, local_currency) is_on_sale = product.is_visible and discount is not None return ProductAvailability( on_sale=is_on_sale, price_range=discounted, price_range_undiscounted=undiscounted, discount=discount, price_range_local_currency=price_range_local, discount_local_currency=discount_local_currency, )
def get_product_availability( product: Product, discounts=None, taxes=None, local_currency=None ) -> ProductAvailability: discounted = product.get_price_range(discounts=discounts, taxes=taxes) undiscounted = product.get_price_range(taxes=taxes) discount = _get_total_discount(undiscounted, discounted) price_range_local, discount_local_currency = _get_product_price_range( discounted, undiscounted, local_currency ) is_on_sale = product.is_visible and discount is not None return ProductAvailability( available=product.is_available, on_sale=is_on_sale, price_range=discounted, price_range_undiscounted=undiscounted, discount=discount, price_range_local_currency=price_range_local, discount_local_currency=discount_local_currency, )
def test_home_view_featured_products(client: Client, product_in_stock: Product): url = reverse('home') expected_html = b'<div class="home__featured">' product_in_stock.is_featured = False product_in_stock.save() def _get(): _response = client.get(url) assert _response.status_code == 200 return _response.content assert expected_html not in _get() product_in_stock.is_featured = True product_in_stock.save() assert expected_html in _get()
def handle(self, *args, **options): with open('{}/products.csv'.format(settings.PROJECT_ROOT), encoding='utf-8') as csvfile: reader = csv.DictReader(csvfile) for row in reader: pk = row['id'] sku = row['sku'] print('Trying to add product ({})'.format(sku)) name = row['name'] description = row['description'] price = row['price'] stock_quantity = row['stock'] is_published = MAP_BOOLEANS[row['is_published']] category = row['category'] product_type = row['type'] variant_name = row['variant_name'] variant_price = row['variant_price'] attribute = slugify(row['attribute']) attribute_slug = slugify(attribute) attribute_value = row['attribute_value'] attribute_value_slug = slugify(attribute_value) try: product_var = ProductVariant.objects.get(sku=sku) except ProductVariant.DoesNotExist: product_var = ProductVariant(sku=sku) except ProductVariant.MultipleObjectsReturned: print( 'Revise sku ({}), multiple product variants returned'. format(sku)) continue product_var.quantity = stock_quantity try: product = Product.objects.get(id=pk) except Product.DoesNotExist: product = Product(id=pk) except Product.MultipleObjectsReturned: print('Revise id ({}), multiple product variants returned'. format(pk)) continue try: product_type = ProductType.objects.get(name=product_type) except ProductType.DoesNotExist: print( 'Product type ({}) for product ({}) does not exist, creating...' .format(product_type, name)) product_type = ProductType.objects.create( name=product_type) product.product_type = product_type try: category = Category.objects.get(slug=category) except Category.DoesNotExist: print( 'Category ({}) for product ({}) does not exist, creating...' .format(category, name)) category = Category.objects.create(slug=category, name=category.title()) product.category = category product.name = name product.description = description product.price = price product.is_published = is_published if variant_name: product_var.name = variant_name if variant_price: product_var.price_override = variant_price try: product_attribute = ProductAttribute.objects.get( slug=attribute_slug) except ProductAttribute.DoesNotExist: print( 'Product attribute ({}) for product ({}) does not exist, creating...' .format(attribute, name)) product_attribute = ProductAttribute.objects.create( slug=attribute_slug, name=attribute.title()) try: product_attribute_value = AttributeChoiceValue.objects.get( slug=attribute_value_slug) except AttributeChoiceValue.DoesNotExist: product_attribute_value = AttributeChoiceValue() product_attribute_value.attribute = product_attribute product_attribute_value.slug = attribute_value_slug product_attribute_value.name = attribute_value.title() product_attribute_value.save() product_var_attrs = dict() product_var_attrs[ product_attribute.pk] = product_attribute_value.pk product_var.attributes = product_var_attrs product_var.product = product try: product.save() except Exception: import pdb pdb.set_trace() product_var.save() print('Product added successfully')
def products_for_pagination(product_type, color_attribute, category, warehouse): product_type2 = ProductType.objects.create(name="Apple") products = Product.objects.bulk_create([ Product( name="Product1", slug="prod1", price=Money("10.00", "USD"), category=category, product_type=product_type2, is_published=True, description="desc1", ), Product( name="ProductProduct1", slug="prod_prod1", price=Money("15.00", "USD"), category=category, product_type=product_type, is_published=False, ), Product( name="ProductProduct2", slug="prod_prod2", price=Money("8.00", "USD"), category=category, product_type=product_type2, is_published=True, ), Product( name="Product2", slug="prod2", price=Money("7.00", "USD"), category=category, product_type=product_type, is_published=False, description="desc2", ), Product( name="Product3", slug="prod3", price=Money("15.00", "USD"), category=category, product_type=product_type2, is_published=True, description="desc3", ), ]) product_attrib_values = color_attribute.values.all() associate_attribute_values_to_instance(products[1], color_attribute, product_attrib_values[0]) associate_attribute_values_to_instance(products[3], color_attribute, product_attrib_values[1]) variants = ProductVariant.objects.bulk_create([ ProductVariant( product=products[0], sku=str(uuid.uuid4()).replace("-", ""), track_inventory=True, ), ProductVariant( product=products[2], sku=str(uuid.uuid4()).replace("-", ""), track_inventory=True, ), ProductVariant( product=products[4], sku=str(uuid.uuid4()).replace("-", ""), track_inventory=True, ), ]) Stock.objects.bulk_create([ Stock(warehouse=warehouse, product_variant=variants[0], quantity=100), Stock(warehouse=warehouse, product_variant=variants[1], quantity=0), Stock(warehouse=warehouse, product_variant=variants[2], quantity=0), ]) return products