def test_product_hightlight_plugin(rf, highlight_type,):
    shop = get_default_shop()
    supplier = get_default_supplier()
    p1 = create_product("p1", shop, supplier, "10")
    p2 = create_product("p2", shop, supplier, "20")
    p3 = create_product("p3", shop, supplier, "30")
    p4 = create_product("p4", shop, supplier, "40")

    sp4 = p4.get_shop_instance(shop)
    sp4.purchasable = False
    sp4.save()

    plugin = ProductHighlightPlugin({
        "type": highlight_type,
        "count": 4,
        "cache_timeout": 120
    })
    plugin_context = plugin.get_context_data(get_context(rf, is_ajax=False))
    context_products = plugin_context["products"]

    assert len(context_products) == 0

    plugin_context = plugin.get_context_data(get_context(rf))
    context_data_url = plugin_context["data_url"]
    context_products = plugin_context["products"]
    if highlight_type in [HighlightType.BEST_SELLING.value, "notvalid_type"]:
        assert len(context_products) == 0
    else:
        assert p1 in context_products
        assert p2 in context_products
        assert p3 in context_products
        assert p4 in context_products

        check_expected_product_count(context_data_url, 4)
        check_expected_product_count(context_data_url, 4)  # one for checking it is cached
Beispiel #2
0
def get_product_highlight(request, plugin_type, cutoff_days, count, cache_timeout):
    orderable_only = "orderable_only" in request.GET
    key, html = context_cache.get_cached_value(
        identifier="xtheme_category_proudcts_highlights",
        item=PRODUCT_HIGHLIGHT_CACHE_KEY_PREFIX % {"shop_id": request.shop.pk},
        context=request,
        plugin_type=plugin_type,
        cutoff_days=cutoff_days,
        count=count,
        cache_timeout=cache_timeout,
        orderable_only=orderable_only,
    )
    if html is not None:
        return HttpResponse(html)

    plugin = ProductHighlightPlugin(
        config={
            "type": plugin_type,
            "cutoff_days": int(cutoff_days),
            "count": int(count),
            "cache_timeout": int(cache_timeout),
            "orderable_only": orderable_only,
        }
    )
    html = plugin.render(dict(request=request))
    context_cache.set_cached_value(key, html, int(cache_timeout))
    return HttpResponse(html)