def test_get_newest_products_cache_bump(): from shuup.front.template_helpers import general supplier = get_default_supplier() shop = get_default_shop() products = [create_product("sku-%d" % x, supplier=supplier, shop=shop) for x in range(2)] children = [create_product("SimpleVarChild-%d" % x, supplier=supplier, shop=shop) for x in range(2)] for child in children: child.link_to_parent(products[0]) context = get_jinja_context() cache.clear() set_cached_value_mock = mock.Mock(wraps=context_cache.set_cached_value) def set_cache_value(key, value, timeout=None): if "newest_products" in key: return set_cached_value_mock(key, value, timeout) with mock.patch.object(context_cache, "set_cached_value", new=set_cache_value): assert set_cached_value_mock.call_count == 0 assert general.get_newest_products(context, 2, orderable_only=False) assert set_cached_value_mock.call_count == 1 # call again, the cache should be returned instead and the set_cached_value shouldn't be called again assert general.get_newest_products(context, 2, orderable_only=False) assert set_cached_value_mock.call_count == 1 # change a shop product, the cache should be bumped ShopProduct.objects.filter(shop=shop).first().save() assert general.get_newest_products(context, 2, orderable_only=False) assert set_cached_value_mock.call_count == 2
def test_get_newest_products(): from shuup.front.template_helpers import general supplier = get_default_supplier() shop = get_default_shop() products = [create_product("sku-%d" % x, supplier=supplier, shop=shop) for x in range(2)] children = [create_product("SimpleVarChild-%d" % x, supplier=supplier, shop=shop) for x in range(2)] for child in children: child.link_to_parent(products[0]) context = get_jinja_context() for cache_test in range(2): newest_products = list(general.get_newest_products(context, n_products=10)) # only 2 products exist assert len(newest_products) == 2 assert products[0] in newest_products assert products[1] in newest_products # Delete one product products[0].soft_delete() for cache_test in range(2): newest_products = list(general.get_newest_products(context, n_products=10)) # only 2 products exist assert len(newest_products) == 1 assert products[0] not in newest_products assert products[1] in newest_products
def test_get_newest_products_sale_only(): from shuup.front.template_helpers import general supplier = get_default_supplier() shop = get_default_shop() product = create_product("sku-1", supplier=supplier, shop=shop, default_price=10) context = get_jinja_context() cache.clear() for cache_test in range(2): assert len(general.get_newest_products(context, n_products=10)) == 1 assert len(general.get_newest_products(context, n_products=10, sale_items_only=True)) == 0 # add a catalog campaign discount for the product from shuup.campaigns.models import CatalogCampaign from shuup.campaigns.models.catalog_filters import ProductFilter from shuup.campaigns.models.product_effects import ProductDiscountAmount campaign = CatalogCampaign.objects.create(shop=shop, public_name="test", name="test", active=True) product_filter = ProductFilter.objects.create() product_filter.products.add(product) campaign.filters.add(product_filter) ProductDiscountAmount.objects.create(campaign=campaign, discount_amount=0.1) cache.clear() for cache_test in range(2): assert len(general.get_newest_products(context, n_products=10, sale_items_only=True)) == 1 assert product in general.get_newest_products(context, n_products=10, sale_items_only=True)
def get_context_data(self, context): request = context["request"] plugin_type = self.config.get("type", HighlightType.NEWEST.value) count = self.config.get("count", 5) cutoff_days = self.config.get("cutoff_days", 30) cache_timeout = self.config.get("cache_timeout", 0) orderable_only = False products = [] if request.is_ajax(): if plugin_type == HighlightType.NEWEST.value: products = get_newest_products(context, count, orderable_only) elif plugin_type == HighlightType.BEST_SELLING.value: products = get_best_selling_products(context, count, cutoff_days, orderable_only) elif plugin_type == HighlightType.RANDOM.value: products = get_random_products(context, count, orderable_only) return { "request": request, "title": self.get_translated_value("title"), "products": products, "data_url": reverse( "shuup:xtheme-product-highlight", kwargs=dict( plugin_type=plugin_type, cutoff_days=cutoff_days, count=count, cache_timeout=cache_timeout ) ), }
def get_context_data(self, context): highlight_type = self.config.get("type", HighlightType.NEWEST.value) count = self.config.get("count", 4) orderable_only = self.config.get("orderable_only", True) sale_items_only = self.config.get("sale_items_only", False) if highlight_type == HighlightType.NEWEST.value: products = get_newest_products(context, count, orderable_only, sale_items_only) elif highlight_type == HighlightType.BEST_SELLING.value: products = get_best_selling_products( context, count, orderable_only=orderable_only, sale_items_only=sale_items_only) elif highlight_type == HighlightType.RANDOM.value: products = get_random_products(context, count, orderable_only, sale_items_only) else: products = [] return { "request": context["request"], "title": self.get_translated_value("title"), "products": products }
def test_get_newest_products(): supplier = get_default_supplier() shop = get_default_shop() products = [create_product("sku-%d" % x, supplier=supplier, shop=shop) for x in range(2)] children = [create_product("SimpleVarChild-%d" % x, supplier=supplier, shop=shop) for x in range(2)] for child in children: child.link_to_parent(products[0]) context = get_jinja_context() # only 2 parent products exist assert len(list(general.get_newest_products(context, n_products=10))) == 2
def get_context_data(self, context): type = self.config.get("type", "newest") count = self.config.get("count", 4) orderable_only = self.config.get("orderable_only", True) if type == "newest": products = get_newest_products(context, count, orderable_only) elif type == "best_selling": products = get_best_selling_products(context, count, orderable_only=orderable_only) elif type == "random": products = get_random_products(context, count, orderable_only) else: products = [] return { "request": context["request"], "title": self.get_translated_value("title"), "products": products }
def get_context_data(self, context): highlight_type = self.config.get("type", HighlightType.NEWEST.value) count = self.config.get("count", 4) orderable_only = self.config.get("orderable_only", True) if highlight_type == HighlightType.NEWEST.value: products = get_newest_products(context, count, orderable_only) elif highlight_type == HighlightType.BEST_SELLING.value: products = get_best_selling_products( context, count, orderable_only=orderable_only, ) elif highlight_type == HighlightType.RANDOM.value: products = get_random_products(context, count, orderable_only) else: products = [] return { "request": context["request"], "title": self.get_translated_value("title"), "products": products }
def test_get_newest_products(): populate_if_required() context = get_jinja_context() assert len(list(general.get_newest_products(context, n_products=4))) == 4
def test_get_newest_products(): populate_if_required() context = get_jinja_context() assert len(list(general.get_newest_products(context, n_products=4))) == 4