changeset ac74bbb16f32 in modules/sale_promotion:default details: https://hg.tryton.org/modules/sale_promotion?cmd=changeset;node=ac74bbb16f32 description: Add ir.message and use custom exceptions
issue3672 diffstat: exceptions.py | 8 ++++++++ message.xml | 10 ++++++++++ sale.py | 21 +++++++-------------- tryton.cfg | 1 + 4 files changed, 26 insertions(+), 14 deletions(-) diffs (86 lines): diff -r a8ce0f82372b -r ac74bbb16f32 exceptions.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/exceptions.py Sat Dec 29 14:20:29 2018 +0100 @@ -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 -r a8ce0f82372b -r ac74bbb16f32 message.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/message.xml Sat Dec 29 14:20:29 2018 +0100 @@ -0,0 +1,10 @@ +<?xml version="1.0"?> +<!-- This file is part of Tryton. The COPYRIGHT file at the top level of +this repository contains the full copyright notices and license terms. --> +<tryton> + <data group="1"> + <record model="ir.message" id="msg_invalid_formula"> + <field name="text">Invalid formula "%(formula)s" in promotion "%(promotion)s" 'with exception "%(exception)s".</field> + </record> + </data> +</tryton> diff -r a8ce0f82372b -r ac74bbb16f32 sale.py --- a/sale.py Mon Oct 01 13:40:24 2018 +0200 +++ b/sale.py Sat Dec 29 14:20:29 2018 +0100 @@ -5,6 +5,7 @@ from simpleeval import simple_eval +from trytond.i18n import gettext from trytond.pool import PoolMeta, Pool from trytond.model import ( ModelSQL, ModelView, MatchMixin, Workflow, DeactivableMixin, fields) @@ -13,6 +14,7 @@ from trytond.tools import decistmt from trytond.modules.product import price_digits +from .exceptions import FormulaError __all__ = ['Sale', 'SaleLine', 'SalePromotion', 'SalePromotion_Product', 'SalePromotion_ProductCategory'] @@ -135,15 +137,6 @@ help=('Python expression that will be evaluated with:\n' '- unit_price: the original unit_price')) - @classmethod - def __setup__(cls): - super(SalePromotion, cls).__setup__() - cls._error_messages.update({ - 'invalid_formula': ('Invalid formula "%(formula)s" ' - 'in promotion "%(promotion)s" ' - 'with exception "%(exception)s".'), - }) - @staticmethod def default_company(): return Transaction().context.get('company') @@ -166,11 +159,11 @@ if not isinstance(self.get_unit_price(**context), Decimal): raise ValueError('Not a Decimal') except Exception as exception: - self.raise_user_error('invalid_formula', { - 'formula': self.formula, - 'promotion': self.rec_name, - 'exception': exception, - }) + raise FormulaError( + gettext('sale_promotion.msg_invalid_formula', + formula=self.formula, + promotion=self.rec_name, + exception=exception)) from exception @classmethod def _promotions_domain(cls, sale): diff -r a8ce0f82372b -r ac74bbb16f32 tryton.cfg --- a/tryton.cfg Mon Oct 01 13:40:24 2018 +0200 +++ b/tryton.cfg Sat Dec 29 14:20:29 2018 +0100 @@ -10,3 +10,4 @@ sale_price_list xml: sale.xml + message.xml