From 1a1dbba305eabb7748db874298be489a98ca0e54 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 | 12 ++++++++++++ message.xml | 10 ++++++++++ sale.py | 24 +++++++++++------------- tests/scenario_sale_stock_quantity.rst | 6 +++--- tryton.cfg | 1 + 5 files changed, 37 insertions(+), 16 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..34b03c1c2f --- /dev/null +++ b/exceptions.py @@ -0,0 +1,12 @@ +# 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 UserError, UserWarning + + +class StockQuantityError(UserError): + pass + + +class StockQuantityWarning(UserWarning): + pass diff --git a/message.xml b/message.xml new file mode 100644 index 0000000000..aeba34634b --- /dev/null +++ b/message.xml @@ -0,0 +1,10 @@ + + + + + + The forcast quantity (%(forecast_quantity)s%(default_uom)s) for line "%(line)s" is lower than its quantity (%(quantity)s%(unit)s). + + + diff --git a/sale.py b/sale.py index 1261817476..55dfda049f 100644 --- a/sale.py +++ b/sale.py @@ -3,11 +3,14 @@ import datetime +from trytond.i18n import gettext from trytond.pool import PoolMeta, Pool from trytond.model import ModelView, Workflow from trytond.transaction import Transaction from trytond.tools import grouped_slice +from .exceptions import StockQuantityError, StockQuantityWarning + __all__ = ['Sale'] @@ -15,16 +18,6 @@ __all__ = ['Sale'] class Sale(metaclass=PoolMeta): __name__ = 'sale.sale' - @classmethod - def __setup__(cls): - super(Sale, cls).__setup__() - cls._error_messages.update({ - 'stock_quantity': ( - 'The forcast quantity ' - '%(forecast_quantity)s%(default_uom)s of ' - '"%(line)s" is lower than %(quantity)s%(unit)s.'), - }) - @classmethod @ModelView.button @Workflow.transition('quotation') @@ -72,6 +65,7 @@ class Sale(metaclass=PoolMeta): User = pool.get('res.user') ModelData = pool.get('ir.model.data') Line = pool.get('sale.line') + Warning = pool.get('res.user.warning') transaction = Transaction() @@ -116,10 +110,14 @@ class Sale(metaclass=PoolMeta): def raise_(line_id, message_values): if not in_group(): - self.raise_user_error('stock_quantity', message_values) + raise StockQuantityError( + gettext('sale_stock_quantity.msg_sale_stock_quantity', + **message_values)) warning_name = 'stock_quantity_warning_%s' % line_id - self.raise_user_warning( - warning_name, 'stock_quantity', message_values) + if Warning.check(warning_name): + raise StockQuantityWarning(warning_name, + gettext('sale_stock_quantity.msg_sale_stock_quantity', + **message_values)) today = Date.today() sale_date = self.sale_date or today diff --git a/tests/scenario_sale_stock_quantity.rst b/tests/scenario_sale_stock_quantity.rst index fb00f76718..cd002988d1 100644 --- a/tests/scenario_sale_stock_quantity.rst +++ b/tests/scenario_sale_stock_quantity.rst @@ -132,7 +132,7 @@ Sale 2 more products with not enough stock:: >>> sale.click('quote') # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... - UserWarning: ... + StockQuantityWarning: ... Clean sales:: @@ -149,7 +149,7 @@ Sale 6 products with not enough stock:: >>> sale.click('quote') # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... - UserWarning: ... + StockQuantityWarning: ... >>> sale.delete() Make an inventory of 3 products in 2 days:: @@ -175,7 +175,7 @@ Sale 4 products with not enough forecast:: >>> sale.click('quote') # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... - UserWarning: ... + StockQuantityWarning: ... >>> sale.delete() Sale 2 products with enough forecast:: diff --git a/tryton.cfg b/tryton.cfg index 7baa8f38cb..7258e54744 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -9,3 +9,4 @@ extras_depend: stock_supply xml: sale.xml + message.xml