def test_price_info_cache_bump(rf):
    request, shop, group = initialize_test(rf, True)

    product_one = create_product("Product_1", shop, default_price=150)
    product_two = create_product("Product_2", shop, default_price=250)

    contact = create_customer()
    group2 = ContactGroup.objects.create(name="Group 2", shop=shop)

    cgp_price = CgpPrice.objects.create(product=product_one, shop=shop, group=group, price_value=100)
    cgp_discount = CgpDiscount.objects.create(product=product_two, shop=shop, group=group, discount_amount_value=200)

    spm = get_pricing_module()
    assert isinstance(spm, CustomerGroupPricingModule)
    pricing_context = spm.get_context_from_request(request)

    for function in [
        lambda: cgp_price.save(),
        lambda: cgp_discount.save(),
        lambda: group2.members.add(contact),
        lambda: cgp_price.delete(),
        lambda: cgp_discount.delete()
    ]:
        cache_price_info(pricing_context, product_one, 1, product_one.get_price_info(pricing_context))
        cache_price_info(pricing_context, product_two, 1, product_two.get_price_info(pricing_context))

        # prices are cached
        assert get_cached_price_info(pricing_context, product_one)
        assert get_cached_price_info(pricing_context, product_two)

        # caches should be bumped
        function()
        assert get_cached_price_info(pricing_context, product_one) is None
        assert get_cached_price_info(pricing_context, product_two) is None
Example #2
0
    def __call__(self, context, item, quantity=1, include_taxes=None, allow_cache=True, supplier=None):
        options = PriceDisplayOptions.from_context(context)
        if options.hide_prices:
            return ""

        if include_taxes is None:
            include_taxes = options.include_taxes

        request = context.get('request')
        price_info = get_cached_price_info(
            request,
            item,
            quantity,
            include_taxes=include_taxes,
            supplier=supplier
        ) if allow_cache else None

        if not price_info:
            price_info = _get_priceful(request, item, quantity, supplier)

            if not price_info:
                return ""

            price_info = convert_taxness(request, item, price_info, include_taxes)
            if allow_cache:
                cache_price_info(request, item, quantity, price_info, include_taxes=include_taxes, supplier=supplier)

        return money(getattr(price_info, self.property_name))
Example #3
0
    def __call__(self, context, item, quantity=1, allow_cache=True, supplier=None):
        request = context.get('request')
        price_info = get_cached_price_info(request, item, quantity, supplier=supplier) if allow_cache else None

        if not price_info:
            price_info = _get_priceful(request, item, quantity, supplier)

            if not price_info:
                return ""
            if allow_cache:
                cache_price_info(request, item, quantity, price_info, supplier=supplier)

        return percent(getattr(price_info, self.property_name))
Example #4
0
def test_happy_hour_prices_expiration(rf):
    with override_settings(
        CACHES={
            'default': {
                'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
                'LOCATION': 'test_happy_hour_prices_bump',
            }
        }
    ):
        happy_hour = init_test()

        # it is now: 2018-01-01 09:00 AM
        before_happy_hour = datetime.datetime(2018, 1, 1, 9, 0, tzinfo=pytz.UTC)    # 09:00 AM
        inside_happy_hour = datetime.datetime(2018, 1, 1, 10, 30, tzinfo=pytz.UTC)  # 10:30 AM
        after_happy_hours = datetime.datetime(2018, 1, 1, 11, 20, tzinfo=pytz.UTC)  # 11:30 AM

        # Create condition from 10am to 11am
        hour_start = datetime.datetime(2018, 1, 1, 10, 0, tzinfo=pytz.UTC).time()  # 10:00 AM
        hour_end = datetime.datetime(2018, 1, 1, 11, 0, tzinfo=pytz.UTC).time()  # 11:00 AM
        set_valid_times_condition(happy_hour, hour_start, hour_end, str(before_happy_hour.weekday()))

        shop = happy_hour.shops.first()
        discount = happy_hour.discounts.first()
        product = discount.product
        shop_product = product.get_shop_instance(shop)
        assert shop_product.default_price_value == 10
        assert discount.discounted_price_value == 6

        def get_request():
            return apply_request_middleware(rf.get("/"))

        price_template = engines["jinja2"].from_string("{{ product|price }}")
        is_discounted_template = engines["jinja2"].from_string("{{ product|is_discounted }}")
        discount_percent_template = engines["jinja2"].from_string("{{ product|discount_percent }}")

        # we start with time being before happy hour
        with patch("django.utils.timezone.now", new=lambda: before_happy_hour):
            # mock also time.time so the cache timeout will be calculated correctly
            with patch("time.time", new=lambda: to_timestamp(before_happy_hour)):
                # check that product price is still the orignal (€10.00)
                # run twice to make sure caches are being used
                for cache_test in range(2):
                    context = dict(product=product, request=get_request())
                    assert price_template.render(context) == format_money(shop_product.default_price)
                    assert is_discounted_template.render(context) == "False"
                    assert discount_percent_template.render(context) == "0%"

                    if cache_test == 1:
                        assert get_cached_price_info(get_request(), product, 1, supplier=shop_product.get_supplier())

        # now we are inside happy hour range
        with patch("django.utils.timezone.now", new=lambda: inside_happy_hour):
            # mock also time.time so the cache timeout will be calculated correctly
            with patch("time.time", new=lambda: to_timestamp(inside_happy_hour)):
                # check that product price is the discounted one (€6.00)
                # run twice to make sure caches are being used
                for cache_test in range(2):
                    context = dict(product=product, request=get_request())
                    assert price_template.render(context) == format_money(
                        shop.create_price(discount.discounted_price_value)
                    )
                    assert is_discounted_template.render(context) == "True"
                    assert discount_percent_template.render(context) == "40%"

                    if cache_test == 1:
                        assert get_cached_price_info(get_request(), product, 1, supplier=shop_product.get_supplier())

                # we change the discounted price from $6 to $7
                # cached should be bumped
                discount.discounted_price_value = 7
                discount.save()
                for cache_test in range(2):
                    context = dict(product=product, request=get_request())
                    assert price_template.render(context) == format_money(
                        shop.create_price(discount.discounted_price_value)
                    )
                    assert is_discounted_template.render(context) == "True"
                    assert discount_percent_template.render(context) == "30%"

                    if cache_test == 1:
                        assert get_cached_price_info(get_request(), product, 1, supplier=shop_product.get_supplier())

        # now we are inside happy hour range
        with patch("django.utils.timezone.now", new=lambda: after_happy_hours):
            # mock also time.time so the cache timeout will be calculated correctly
            with patch("time.time", new=lambda: to_timestamp(after_happy_hours)):
                # check that product price is the orignal (€10.00)
                # run twice to make sure caches are being used
                for cache_test in range(2):
                    context = dict(product=product, request=get_request())
                    assert price_template.render(context) == format_money(shop_product.default_price)
                    assert is_discounted_template.render(context) == "False"
                    assert discount_percent_template.render(context) == "0%"

                    if cache_test == 1:
                        assert get_cached_price_info(get_request(), product, 1, supplier=shop_product.get_supplier())
 def assert_nothing_is_cached():
     # nothing is cached
     assert get_cached_price_info(request, product, 1) is None
 def assert_cache_product():
     cache_price_info(request, product, 1, product.get_price_info(request))
     assert get_cached_price_info(request, product, 1).price == shop.create_price(initial_price)
 def assert_product2_is_cached():
     assert get_cached_price_info(request_shop2, product2) is not None
 def assert_product1_is_not_cached():
     assert get_cached_price_info(request, product1) is None
 def assert_cache_product1(discounted=False):
     cache_price_info(request, product1, 1, product1.get_price_info(request))
     if discounted:
         assert get_cached_price_info(request, product1, 1).price == shop1.create_price(discounted_price)
     else:
         assert get_cached_price_info(request, product1, 1).price == shop1.create_price(initial_price)