changeset 2ae74fca771a in modules/production_outsourcing:default details: https://hg.tryton.org/modules/production_outsourcing?cmd=changeset;node=2ae74fca771a description: Add ir.message and use custom exceptions
issue3672 diffstat: exceptions.py | 8 ++++++++ message.xml | 10 ++++++++++ production.py | 22 +++++++++++----------- tests/scenario_production_outsourcing.rst | 2 +- tryton.cfg | 1 + 5 files changed, 31 insertions(+), 12 deletions(-) diffs (101 lines): diff -r fead92bb5e3f -r 2ae74fca771a 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.exceptions import UserWarning + + +class PurchaseWarning(UserWarning): + pass diff -r fead92bb5e3f -r 2ae74fca771a 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_pending_purchase_done"> + <field name="text">The productions "%(productions)s" cannot be processed as they have pending purchases.</field> + </record> + </data> +</tryton> diff -r fead92bb5e3f -r 2ae74fca771a production.py --- a/production.py Mon Oct 01 15:27:32 2018 +0200 +++ b/production.py Sat Dec 29 14:20:29 2018 +0100 @@ -4,10 +4,13 @@ from decimal import Decimal from itertools import groupby +from trytond.i18n import gettext from trytond.model import ModelView, Workflow, fields from trytond.pool import PoolMeta, Pool from trytond.pyson import Eval, Bool, If +from .exceptions import PurchaseWarning + class Routing(metaclass=PoolMeta): __name__ = 'production.routing' @@ -100,15 +103,6 @@ depends=['company'], help="The lines to add to the production cost.") - @classmethod - def __setup__(cls): - super(Production, cls).__setup__() - cls._error_messages.update({ - 'pending_purchase_done': ( - 'The productions "%s" can not be done ' - 'as they have pending purchases'), - }) - def get_cost(self, name): pool = Pool() Currency = pool.get('currency.currency') @@ -218,6 +212,9 @@ @ModelView.button @Workflow.transition('done') def done(cls, productions): + pool = Pool() + Warning = pool.get('res.user.warning') + def pending_purchase(production): return any(l.purchase_state in {'draft', 'quotation'} for l in production.purchase_lines) @@ -228,6 +225,9 @@ names += '...' warning_name = '%s.pending_purchase.done' % hashlib.md5( str(pendings).encode('utf-8')).hexdigest() - cls.raise_user_warning( - warning_name, 'pending_purchase_done', names) + if Warning.check(warning_name): + raise PurchaseWarning( + warning_name, + gettext('production_outsourcing.msg_pending_purchase_done', + productions=names)) super(Production, cls).done(productions) diff -r fead92bb5e3f -r 2ae74fca771a tests/scenario_production_outsourcing.rst --- a/tests/scenario_production_outsourcing.rst Mon Oct 01 15:27:32 2018 +0200 +++ b/tests/scenario_production_outsourcing.rst Sat Dec 29 14:20:29 2018 +0100 @@ -153,7 +153,7 @@ >>> production.click('done') # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... - UserWarning: ... + PurchaseWarning: ... Validate the purchase:: diff -r fead92bb5e3f -r 2ae74fca771a tryton.cfg --- a/tryton.cfg Mon Oct 01 15:27:32 2018 +0200 +++ b/tryton.cfg Sat Dec 29 14:20:29 2018 +0100 @@ -9,3 +9,4 @@ xml: production.xml purchase.xml + message.xml