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): key, val = context_cache.get_cached_value( identifier=self.cache_identifier, item=item, context=context.get('request', context), quantity=quantity, include_taxes=include_taxes, name=self.name, allow_cache=allow_cache) if val is not None: return val options = PriceDisplayOptions.from_context(context) if options.hide_prices: context_cache.set_cached_value(key, "") return "" if include_taxes is None: include_taxes = options.include_taxes request = context.get('request') orig_priceful = _get_priceful(request, item, quantity) if not orig_priceful: context_cache.set_cached_value(key, "") return "" priceful = convert_taxness(request, item, orig_priceful, include_taxes) price_value = getattr(priceful, self.property_name) val = money(price_value) context_cache.set_cached_value(key, val) return val
def __call__(self, context, product, quantity=1, allow_cache=True): """ :type product: shuup.core.models.Product """ key, val = context_cache.get_cached_value( identifier=self.cache_identifier, item=product, context=context.get('request', context), quantity=quantity, name=self.name, allow_cache=allow_cache) if val is not None: return val options = PriceDisplayOptions.from_context(context) if options.hide_prices: val = ("", "") context_cache.set_cached_value(key, val) return val request = context.get('request') priced_children = product.get_priced_children(request, quantity) priced_products = priced_children if priced_children else [ (product, _get_priceful(request, product, quantity))] def get_formatted_price(priced_product): (prod, price_info) = priced_product if not price_info: return "" pf = convert_taxness(request, prod, price_info, options.include_taxes) price = money(pf.price) return price min_max = (priced_products[0], priced_products[-1]) prices = tuple(get_formatted_price(x) for x in min_max) context_cache.set_cached_value(key, prices) return prices
def __call__(self, context, item, quantity=1): options = PriceDisplayOptions.from_context(context) if options.hide_prices: return "" request = context.get('request') orig_priceful = _get_priceful(request, item, quantity) if not orig_priceful: return "" priceful = convert_taxness( request, item, orig_priceful, options.include_taxes) price_value = getattr(priceful, self.property_name) return money(price_value)
def __call__(self, context, product, quantity=1, allow_cache=True, supplier=None): """ :type product: shuup.core.models.Product """ options = PriceDisplayOptions.from_context(context) if options.hide_prices: return ("", "") request = context.get('request') priced_products = get_many_cached_price_info( request, product, quantity, include_taxes=options.include_taxes, supplier=supplier ) if allow_cache else None if not priced_products: priced_children_key = PRICED_CHILDREN_CACHE_KEY % (product.id, quantity) priced_products = [] if hasattr(request, priced_children_key): priced_children = getattr(request, priced_children_key) else: priced_children = product.get_priced_children(request, quantity) or [ (product, _get_priceful(request, product, quantity, supplier)) ] setattr(request, priced_children_key, priced_children) for child_product, price_info in priced_children: if not price_info: continue priceful = convert_taxness(request, child_product, price_info, options.include_taxes) priced_products.append(priceful) if priced_products and allow_cache: cache_many_price_info( request, product, quantity, priced_products, include_taxes=options.include_taxes, supplier=supplier) if not priced_products: return ("", "") return (money(priced_products[0].price), money(priced_products[-1].price))
def render_price_property(request, item, priceful, property_name='price'): """ Render price property of a Priceful object. :type request: django.http.HttpRequest :type item: shuup.core.taxing.TaxableItem :type priceful: shuup.core.pricing.Priceful :type propert_name: str :rtype: str """ options = PriceDisplayOptions.from_context({'request': request}) if options.hide_prices: return "" new_priceful = convert_taxness( request, item, priceful, options.include_taxes) price_value = getattr(new_priceful, property_name) return money(price_value)
def render_price_property(request, item, priceful, property_name='price'): """ Render price property of a Priceful object. :type request: django.http.HttpRequest :type item: shuup.core.taxing.TaxableItem :type priceful: shuup.core.pricing.Priceful :type propert_name: str :rtype: str """ options = PriceDisplayOptions.from_context({'request': request}) if options.hide_prices: return "" new_priceful = convert_taxness(request, item, priceful, options.include_taxes) price_value = getattr(new_priceful, property_name) return money(price_value)
def __call__(self, context, product, quantity=1, allow_cache=True): """ :type product: shuup.core.models.Product """ key, val = context_cache.get_cached_value( identifier=self.cache_identifier, item=product, context=context.get('request', context), quantity=quantity, name=self.name, allow_cache=allow_cache) if val is not None: return val options = PriceDisplayOptions.from_context(context) if options.hide_prices: val = ("", "") context_cache.set_cached_value(key, val) return val request = context.get('request') priced_children_key = PRICED_CHILDREN_CACHE_KEY % (product.id, quantity) if hasattr(request, priced_children_key): priced_children = getattr(request, priced_children_key) else: priced_children = product.get_priced_children(request, quantity) setattr(request, priced_children_key, priced_children) priced_products = priced_children if priced_children else [ (product, _get_priceful(request, product, quantity)) ] def get_formatted_price(priced_product): (prod, price_info) = priced_product if not price_info: return "" pf = convert_taxness(request, prod, price_info, options.include_taxes) price = money(pf.price) return price min_max = (priced_products[0], priced_products[-1]) prices = tuple(get_formatted_price(x) for x in min_max) context_cache.set_cached_value(key, prices) return prices
def __call__(self, context, source): """ :type source: shuup.core.order_creator.OrderSource| shuup.core.models.Order """ options = PriceDisplayOptions.from_context(context) if options.hide_prices: return "" try: if options.include_taxes is None: total = source.total_price elif options.include_taxes: total = source.taxful_total_price else: total = source.taxless_total_price except TypeError: total = source.total_price return money(total)
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_item_price_info(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 _get_template_engine_and_context(product_sku='6.0745', create_var_product=False): engine = django.template.engines['jinja2'] assert isinstance(engine, django_jinja.backend.Jinja2) shop = get_default_shop() shop.currency = 'USD' shop.prices_include_tax = False shop.save() request = RequestFactory().get('/') request.shop = shop request.customer = AnonymousContact() request.person = request.customer PriceDisplayOptions(include_taxes=False).set_for_request(request) tax = get_default_tax() create_default_tax_rule(tax) tax_class = get_default_tax_class() order, order_line = _get_order_and_order_line(request) product = create_product(sku=product_sku, shop=shop, tax_class=tax_class) if create_var_product: var_product = create_product(sku="32.9", shop=shop, tax_class=tax_class) child_product_1 = create_product(sku="4.50", shop=shop, tax_class=tax_class, supplier=get_default_supplier()) child_product_2 = create_product(sku="12.00", shop=shop, tax_class=tax_class, supplier=get_default_supplier()) child_product_1.link_to_parent(var_product, variables={"color": "red"}) child_product_2.link_to_parent(var_product, variables={"color": "blue"}) context = { 'request': request, 'prod': product, 'var_prod': var_product if create_var_product else None, # TODO: Test also with variant products 'sline': _get_source_line(request), 'bline': _get_basket_line(request), 'oline': order_line, 'order': order } return (engine, context)
def __call__(self, context, product, quantity=1): """ :type product: shuup.core.models.Product """ options = PriceDisplayOptions.from_context(context) if options.hide_prices: return ("", "") request = context.get('request') priced_children = product.get_priced_children(request, quantity) priced_products = priced_children if priced_children else [ (product, _get_priceful(request, product, quantity))] def get_formatted_price(priced_product): (prod, price_info) = priced_product if not price_info: return "" pf = convert_taxness( request, prod, price_info, options.include_taxes) return money(pf.price) min_max = (priced_products[0], priced_products[-1]) return tuple(get_formatted_price(x) for x in min_max)
def __call__(self, context, product, quantity=1): """ :type product: shuup.core.models.Product """ options = PriceDisplayOptions.from_context(context) if options.hide_prices: return ("", "") request = context.get('request') priced_children = product.get_priced_children(request, quantity) priced_products = priced_children if priced_children else [ (product, _get_priceful(request, product, quantity)) ] def get_formatted_price(priced_product): (prod, price_info) = priced_product if not price_info: return "" pf = convert_taxness(request, prod, price_info, options.include_taxes) return money(pf.price) min_max = (priced_products[0], priced_products[-1]) return tuple(get_formatted_price(x) for x in min_max)
def to_price_display(self): return PriceDisplayOptions( include_taxes=self.show_prices_including_taxes, show_prices=(not self.hide_prices), )
def get_price_display_options(group): options = group.price_display_options.for_group_and_shop(group, shop=group.shop) return options.to_price_display() or PriceDisplayOptions()
def get_price_display_options(self): return get_price_display_options( self) if self.pk else PriceDisplayOptions()
def test_price_display_options_default(): options = PriceDisplayOptions() assert options.show_prices is True assert options.hide_prices is False assert options.include_taxes is None