]> git.ipfire.org Git - thirdparty/python-fints.git/commitdiff
Store amounts as Decimals, not floats, to prevent problems with the maximum length
authorRaphael Michel <mail@raphaelmichel.de>
Fri, 7 Dec 2018 15:45:08 +0000 (16:45 +0100)
committerRaphael Michel <mail@raphaelmichel.de>
Fri, 7 Dec 2018 15:45:08 +0000 (16:45 +0100)
fints/fields.py

index 20600ffb3414d98bc3b4e00351bff7dad7d0ce70..47d61392329e3e672ace5a91c7ab4d896f768031 100644 (file)
@@ -136,11 +136,31 @@ class FloatField(DataElementField):
         return retval
 
 
-class AmountField(FixedLengthMixin, FloatField):
+class AmountField(FixedLengthMixin, DataElementField):
     type = 'wrt'
+    _DOC_TYPE = decimal.Decimal
     _FIXED_LENGTH = [None, None, 15]
     # FIXME Needs test
 
+    def _parse_value(self, value):
+        if isinstance(value, float):
+            return value
+
+        if isinstance(value, decimal.Decimal):
+            return value
+
+        _value = str(value)
+        if not re.match(r'^(?:0|[1-9]\d*),(?:\d*[1-9]|)$', _value):
+            raise TypeError("Only digits and ',' allowed for value of type 'float', no superfluous leading or trailing zeroes allowed: {!r}".format(value))
+
+        return decimal.Decimal(_value.replace(",", "."))
+
+    def _render_value(self, value):
+        retval = str(value)
+        retval = retval.replace('.', ',').rstrip('0')
+        self._check_value_length(retval)
+        return retval
+
 
 class BinaryField(DataElementField):
     type = 'bin'