diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2019-02-08 16:22:53 +0100 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2019-03-29 18:04:38 +0100 |
commit | 5bc40f6cd56b5b3731d705409f31ef6711c8aa25 (patch) | |
tree | 6433907c2322c82147c73c945183cabe1f697901 | |
parent | 87378c6bbbd60e138b51519c68556917a071e9a1 (diff) | |
download | bank-5bc40f6cd56b5b3731d705409f31ef6711c8aa25.tar.gz bank-5bc40f6cd56b5b3731d705409f31ef6711c8aa25.tar.bz2 bank-5bc40f6cd56b5b3731d705409f31ef6711c8aa25.zip |
Addressing #5543.
-rw-r--r-- | talerbank/app/models.py | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/talerbank/app/models.py b/talerbank/app/models.py index 0e9cc47..211ee49 100644 --- a/talerbank/app/models.py +++ b/talerbank/app/models.py @@ -28,6 +28,16 @@ from django.core.exceptions import \ ObjectDoesNotExist from .amount import Amount, BadFormatAmount, CurrencyMismatch +class InvalidAmount(Amount): + def __init__(self, currency): + super(InvalidAmount, self).__init__(currency, value=float('nan'), fraction=float('nan')) + + def stringify(self): + return "Invalid Amount, please report" + def dump(self): + return "Invalid Amount, please report" + +class AmountField(models.Field): ## # Helper function that instantiates a zero-valued @a Amount @@ -75,7 +85,22 @@ class AmountField(models.Field): del args # pacify PEP checkers if value is None: return Amount.parse(settings.TALER_CURRENCY) - return Amount.parse(value) + try: + return Amount.parse(value) + except NumberTooBig: + # Keep the currency right to avoid causing + # exceptions if some operation is attempted + # against this invalid amount. NOTE that the + # value is defined as NaN, so no actual/useful + # amount will ever be generated using this one. + # And more, the NaN value will make it easier + # to scan the database to find these faulty + # amounts. + # We also decide to not raise exception here + # because they would propagate in too many places + # in the code, and it would be too verbose to + # just try-cactch any possible exception situation. + return InvalidAmount(settings.TALER_CURRENCY) ## |