Add test for money filter + streamline rounding error protection logic (#3714)

* add test cases

* use rounding protection only for currencies with <2 decimal places

* add more test cases

* use parameterized tests
This commit is contained in:
Mira
2023-11-13 17:37:18 +01:00
committed by GitHub
parent aa1c8ae054
commit 1d5a8a5948
2 changed files with 83 additions and 30 deletions

View File

@@ -48,11 +48,18 @@ def money_filter(value: Decimal, arg='', hide_currency=False):
places = settings.CURRENCY_PLACES.get(arg, 2)
rounded = value.quantize(Decimal('1') / 10 ** places, ROUND_HALF_UP)
if places < 2 and rounded != value:
places = 2
# We display decimal places even if we shouldn't for this currency if rounding
# would make the numbers incorrect. If this branch executes, it's likely a bug in
# pretix, but we won't show wrong numbers!
if hide_currency:
return floatformat(value, 2)
else:
return '{} {}'.format(arg, floatformat(value, 2))
if hide_currency:
return floatformat(value, places)
locale_parts = translation.get_language().split('-', 1)
locale_parts = translation.get_language().split("-", 1)
locale = locale_parts[0]
if len(locale_parts) > 1 and len(locale_parts[1]) == 2:
try:
@@ -61,20 +68,9 @@ def money_filter(value: Decimal, arg='', hide_currency=False):
pass
try:
if rounded != value:
# We display decimal places even if we shouldn't for this currency if rounding
# would make the numbers incorrect. If this branch executes, it's likely a bug in
# pretix, but we won't show wrong numbers!
return '{} {}'.format(
arg,
floatformat(value, 2)
)
return format_currency(value, arg, locale=locale)
except:
return '{} {}'.format(
arg,
floatformat(value, places)
)
return '{} {}'.format(arg, floatformat(value, places))
@register.filter("money_numberfield")