本文整理汇总了Python中shuup_tests.front.fixtures.get_jinja_context函数的典型用法代码示例。如果您正苦于以下问题:Python get_jinja_context函数的具体用法?Python get_jinja_context怎么用?Python get_jinja_context使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_jinja_context函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_pagination_variables
def test_get_pagination_variables():
populate_if_required() # Makes sure there is at least 30 products in db
products = Product.objects.all()[:19]
assert len(products) == 19
vars = {"products": products}
context = get_jinja_context(**vars)
variables = general.get_pagination_variables(context, context["products"], limit=4)
assert variables["page"].number == 1
assert len(variables["objects"]) == 4
context = get_jinja_context(path="/?page=5", **vars)
variables = general.get_pagination_variables(context, context["products"], limit=4)
assert variables["page"].number == 5
assert len(variables["objects"]) == 3
variables = general.get_pagination_variables(context, context["products"], limit=20)
assert not variables["is_paginated"]
assert variables["page"].number == 1
context = get_jinja_context(path="/?page=42", **vars)
variables = general.get_pagination_variables(context, context["products"], limit=4)
assert variables["page"].number == 5
assert len(variables["objects"]) == 3
vars = {"products": []}
context = get_jinja_context(path="/", **vars)
variables = general.get_pagination_variables(context, context["products"], limit=4)
assert not variables["is_paginated"]
示例2: test_category_links_plugin_with_customer
def test_category_links_plugin_with_customer(rf, show_all_categories):
"""
Test plugin for categories that is visible for certain group
"""
shop = get_default_shop()
group = get_default_customer_group()
customer = create_random_person()
customer.groups.add(group)
customer.save()
request = rf.get("/")
request.shop = get_default_shop()
apply_request_middleware(request)
request.customer = customer
category = get_default_category()
category.status = CategoryStatus.VISIBLE
category.visibility = CategoryVisibility.VISIBLE_TO_GROUPS
category.visibility_groups.add(group)
category.shops.add(shop)
category.save()
vars = {"request": request}
context = get_jinja_context(**vars)
plugin = CategoryLinksPlugin({"categories": [category.pk], "show_all_categories": show_all_categories})
assert category.is_visible(customer)
assert category in plugin.get_context_data(context)["categories"]
customer_without_groups = create_random_person()
customer_without_groups.groups.clear()
assert not category.is_visible(customer_without_groups)
request.customer = customer_without_groups
context = get_jinja_context(**vars)
assert category not in plugin.get_context_data(context)["categories"]
示例3: test_cross_sell_plugin_cache_bump
def test_cross_sell_plugin_cache_bump():
shop = get_default_shop()
supplier = get_default_supplier()
product = create_product("test-sku", shop=shop, supplier=supplier)
context = get_jinja_context(product=product)
total_count = 5
trim_count = 3
type = ProductCrossSellType.RELATED
_create_cross_sell_products(product, shop, supplier, type, total_count)
assert ProductCrossSell.objects.filter(product1=product, type=type).count() == total_count
set_cached_value_mock = mock.Mock(wraps=context_cache.set_cached_value)
def set_cache_value(key, value, timeout=None):
if "product_cross_sells" 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 product_helpers.get_product_cross_sells(context, product, type, trim_count)
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 product_helpers.get_product_cross_sells(context, product, type, trim_count)
assert set_cached_value_mock.call_count == 1
# bump caches
ProductCrossSell.objects.filter(product1=product, type=type).first().save()
assert product_helpers.get_product_cross_sells(context, product, type, trim_count)
assert set_cached_value_mock.call_count == 2
示例4: test_best_selling_products_with_multiple_orders
def test_best_selling_products_with_multiple_orders():
context = get_jinja_context()
supplier = get_default_supplier()
shop = get_default_shop()
n_products = 2
price = 10
product_1 = create_product("test-sku-1", supplier=supplier, shop=shop)
product_2 = create_product("test-sku-2", supplier=supplier, shop=shop)
create_order_with_product(product_1, supplier, quantity=1, taxless_base_unit_price=price, shop=shop)
create_order_with_product(product_2, supplier, quantity=1, taxless_base_unit_price=price, shop=shop)
cache.clear()
# Two initial products sold
assert product_1 in general.get_best_selling_products(context, n_products=n_products)
assert product_2 in general.get_best_selling_products(context, n_products=n_products)
product_3 = create_product("test-sku-3", supplier=supplier, shop=shop)
create_order_with_product(product_3, supplier, quantity=2, taxless_base_unit_price=price, shop=shop)
cache.clear()
# Third product sold in greater quantity
assert product_3 in general.get_best_selling_products(context, n_products=n_products)
create_order_with_product(product_1, supplier, quantity=4, taxless_base_unit_price=price, shop=shop)
create_order_with_product(product_2, supplier, quantity=4, taxless_base_unit_price=price, shop=shop)
cache.clear()
# Third product outsold by first two products
assert product_3 not in general.get_best_selling_products(context, n_products=n_products)
示例5: test_get_listed_products_orderable_only
def test_get_listed_products_orderable_only():
context = get_jinja_context()
shop = get_default_shop()
simple_supplier = get_simple_supplier()
n_products = 2
# Create product without stock
product = create_product(
"test-sku",
supplier=simple_supplier,
shop=shop,
stock_behavior=StockBehavior.STOCKED
)
assert len(general.get_listed_products(context, n_products, orderable_only=True)) == 0
assert len(general.get_listed_products(context, n_products, orderable_only=False)) == 1
# Increase stock on product
quantity = product.get_shop_instance(shop).minimum_purchase_quantity
simple_supplier.adjust_stock(product.id, quantity)
assert len(general.get_listed_products(context, n_products, orderable_only=True)) == 1
assert len(general.get_listed_products(context, n_products, orderable_only=False)) == 1
# Decrease stock on product
simple_supplier.adjust_stock(product.id, -quantity)
assert len(general.get_listed_products(context, n_products, orderable_only=True)) == 0
assert len(general.get_listed_products(context, n_products, orderable_only=False)) == 1
示例6: test_get_listed_products_cache_bump
def test_get_listed_products_cache_bump():
supplier = get_default_supplier()
shop = get_default_shop()
product_1 = create_product("test-sku-1", supplier=supplier, shop=shop,)
from shuup.front.template_helpers import general
filter_dict = {"id": product_1.pk}
cache.clear()
context = get_jinja_context()
set_cached_value_mock = mock.Mock(wraps=context_cache.set_cached_value)
def set_cache_value(key, value, timeout=None):
if "listed_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
for cache_test in range(2):
assert general.get_listed_products(context, n_products=2, filter_dict=filter_dict, orderable_only=False)
assert set_cached_value_mock.call_count == 1
# bump cache
product_1.save()
for cache_test in range(2):
assert general.get_listed_products(context, n_products=2, filter_dict=filter_dict, orderable_only=False)
assert set_cached_value_mock.call_count == 2
# use other filters
from django.db.models import Q
for cache_test in range(2):
assert general.get_listed_products(context, n_products=2, extra_filters=Q(translations__name__isnull=False))
assert set_cached_value_mock.call_count == 3
示例7: test_best_selling_products_with_multiple_orders
def test_best_selling_products_with_multiple_orders():
context = get_jinja_context()
supplier = get_default_supplier()
shop = get_default_shop()
n_products = 2
price = 10
product_1 = create_product("test-sku-1", supplier=supplier, shop=shop)
product_2 = create_product("test-sku-2", supplier=supplier, shop=shop)
create_order_with_product(product_1, supplier, quantity=1, taxless_base_unit_price=price, shop=shop)
create_order_with_product(product_2, supplier, quantity=1, taxless_base_unit_price=price, shop=shop)
cache.clear()
# Two initial products sold
assert product_1 in general.get_best_selling_products(context, n_products=n_products)
assert product_2 in general.get_best_selling_products(context, n_products=n_products)
product_3 = create_product("test-sku-3", supplier=supplier, shop=shop)
create_order_with_product(product_3, supplier, quantity=2, taxless_base_unit_price=price, shop=shop)
cache.clear()
# Third product sold in greater quantity
assert product_3 in general.get_best_selling_products(context, n_products=n_products)
create_order_with_product(product_1, supplier, quantity=4, taxless_base_unit_price=price, shop=shop)
create_order_with_product(product_2, supplier, quantity=4, taxless_base_unit_price=price, shop=shop)
cache.clear()
# Third product outsold by first two products
assert product_3 not in general.get_best_selling_products(context, n_products=n_products)
children = [create_product("SimpleVarChild-%d" % x, supplier=supplier, shop=shop) for x in range(5)]
for child in children:
child.link_to_parent(product_3)
create_order_with_product(child, supplier, quantity=1, taxless_base_unit_price=price, shop=shop)
cache.clear()
# Third product now sold in greatest quantity
assert product_3 == general.get_best_selling_products(context, n_products=n_products)[0]
示例8: test_banner_box_plugin
def test_banner_box_plugin():
context = get_jinja_context()
test_carousel = Carousel.objects.create(name="test")
plugin = BannerBoxPlugin(config={"carousel": test_carousel.pk, "title": "Test"})
data = plugin.get_context_data(context)
assert data.get("carousel") == test_carousel
assert data.get("title") == "Test"
示例9: test_social_media_plugin_ordering
def test_social_media_plugin_ordering():
"""
Test that social media plugin ordering works as expected
"""
context = get_jinja_context()
icon_classes = SocialMediaLinksPlugin.icon_classes
link_1_type = "Facebook"
link_1 = {
"url": "http://www.facebook.com",
"ordering": 2,
}
link_2_type = "Twitter"
link_2 = {
"url": "http://www.twitter.com",
"ordering": 1,
}
links = {
link_1_type: link_1,
link_2_type: link_2,
}
plugin = SocialMediaLinksPlugin({"links": links})
assert len(plugin.get_links()) == 2
# Make sure link 2 comes first
assert plugin.get_links()[0][2] == link_2["url"]
示例10: get_context
def get_context(rf, customer=None):
request = rf.get("/")
request.shop = factories.get_default_shop()
apply_request_middleware(request)
if customer:
request.customer = customer
return get_jinja_context(**{"request": request})
示例11: test_get_listed_products_filter
def test_get_listed_products_filter():
context = get_jinja_context()
shop = get_default_shop()
supplier = get_default_supplier()
product_1 = create_product(
"test-sku-1",
supplier=supplier,
shop=shop,
)
product_2 = create_product(
"test-sku-2",
supplier=supplier,
shop=shop,
)
cache.clear()
from shuup.front.template_helpers import general
filter_dict = {"id": product_1.id}
for cache_test in range(2):
product_list = general.get_listed_products(context, n_products=2, filter_dict=filter_dict)
assert product_1 in product_list
assert product_2 not in product_list
for cache_test in range(2):
product_list = general.get_listed_products(context, n_products=2, filter_dict=filter_dict, orderable_only=False)
assert product_1 in product_list
assert product_2 not in product_list
示例12: test_get_random_products_cache_bump
def test_get_random_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 "random_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_random_products(context, n_products=10)
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_random_products(context, n_products=10)
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_random_products(context, n_products=10)
assert set_cached_value_mock.call_count == 2
示例13: test_get_newest_products
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
示例14: test_plugin_renders_absolute_links
def test_plugin_renders_absolute_links():
"""
Test that the plugin renders only absolute links.
"""
context = get_jinja_context()
page = create_page(eternal=True, visible_in_menu=True)
absolute_link = "/%s" % page.url
plugin = PageLinksPlugin({"show_all_pages": True})
assert absolute_link in plugin.render(context)
示例15: test_get_best_selling_products
def test_get_best_selling_products():
from shuup.front.template_helpers import general
context = get_jinja_context()
# No products sold
assert len(list(general.get_best_selling_products(context, n_products=3))) == 0
shop = get_default_shop()
supplier = get_default_supplier()
supplier2 = Supplier.objects.create(name="supplier2", enabled=True, is_approved=True)
supplier3 = Supplier.objects.create(name="supplier3", enabled=True, is_approved=True)
supplier2.shops.add(shop)
supplier3.shops.add(shop)
product1 = create_product("product1", shop, supplier, 10)
product2 = create_product("product2", shop, supplier, 20)
create_order_with_product(product1, supplier, quantity=1, taxless_base_unit_price=10, shop=shop)
create_order_with_product(product2, supplier, quantity=2, taxless_base_unit_price=20, shop=shop)
cache.clear()
# Two products sold
for cache_test in range(2):
best_selling_products = list(general.get_best_selling_products(context, n_products=3))
assert len(best_selling_products) == 2
assert product1 in best_selling_products
assert product2 in best_selling_products
# Make order unorderable
shop_product = product1.get_shop_instance(shop)
shop_product.visibility = ShopProductVisibility.NOT_VISIBLE
shop_product.save()
cache.clear()
for cache_test in range(2):
best_selling_products = list(general.get_best_selling_products(context, n_products=3))
assert len(best_selling_products) == 1
assert product1 not in best_selling_products
assert product2 in best_selling_products
# add a new product with discounted amount
product3 = create_product("product3", supplier=supplier, shop=shop, default_price=30)
create_order_with_product(product3, supplier, quantity=1, taxless_base_unit_price=30, shop=shop)
from shuup.customer_group_pricing.models import CgpDiscount
CgpDiscount.objects.create(
shop=shop,
product=product3,
group=AnonymousContact.get_default_group(),
discount_amount_value=5
)
cache.clear()
for cache_test in range(2):
best_selling_products = list(general.get_best_selling_products(context, n_products=3, orderable_only=True))
assert len(best_selling_products) == 2
assert product1 not in best_selling_products
assert product2 in best_selling_products
assert product3 in best_selling_products