diff --git a/exceptions.py b/exceptions.py new file mode 100644 index 0000000000..f5c83d762d --- /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.exceptions import UserWarning + + +class PurchaseWarning(UserWarning): + pass diff --git a/message.xml b/message.xml new file mode 100644 index 0000000000..c100533d83 --- /dev/null +++ b/message.xml @@ -0,0 +1,10 @@ + + + + + + The productions "%(productions)s" cannot be processed as they have pending purchases. + + + diff --git a/production.py b/production.py index f281412b3c..d85c7a3f24 100644 --- a/production.py +++ b/production.py @@ -4,10 +4,13 @@ import hashlib 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 @@ class Production(metaclass=PoolMeta): 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 @@ class Production(metaclass=PoolMeta): @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 @@ class Production(metaclass=PoolMeta): 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 --git a/tests/scenario_production_outsourcing.rst b/tests/scenario_production_outsourcing.rst index d8cf6df5b4..fb5ddd7373 100644 --- a/tests/scenario_production_outsourcing.rst +++ b/tests/scenario_production_outsourcing.rst @@ -153,7 +153,7 @@ Try to do the production with pending purchase:: >>> production.click('done') # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... - UserWarning: ... + PurchaseWarning: ... Validate the purchase:: diff --git a/tryton.cfg b/tryton.cfg index 2c7dcb201b..cc1bb79cb1 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -9,3 +9,4 @@ depends: xml: production.xml purchase.xml + message.xml