def test_money_formatter_digit_grouping(): with translation.override("en-US"): assert money(usd(12345678)) == "$12,345,678.00" with translation.override("fi-FI"): assert money(usd(12345678)) == nbsp("12 345 678,00 $") with translation.override("ar-QA"): assert money(usd(12345678)) == nbsp("US$ 12,345,678.00")
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 test_money_formatter_with_extra_digits(): with translation.override("en-US"): assert money(usd("1234.123456"), widen=0) == "$1,234.12" assert money(usd("1234.123456"), widen=1) == "$1,234.123" assert money(usd("1234.123456"), widen=2) == "$1,234.1235" assert money(usd("1234.123456"), widen=3) == "$1,234.12346" assert money(usd("1234.123456"), widen=4) == "$1,234.123456" with translation.override("fi-FI"): assert money(usd("1234.123456"), widen=0) == nbsp("1 234,12 $") assert money(usd("1234.123456"), widen=1) == nbsp("1 234,123 $") assert money(usd("1234.123456"), widen=2) == nbsp("1 234,1235 $") assert money(usd("1234.123456"), widen=3) == nbsp("1 234,12346 $") assert money(usd("1234.123456"), widen=4) == nbsp("1 234,123456 $")
def test_order_price_display(): shop = get_default_shop() supplier = get_default_supplier() order = _get_order(shop, supplier) # Formatted line prices should match with the formatted order total prices # Strip euro sign before converting formatted price to decimal taxful_total = decimal.Decimal("0.00") taxless_total = decimal.Decimal("0.00") for line in order.lines.all(): taxful_total += decimal.Decimal(money(line.taxful_price).strip("\u20ac")) taxless_total += decimal.Decimal(money(line.taxless_price).strip("\u20ac")) assert decimal.Decimal(money(order.taxful_total_price).strip("\u20ac")) == taxful_total assert decimal.Decimal(money(order.taxless_total_price).strip("\u20ac")) == taxless_total
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 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)
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
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, 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 __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 test_money_formatter_en(): with translation.override("en-US"): assert money(Money("29.99", "USD")) == "$29.99" assert money(Money("29.99", "EUR")) == "€29.99" assert money(Money("29.99", "GBP")) == "£29.99" assert money(Money("29.99", "CAD")) == "CA$29.99" assert money(Money("29.99", "JPY")) == "¥30" # No such thing as a decimal yen! assert money(Money("29.99", "CNY")) == "CN¥29.99" assert money(Money("29.99", "KRW")) == "₩30" # the 1/100 subunit "jeon" is theoretical and not in use assert money(Money("29.99", "SEK")) == "kr29.99"
def test_money_formatter_fi(): with translation.override("fi-FI"): assert money(Money("29.99", "USD")) == nbsp("29,99 $") assert money(Money("29.99", "EUR")) == nbsp("29,99 €") assert money(Money("29.99", "GBP")) == nbsp("29,99 £") assert money(Money("29.99", "CAD")) == nbsp("29,99 CAD") assert money(Money("29.99", "JPY")) == nbsp("30 ¥") # No such thing as a decimal yen! assert money(Money("29.99", "CNY")) == nbsp("29,99 CNY") assert money(Money("29.99", "KRW")) == nbsp("30 KRW") # the 1/100 subunit "jeon" is theoretical and not in use assert money(Money("29.99", "SEK")) == nbsp("29,99 SEK")
def test_money_formatter_default_digit_rounding(): with translation.override("en-US"): assert money(usd("1.234")) == "$1.23" assert money(usd("1.235")) == "$1.24" assert money(usd("1.244")) == "$1.24" assert money(usd("1.245")) == "$1.24" assert money(usd("1.254")) == "$1.25" assert money(usd("1.255")) == "$1.26" assert money(usd("1.111111111111")) == "$1.11"
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, 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 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, 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 test_money_formatter_with_specified_digits(): with translation.override("en-US"): assert money(usd("1234.123456"), digits=0) == "$1,234" assert money(usd("1234.123456"), digits=1) == "$1,234.1" assert money(usd("1234.123456"), digits=3) == "$1,234.123" assert money(usd("1234.123456"), digits=4) == "$1,234.1235" assert money(usd("1234.123456"), digits=5) == "$1,234.12346" assert money(usd("1234.123456"), digits=6) == "$1,234.123456" with translation.override("fi-FI"): assert money(usd("1234.123456"), digits=0) == nbsp("1 234 $") assert money(usd("1234.123456"), digits=1) == nbsp("1 234,1 $") assert money(usd("1234.123456"), digits=3) == nbsp("1 234,123 $") assert money(usd("1234.123456"), digits=4) == nbsp("1 234,1235 $") assert money(usd("1234.123456"), digits=5) == nbsp("1 234,12346 $") assert money(usd("1234.123456"), digits=6) == nbsp("1 234,123456 $")
def test_money_formatter_default_digit_expanding(): with translation.override("en-US"): assert money(usd(0)) == "$0.00" assert money(usd(1)) == "$1.00"