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))
def __call__(self, context, item, quantity=1, include_taxes=None, allow_cache=True): 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) if allow_cache else None if not price_info: price_info = _get_priceful(request, item, quantity) 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) return money(getattr(price_info, self.property_name))
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
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_item_price_info(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))
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)
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))
def __call__(self, context, item, quantity=1, allow_cache=True): request = context.get('request') price_info = get_cached_price_info(request, item, quantity) if allow_cache else None if not price_info: price_info = _get_priceful(request, item, quantity) if not price_info: return "" if allow_cache: cache_price_info(request, item, quantity, price_info) return percent(getattr(price_info, self.property_name))
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 test_bump_caches_signal(rf): """ Test that prices are bumped when discount objects changes """ initial_price = 10 discounted_price = 5 shop1 = factories.get_default_shop() shop2 = factories.get_shop(identifier="shop2", domain="shop2") product1 = factories.create_product( "product", shop=shop1, supplier=factories.get_default_supplier(), default_price=initial_price) product2 = factories.create_product( "product2", shop=shop2, supplier=factories.get_default_supplier(), default_price=20) now = datetime(2018, 1, 1, 9, 0, tzinfo=pytz.UTC) # 01/01/2018 09:00 AM with patch("django.utils.timezone.now", new=lambda: now): discount = Discount.objects.create( name="discount", active=True, start_datetime=now - timedelta(days=10), end_datetime=now + timedelta(days=10), discounted_price_value=discounted_price, shop=shop1, ) request = apply_request_middleware(rf.get("/")) request_shop2 = apply_request_middleware( rf.get("/", HTTP_HOST=shop2.domain)) 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) def assert_product1_is_not_cached(): assert get_cached_price_info(request, product1) is None def assert_product2_is_cached(): assert get_cached_price_info(request_shop2, product2) is not None assert_product1_is_not_cached() assert_cache_product1(True) # cache bumped - the cache should be dropped - then, cache again discount.save() assert_product1_is_not_cached() assert_cache_product1(True) # cache product 2.. from now on, shop2 cache should never be bumped cache_price_info(request_shop2, product2, 1, product2.get_price_info(request_shop2)) assert_product2_is_cached() discount.product = product1 discount.save() assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() happy_hour = HappyHour.objects.create(name="hh 1", shop=shop1) happy_hour.discounts.add(discount) assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() happy_hour.save() assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() time_range = TimeRange.objects.create( happy_hour=happy_hour, from_hour=(now - timedelta(hours=1)).time(), to_hour=(now + timedelta(hours=1)).time(), weekday=now.weekday(), ) assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() time_range.save() assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() time_range.delete() assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() with pytest.raises(DiscountM2MChangeError): handle_generic_m2m_changed("test", time_range)
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)
def test_bump_caches_signal(rf): """ Test that prices are bumped when discount objects changes """ initial_price = 10 discounted_price = 5 shop1 = factories.get_default_shop() shop2 = factories.get_shop(identifier="shop2", domain="shop2") product1 = factories.create_product( "product", shop=shop1, supplier=factories.get_default_supplier(), default_price=initial_price ) product2 = factories.create_product( "product2", shop=shop2, supplier=factories.get_default_supplier(), default_price=20 ) now = datetime(2018, 1, 1, 9, 0, tzinfo=pytz.UTC) # 01/01/2018 09:00 AM with patch("django.utils.timezone.now", new=lambda: now): discount = Discount.objects.create( name="discount", active=True, start_datetime=now-timedelta(days=10), end_datetime=now+timedelta(days=10), discounted_price_value=discounted_price ) request = apply_request_middleware(rf.get("/")) request_shop2 = apply_request_middleware(rf.get("/", HTTP_HOST=shop2.domain)) 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) def assert_product1_is_not_cached(): assert get_cached_price_info(request, product1) is None def assert_product2_is_cached(): assert get_cached_price_info(request_shop2, product2) is not None assert_product1_is_not_cached() assert_cache_product1() # cache bumped - the cache should be dropped - then, cache again discount.save() assert_product1_is_not_cached() assert_cache_product1() discount.shops.add(shop1) assert_product1_is_not_cached() assert_cache_product1(True) # cache product 2.. from now on, shop2 cache should never be bumped cache_price_info(request_shop2, product2, 1, product2.get_price_info(request_shop2)) assert_product2_is_cached() discount.product = product1 discount.save() assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() availability_exception = AvailabilityException.objects.create( name="ae1", start_datetime=now+timedelta(days=20), end_datetime=now+timedelta(days=30), ) availability_exception.discounts.add(discount) assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() availability_exception.save() assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() happy_hour = HappyHour.objects.create(name="hh 1") happy_hour.discounts.add(discount) assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() happy_hour.save() assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() time_range = TimeRange.objects.create( happy_hour=happy_hour, from_hour=(now - timedelta(hours=1)).time(), to_hour=(now + timedelta(hours=1)).time(), weekday=now.weekday() ) assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() time_range.save() assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() time_range.delete() assert_product1_is_not_cached() assert_cache_product1(True) assert_product2_is_cached() with pytest.raises(DiscountM2MChangeError): handle_generic_m2m_changed("test", time_range)