Add DecimalNull as tools

This commit is contained in:
Cédric Krier
2025-04-08 09:59:33 +02:00
parent 88b9c02577
commit fff5663ae1
5 changed files with 43 additions and 25 deletions
+2 -25
View File
@@ -1,7 +1,6 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of # This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms. # this repository contains the full copyright notices and license terms.
from decimal import Decimal from decimal import Decimal
from functools import total_ordering
from simpleeval import simple_eval from simpleeval import simple_eval
@@ -11,35 +10,13 @@ from trytond.model import (
sequence_ordered) sequence_ordered)
from trytond.modules.product import round_price from trytond.modules.product import round_price
from trytond.pool import Pool from trytond.pool import Pool
from trytond.tools import decistmt from trytond.tools.decimal_ import DecimalNull as Null
from trytond.tools.decimal_ import decistmt
from trytond.transaction import Transaction from trytond.transaction import Transaction
from .exceptions import FormulaError from .exceptions import FormulaError
@total_ordering
class Null(Decimal):
def __eq__(self, other):
if isinstance(other, Null) or other is None:
return True
return False
def __lt__(self, other):
return 0 < other
def _return_self(self, *args, **kwargs):
return self
_OPERATORS = (
'add sub mul matmul truediv floordiv mod divmod pow lshift rshift and xor '
'or'.split())
for op in _OPERATORS:
setattr(Null, '__%s__' % op, _return_self)
setattr(Null, '__r%s__' % op, _return_self)
class PriceList(DeactivableMixin, ModelSQL, ModelView): class PriceList(DeactivableMixin, ModelSQL, ModelView):
__name__ = 'product.price_list' __name__ = 'product.price_list'
+1
View File
@@ -1,3 +1,4 @@
* Add DecimalNull as tools
* Add chat backend * Add chat backend
* Transaction.check_warnings is now a directory mapping the key to the warnings * Transaction.check_warnings is now a directory mapping the key to the warnings
* Store target of field as relation * Store target of field as relation
+13
View File
@@ -0,0 +1,13 @@
.. _ref-tools-decimal:
.. module:: trytond.tools.decimal_
decimal_
========
.. function:: decistmt(string)
Substitute Decimals for floats and integers in the string statements.
.. class:: DecimalNull
A Decimal that behaves like the SQL ``NULL``.
+1
View File
@@ -11,6 +11,7 @@ Tools API reference.
barcode barcode
chart chart
decimal
email email
immutabledict immutabledict
logging logging
+26
View File
@@ -1,5 +1,8 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of # This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms. # this repository contains the full copyright notices and license terms.
from decimal import Decimal
from functools import total_ordering
from io import BytesIO from io import BytesIO
from tokenize import NAME, NUMBER, OP, STRING, tokenize, untokenize from tokenize import NAME, NUMBER, OP, STRING, tokenize, untokenize
@@ -45,3 +48,26 @@ def decistmt(s):
else: else:
result.append((toknum, tokval)) result.append((toknum, tokval))
return untokenize(result).decode('utf-8') return untokenize(result).decode('utf-8')
@total_ordering
class DecimalNull(Decimal):
def __eq__(self, other):
if isinstance(other, DecimalNull) or other is None:
return True
return False
def __lt__(self, other):
return 0 < other
def _return_self(self, *args, **kwargs):
return self
_OPERATORS = (
'add sub mul matmul truediv floordiv mod divmod pow lshift rshift and xor '
'or'.split())
for op in _OPERATORS:
setattr(DecimalNull, '__%s__' % op, _return_self)
setattr(DecimalNull, '__r%s__' % op, _return_self)