Don't allow to use float in Numeric fields
The goal is to prevent to introduce float errors in Numeric fields. There is a trick for external protocols like XML-RPC and JSON-RPC where the string representation is stored in the Float object and will be used to convert it into Decimal.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
* Don't allow to use float in Numeric fields
|
||||
* set/get function on Fields take a list of ids
|
||||
* New interface for Function/Property fields
|
||||
Take a Field as first argument instead of many separate arguments
|
||||
|
||||
@@ -79,8 +79,10 @@ class Numeric(Float):
|
||||
def sql_format(value):
|
||||
if not value:
|
||||
value = Decimal('0.0')
|
||||
if isinstance(value, (int, long, float)):
|
||||
if isinstance(value, (int, long)):
|
||||
value = Decimal(repr(value))
|
||||
if isinstance(value, float) and hasattr(value, 'decimal'):
|
||||
value = Decimal(value.decimal)
|
||||
assert isinstance(value, Decimal)
|
||||
return value
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
#this repository contains the full copyright notices and license terms.
|
||||
|
||||
class Float(float):
|
||||
|
||||
def __init__(self, value):
|
||||
super(Float, self).__init__()
|
||||
self.__decimal = value
|
||||
|
||||
@property
|
||||
def decimal(self):
|
||||
return self.__decimal
|
||||
|
||||
def __repr__(self):
|
||||
return self.decimal
|
||||
|
||||
def __str__(self):
|
||||
return self.decimal
|
||||
@@ -3,6 +3,7 @@
|
||||
from trytond.protocols.sslsocket import SSLSocket
|
||||
from trytond.protocols.dispatcher import dispatch
|
||||
from trytond.config import CONFIG
|
||||
from trytond.protocols.datatype import Float
|
||||
import SimpleXMLRPCServer
|
||||
import SimpleHTTPServer
|
||||
import SocketServer
|
||||
@@ -77,7 +78,7 @@ class SimpleJSONRPCDispatcher(SimpleXMLRPCServer.SimpleXMLRPCDispatcher):
|
||||
existing method through subclassing is the prefered means
|
||||
of changing method dispatch behavior.
|
||||
"""
|
||||
rawreq = json.loads(data, object_hook=object_hook)
|
||||
rawreq = json.loads(data, object_hook=object_hook, parse_float=Float)
|
||||
|
||||
req_id = rawreq.get('id', 0)
|
||||
method = rawreq['method']
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from trytond.protocols.sslsocket import SSLSocket
|
||||
from trytond.protocols.dispatcher import dispatch
|
||||
from trytond.config import CONFIG
|
||||
from trytond.protocols.datatype import Float
|
||||
import SimpleXMLRPCServer
|
||||
import SocketServer
|
||||
import xmlrpclib
|
||||
@@ -21,6 +22,11 @@ xmlrpclib.Marshaller.dispatch[Decimal] = \
|
||||
xmlrpclib.Marshaller.dispatch[type(None)] = \
|
||||
lambda self, value, write: self.dump_bool(bool(value), write)
|
||||
|
||||
def _end_double(self, data):
|
||||
self.append(Float(data))
|
||||
self._value = 0
|
||||
xmlrpclib.Unmarshaller.dispatch["double"] = _end_double
|
||||
|
||||
|
||||
class GenericXMLRPCRequestHandler:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user