From 407710cb1d202139529b52b2a39b0eaa0dc71cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 29 Dec 2018 14:20:30 +0100 Subject: [PATCH] Add ir.message and use custom exceptions issue3672 --- exceptions.py | 12 ++++++++++++ message.xml | 24 ++++++++++++++++++++++++ order_point.py | 28 +++++++++------------------- stock.py | 24 ++++++++++++------------ tryton.cfg | 1 + 5 files changed, 58 insertions(+), 31 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..887d396e96 --- /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 UserWarning +from trytond.model.exceptions import ValidationError + + +class SupplyWarning(UserWarning): + pass + + +class OrderPointValidationError(ValidationError): + pass diff --git a/message.xml b/message.xml new file mode 100644 index 0000000000..6ca9f6ac61 --- /dev/null +++ b/message.xml @@ -0,0 +1,24 @@ + + + + + + Only one order point is allowed for each product-location pair. + + + You cannot define for the same product two order points with opposite locations +(from "Storage Location" to "Provisioning Location" and vice versa). + + + You cannot define for the same product two order points with opposite locations +(from "Overflowing Location" to "Provisioning Location" and vice versa). + + + There are some supplier moves that are late. + + + There are some customer moves that are late. + + + diff --git a/order_point.py b/order_point.py index 7d562101fd..717e0684f1 100644 --- a/order_point.py +++ b/order_point.py @@ -2,10 +2,13 @@ # this repository contains the full copyright notices and license terms. from sql import Null +from trytond.i18n import gettext from trytond.model import ModelView, ModelSQL, fields from trytond.pyson import If, Equal, Eval, Not, In from trytond.transaction import Transaction +from .exceptions import OrderPointValidationError + __all__ = ['OrderPoint'] @@ -105,22 +108,6 @@ class OrderPoint(ModelSQL, ModelView): unit_digits = fields.Function(fields.Integer('Unit Digits'), 'get_unit_digits') - @classmethod - def __setup__(cls): - super(OrderPoint, cls).__setup__() - cls._error_messages.update({ - 'unique_op': ('Only one order point is allowed ' - 'for each product-location pair.'), - 'concurrent_provisioning_location_internal_op': ('You can not ' - 'define on the same product two order points with ' - 'opposite locations (from "Storage Location" to ' - '"Provisioning Location" and vice versa).'), - 'concurrent_overflowing_location_internal_op': ('You can not ' - 'define on the same product two order points with ' - 'opposite locations (from "Storage Location" to ' - '"Overflowing Location" and vice versa).'), - }) - @classmethod def __register__(cls, module_name): cursor = Transaction().connection.cursor() @@ -194,8 +181,10 @@ class OrderPoint(ModelSQL, ModelView): ('type', '=', 'internal')] query.append(arg) if query and cls.search(['OR'] + query): - cls.raise_user_error( - 'concurrent_%s_internal_op' % location_name) + raise OrderPointValidationError( + gettext('stock_supply' + '.msg_order_point_concurrent_%s_internal' % + location_name)) @staticmethod def _type2field(type=None): @@ -226,7 +215,8 @@ class OrderPoint(ModelSQL, ModelView): ] query.append(arg) if cls.search(query): - cls.raise_user_error('unique_op') + raise OrderPointValidationError( + gettext('stock_supply.msg_order_point_unique')) def get_rec_name(self, name): return "%s@%s" % (self.product.name, self.location.name) diff --git a/stock.py b/stock.py index b10b2f5c56..d1e4e8b4c5 100644 --- a/stock.py +++ b/stock.py @@ -1,10 +1,13 @@ # 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.i18n import gettext from trytond.pool import Pool from trytond.model import ModelView from trytond.wizard import (Wizard, StateView, StateAction, StateTransition, Button) +from .exceptions import SupplyWarning + __all__ = ['StockSupply', 'StockSupplyStart'] @@ -21,14 +24,6 @@ class StockSupply(Wizard): internal = StateAction('stock.act_shipment_internal_form') purchase = StateAction('purchase_request.act_purchase_request_form') - @classmethod - def __setup__(cls): - super(StockSupply, cls).__setup__() - cls._error_messages.update({ - 'late_supplier_moves': 'There are some late supplier moves.', - 'late_customer_moves': 'There are some late customer moves.', - }) - @classmethod def types(cls): return ['internal', 'purchase'] @@ -46,6 +41,7 @@ class StockSupply(Wizard): Move = pool.get('stock.move') ShipmentInternal = pool.get('stock.shipment.internal') Date = pool.get('ir.date') + Warning = pool.get('res.user.warning') today = Date.today() if Move.search([ ('from_location.type', '=', 'supplier'), @@ -53,16 +49,20 @@ class StockSupply(Wizard): ('state', '=', 'draft'), ('planned_date', '<', today), ], order=[]): - self.raise_user_warning('%s.supplier@%s' % (self.__name__, today), - 'late_supplier_moves') + name = '%s.supplier@%s' % (self.__name__, today) + if Warning.check(name): + raise SupplyWarning(name, + gettext('stock_supply.msg_late_supplier_moves')) if Move.search([ ('from_location.type', '=', 'storage'), ('to_location.type', '=', 'customer'), ('state', '=', 'draft'), ('planned_date', '<', today), ], order=[]): - self.raise_user_warning('%s.customer@%s' % (self.__name__, today), - 'late_customer_moves') + name = '%s..customer@%s' % (self.__name__, today) + if Warning.check(name): + raise SupplyWarning(name, + gettext('stock_supply.msg_late_customer_moves')) first = True created = False diff --git a/tryton.cfg b/tryton.cfg index c7e740821a..8db254379e 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -15,3 +15,4 @@ xml: product.xml location.xml stock.xml + message.xml