From fff5663ae108eecdc53203928d1c1cada8258e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 8 Apr 2025 09:59:33 +0200 Subject: [PATCH] Add DecimalNull as tools --- modules/product_price_list/price_list.py | 27 ++---------------------- trytond/CHANGELOG | 1 + trytond/doc/ref/tools/decimal.rst | 13 ++++++++++++ trytond/doc/ref/tools/index.rst | 1 + trytond/trytond/tools/decimal_.py | 26 +++++++++++++++++++++++ 5 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 trytond/doc/ref/tools/decimal.rst diff --git a/modules/product_price_list/price_list.py b/modules/product_price_list/price_list.py index f0469178dc..ea23a3da24 100644 --- a/modules/product_price_list/price_list.py +++ b/modules/product_price_list/price_list.py @@ -1,7 +1,6 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. from decimal import Decimal -from functools import total_ordering from simpleeval import simple_eval @@ -11,35 +10,13 @@ from trytond.model import ( sequence_ordered) from trytond.modules.product import round_price 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 .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): __name__ = 'product.price_list' diff --git a/trytond/CHANGELOG b/trytond/CHANGELOG index 42a8013af6..501f45d438 100644 --- a/trytond/CHANGELOG +++ b/trytond/CHANGELOG @@ -1,3 +1,4 @@ +* Add DecimalNull as tools * Add chat backend * Transaction.check_warnings is now a directory mapping the key to the warnings * Store target of field as relation diff --git a/trytond/doc/ref/tools/decimal.rst b/trytond/doc/ref/tools/decimal.rst new file mode 100644 index 0000000000..7b1c966a77 --- /dev/null +++ b/trytond/doc/ref/tools/decimal.rst @@ -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``. diff --git a/trytond/doc/ref/tools/index.rst b/trytond/doc/ref/tools/index.rst index 7eb9f20ed7..c8b887bbd4 100644 --- a/trytond/doc/ref/tools/index.rst +++ b/trytond/doc/ref/tools/index.rst @@ -11,6 +11,7 @@ Tools API reference. barcode chart + decimal email immutabledict logging diff --git a/trytond/trytond/tools/decimal_.py b/trytond/trytond/tools/decimal_.py index c418ae9890..7652c905d3 100644 --- a/trytond/trytond/tools/decimal_.py +++ b/trytond/trytond/tools/decimal_.py @@ -1,5 +1,8 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. + +from decimal import Decimal +from functools import total_ordering from io import BytesIO from tokenize import NAME, NUMBER, OP, STRING, tokenize, untokenize @@ -45,3 +48,26 @@ def decistmt(s): else: result.append((toknum, tokval)) 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)