main
Last change
on this file was
42bd667,
checked in by David Fuertes <dfuertes@…>, 4 years ago
|
Historial Limpio
|
-
Property mode set to
100755
|
File size:
970 bytes
|
Line | |
---|
1 | import decimal |
---|
2 | from .._gae import ndb |
---|
3 | |
---|
4 | |
---|
5 | # TODO Needs more testing |
---|
6 | class NDBDecimalProperty(ndb.StringProperty): |
---|
7 | """ |
---|
8 | NDB decimal implementation |
---|
9 | """ |
---|
10 | |
---|
11 | data_type = decimal.Decimal |
---|
12 | |
---|
13 | def __init__(self, precision, scale, **kwargs): |
---|
14 | d = "1." |
---|
15 | for x in range(scale): |
---|
16 | d += "0" |
---|
17 | self.round = decimal.Decimal(d) |
---|
18 | |
---|
19 | def _to_base_type(self, value): |
---|
20 | if value is None or value == "": |
---|
21 | return None |
---|
22 | else: |
---|
23 | return str(value) |
---|
24 | |
---|
25 | def _from_base_type(self, value): |
---|
26 | if value is None or value == "": |
---|
27 | return None |
---|
28 | else: |
---|
29 | return decimal.Decimal(value).quantize(self.round) |
---|
30 | |
---|
31 | def _validate(self, value): |
---|
32 | if value is None or isinstance(value, decimal.Decimal): |
---|
33 | return value |
---|
34 | elif isinstance(value, basestring): |
---|
35 | return decimal.Decimal(value) |
---|
36 | raise TypeError("Property %s must be a Decimal or string." % self._name) |
---|
Note: See
TracBrowser
for help on using the repository browser.