示例#1
0
def test_product_selection_plugin(rf):
    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")

    sp1 = p1.get_shop_instance(shop)
    sp2 = p2.get_shop_instance(shop)
    sp3 = p3.get_shop_instance(shop)

    plugin = ProductSelectionPlugin({
        "products": [sp1.pk, sp2.pk, sp3.pk],
        "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"]
    assert p1 in context_products
    assert p2 in context_products
    assert p3 in context_products
    assert p4 not in context_products

    check_expected_product_count(context_data_url, 3)
    check_expected_product_count(context_data_url,
                                 3)  # one for checking it is cached

    # test the plugin form
    with override_current_theme_class(None):
        theme = get_current_theme(get_default_shop())
        cell = LayoutCell(theme,
                          ProductSelectionPlugin.identifier,
                          sizes={"md": 8})
        lcfg = LayoutCellFormGroup(layout_cell=cell,
                                   theme=theme,
                                   request=apply_request_middleware(
                                       rf.get("/")))
        # not valid, products are required
        assert not lcfg.is_valid()

        lcfg = LayoutCellFormGroup(data={
            "general-cell_width": "8",
            "general-cell_align": "pull-right",
            "plugin-products": [p1.pk, p2.pk],
            "plugin-cache_timeout": 120
        },
                                   layout_cell=cell,
                                   theme=theme,
                                   request=apply_request_middleware(
                                       rf.get("/")))
        assert lcfg.is_valid()
        lcfg.save()
        assert cell.config["products"] == [str(p1.pk), str(p2.pk)]
示例#2
0
def get_prouduct_selections_highlight(request, product_ids, cache_timeout):
    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=product_ids,
        cache_timeout=cache_timeout,
    )
    if html is not None:
        return HttpResponse(html)

    plugin = ProductSelectionPlugin(
        config={"products": [prod_id for prod_id in product_ids.split(",")], "cache_timeout": int(cache_timeout)}
    )
    html = plugin.render(dict(request=request))
    context_cache.set_cached_value(key, html, int(cache_timeout))
    return HttpResponse(html)