Ejemplo n.º 1
0
def test_products_for_category():
    from shuup.front.template_helpers import general

    supplier = get_default_supplier()
    shop = get_default_shop()

    category = get_default_category()
    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)
    ]
    category.shops.add(shop)

    for child in children:
        child.link_to_parent(products[0])

    context = get_jinja_context()

    for product in products:
        product.get_shop_instance(shop).categories.add(category)

    category_products = list(
        general.get_products_for_categories(context, [category]))
    assert len(category_products) == 2
    assert products[0] in category_products
    assert products[1] in category_products
Ejemplo n.º 2
0
    def get_context_data(self, context):
        request = context["request"]
        products = []
        category_id = self.config.get("category")
        count = self.config.get("count", 5)
        cache_timeout = self.config.get("cache_timeout", 0)
        orderable_only = self.config.get("orderable_only", False)

        if request.is_ajax() and category_id:
            products = get_products_for_categories(
                context, [category_id],
                n_products=count,
                orderable_only=orderable_only)

        return {
            "request":
            request,
            "title":
            self.get_translated_value("title"),
            "products":
            products,
            "orderable_only":
            orderable_only,
            "data_url":
            reverse(
                "shuup:xtheme-category-products-highlight",
                kwargs=dict(category_id=category_id,
                            count=count,
                            cache_timeout=cache_timeout),
            ),
        }
Ejemplo n.º 3
0
def test_products_for_category_cache_bump():
    from shuup.front.template_helpers import general

    supplier = get_default_supplier()
    shop = get_default_shop()
    category = get_default_category()
    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)
    ]
    category.shops.add(shop)

    for child in children:
        child.link_to_parent(products[0])

    context = get_jinja_context()

    for product in products:
        product.get_shop_instance(shop).categories.add(category)

    cache.clear()
    set_cached_value_mock = mock.Mock(wraps=context_cache.set_cached_value)

    def set_cache_value(key, value, timeout=None):
        if "products_for_category" 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_products_for_categories(context, [category])
        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_products_for_categories(context, [category])
        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_products_for_categories(context, [category])
        assert set_cached_value_mock.call_count == 2
Ejemplo n.º 4
0
    def get_context_data(self, context):
        products = []
        category_id = self.config.get("category")
        count = self.config.get("count")
        orderable_only = self.config.get("orderable_only", True)

        if category_id:
            products = get_products_for_categories(
                context, [category_id], n_products=count, orderable_only=orderable_only
            )
        return {"request": context["request"], "title": self.get_translated_value("title"), "products": products}
Ejemplo n.º 5
0
 def get_context_data(self, context):
     products = []
     category_id = self.config.get("category")
     count = self.config.get("count")
     category = Category.objects.filter(
         id=category_id).first() if category_id else None
     if category:
         products = get_products_for_categories(context, [category],
                                                n_products=count)
     return {
         "request": context["request"],
         "title": self.get_translated_value("title"),
         "products": products
     }
Ejemplo n.º 6
0
def test_products_for_category_cache_bump():
    from shuup.front.template_helpers import general
    supplier = get_default_supplier()
    shop = get_default_shop()
    category = get_default_category()
    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)]
    category.shops.add(shop)

    for child in children:
        child.link_to_parent(products[0])

    context = get_jinja_context()

    for product in products:
        product.get_shop_instance(shop).categories.add(category)

    cache.clear()
    set_cached_value_mock = mock.Mock(wraps=context_cache.set_cached_value)
    def set_cache_value(key, value, timeout=None):
        if "products_for_category" 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_products_for_categories(context, [category])
        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_products_for_categories(context, [category])
        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_products_for_categories(context, [category])
        assert set_cached_value_mock.call_count == 2
Ejemplo n.º 7
0
    def get_context_data(self, context):
        products = []
        category_id = self.config.get("category")
        count = self.config.get("count")
        orderable_only = self.config.get("orderable_only", True)
        sale_items_only = self.config.get("sale_items_only", False)

        category = Category.objects.filter(id=category_id).first() if category_id else None
        if category:
            products = get_products_for_categories(
                context, [category], n_products=count, orderable_only=orderable_only, sale_items_only=sale_items_only)
        return {
            "request": context["request"],
            "title": self.get_translated_value("title"),
            "products": products
        }
Ejemplo n.º 8
0
    def get_context_data(self, context):
        products = []
        category_id = self.config.get("category")
        count = self.config.get("count")
        orderable_only = self.config.get("orderable_only", True)

        if category_id:
            products = get_products_for_categories(
                context,
                [category_id],
                n_products=count,
                orderable_only=orderable_only
            )
        return {
            "request": context["request"],
            "title": self.get_translated_value("title"),
            "products": products
        }
Ejemplo n.º 9
0
def test_products_for_category():
    from shuup.front.template_helpers import general

    supplier = get_default_supplier()
    shop = get_default_shop()

    category = get_default_category()
    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)]
    category.shops.add(shop)

    for child in children:
        child.link_to_parent(products[0])

    context = get_jinja_context()

    for product in products:
        product.get_shop_instance(shop).categories.add(category)

    category_products = list(general.get_products_for_categories(context, [category]))
    assert len(category_products) == 2
    assert products[0] in category_products
    assert products[1] in category_products