From cd2e5bf7cdaf8cdd5541975baf8c17758ed830a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 29 Dec 2018 14:20:29 +0100 Subject: [PATCH] Add ir.message and use custom exceptions issue3672 --- exceptions.py | 8 ++++++++ message.xml | 10 ++++++++++ price_list.py | 21 ++++++++------------- tryton.cfg | 1 + 4 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 exceptions.py create mode 100644 message.xml diff --git a/exceptions.py b/exceptions.py new file mode 100644 index 0000000000..801fcd7bf7 --- /dev/null +++ b/exceptions.py @@ -0,0 +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 trytond.model.exceptions import ValidationError + + +class FormulaError(ValidationError): + pass diff --git a/message.xml b/message.xml new file mode 100644 index 0000000000..1bd824cc0b --- /dev/null +++ b/message.xml @@ -0,0 +1,10 @@ + + + + + + Invalid formula "%(formula)s" in price list line "%(line)s" with exception "%(exception)s". + + + diff --git a/price_list.py b/price_list.py index 04ed9874d3..54c1b3572f 100644 --- a/price_list.py +++ b/price_list.py @@ -4,6 +4,7 @@ from decimal import Decimal from simpleeval import simple_eval +from trytond.i18n import gettext from trytond.model import ModelView, ModelSQL, MatchMixin, fields, \ sequence_ordered from trytond.tools import decistmt @@ -11,6 +12,8 @@ from trytond.pyson import If, Eval from trytond.transaction import Transaction from trytond.pool import Pool +from .exceptions import FormulaError + __all__ = ['PriceList', 'PriceListLine'] @@ -103,14 +106,6 @@ class PriceListLine(sequence_ordered(), ModelSQL, ModelView, MatchMixin): help=('Python expression that will be evaluated with:\n' '- unit_price: the original unit_price')) - @classmethod - def __setup__(cls): - super(PriceListLine, cls).__setup__() - cls._error_messages.update({ - 'invalid_formula': ('Invalid formula "%(formula)s" in price ' - 'list line "%(line)s" with exception "%(exception)s".'), - }) - @staticmethod def default_formula(): return 'unit_price' @@ -137,11 +132,11 @@ class PriceListLine(sequence_ordered(), ModelSQL, ModelView, MatchMixin): if not isinstance(self.get_unit_price(**context), Decimal): raise ValueError except Exception as exception: - self.raise_user_error('invalid_formula', { - 'formula': self.formula, - 'line': self.rec_name, - 'exception': exception, - }) + raise FormulaError( + gettext('product_price_list.msg_invalid_format', + formula=self.formula, + line=self.rec_name, + exception=exception)) from exception def match(self, pattern): if 'quantity' in pattern: diff --git a/tryton.cfg b/tryton.cfg index f5a75fa5da..7ce278f100 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -6,3 +6,4 @@ depends: product xml: price_list.xml + message.xml