changeset ae6a7719df28 in modules/stock_lot_sled:default
details:
https://hg.tryton.org/modules/stock_lot_sled?cmd=changeset;node=ae6a7719df28
description:
Add ir.message and use custom exceptions
issue3672
diffstat:
exceptions.py | 12 +++++++++++
message.xml | 16 +++++++++++++++
stock.py | 60 ++++++++++++++++++++++------------------------------------
tryton.cfg | 1 +
4 files changed, 52 insertions(+), 37 deletions(-)
diffs (166 lines):
diff -r 9e3cfa22451d -r ae6a7719df28 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,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 LotExpiredError(UserError):
+ pass
+
+
+class LotExpiredWarning(UserWarning):
+ pass
diff -r 9e3cfa22451d -r ae6a7719df28 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,16 @@
+<?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_lot_modify_expiration_date_period_close">
+ <field name="text">You cannot modify expiration dates of lot
"%(lot)s because it is used on move "%(move)s" in a closed period.</field>
+ </record>
+ <record model="ir.message" id="msg_move_lot_expired">
+ <field name="text">You cannot process move "%(move)s" because its
lot "%(lot)s" has expired.</field>
+ </record>
+ <record model="ir.message" id="msg_period_close_sled">
+ <field name="text">You cannot close periods before the expiration
of lot "%(lot)s" (%(date)s).</field>
+ </record>
+ </data>
+</tryton>
diff -r 9e3cfa22451d -r ae6a7719df28 stock.py
--- a/stock.py Mon Oct 01 13:15:58 2018 +0200
+++ b/stock.py Sat Dec 29 14:20:29 2018 +0100
@@ -5,12 +5,17 @@
from sql import Union, Join, Select, Table, Null, As
from sql.conditionals import Greatest
+from trytond.i18n import gettext
from trytond.pool import PoolMeta, Pool
from trytond.model import ModelView, Workflow, ModelSQL, ValueMixin, fields
+from trytond.model.exceptions import AccessError
from trytond.pyson import Eval
from trytond.transaction import Transaction
from trytond.tools import grouped_slice
+from trytond.modules.stock.exceptions import PeriodCloseError
+from .exceptions import LotExpiredError, LotExpiredWarning
+
__all__ = ['Configuration', 'ConfigurationLotShelfLife',
'Lot', 'Move', 'Period']
@@ -66,15 +71,6 @@
fields.Selection(DATE_STATE, 'Expiration State'),
'on_change_with_expiration_state')
- @classmethod
- def __setup__(cls):
- super(Lot, cls).__setup__()
- cls._error_messages.update({
- 'period_closed_expiration_dates': ('You can not modify '
- 'the expiration dates of lot "%(lot)s" because '
- 'it is used on a move "%(move)s" in a closed period'),
- })
-
@fields.depends('product')
def on_change_with_shelf_life_expiration_state(self, name=None):
if self.product:
@@ -138,24 +134,17 @@
]], limit=1)
if moves:
move, = moves
- cls.raise_user_error('period_closed_expiration_dates', {
- 'lot': move.lot.rec_name,
- 'move': move.rec_name,
- })
+ raise AccessError(
+ gettext('stock_lot_sled'
+ '.msg_lot_modify_expiration_date_period_close',
+ lot=move.lot.rec_name,
+ move=move.rec_name))
class Move(metaclass=PoolMeta):
__name__ = 'stock.move'
@classmethod
- def __setup__(cls):
- super(Move, cls).__setup__()
- cls._error_messages.update({
- 'expiration_dates': ('The lot "%(lot)s" '
- 'on move "%(move)s" is expired'),
- })
-
- @classmethod
@ModelView.button
@Workflow.transition('done')
def do(cls, moves):
@@ -191,6 +180,7 @@
Group = pool.get('res.group')
User = pool.get('res.user')
ModelData = pool.get('ir.model.data')
+ Warning = pool.get('res.user.warning')
types = cls.check_expiration_dates_types()
locations = cls.check_expiration_dates_locations()
@@ -219,10 +209,15 @@
'lot': move.lot.rec_name if move.lot else '',
}
if not in_group():
- cls.raise_user_error('expiration_dates', values)
+ raise LotExpiredError(
+ gettext('stock_lot_sled.msg_move_lot_expired',
+ **values))
else:
- cls.raise_user_warning('%s.check_expiration_dates' % move,
- 'expiration_dates', values)
+ warning_name = '%s.check_expiration_dates' % move
+ if Warning.check(warning_name):
+ raise LotExpiredWarning(warning_name,
+ gettext('stock_lot_sled.msg_move_lot_expired',
+ **values))
@classmethod
def compute_quantities_query(cls, location_ids, with_childs=False,
@@ -328,15 +323,6 @@
__name__ = 'stock.period'
@classmethod
- def __setup__(cls):
- super(Period, cls).__setup__()
- cls._error_messages.update({
- 'close_period_sled': ('You can not close a period '
- 'before the Shelf Live Expiration Date "%(date)s" '
- 'of Lot "%(lot)s"'),
- })
-
- @classmethod
@ModelView.button
def close(cls, periods):
pool = Pool()
@@ -367,7 +353,7 @@
lot_id, = lot_id
lot = Lot(lot_id)
lang = Lang.get()
- cls.raise_user_error('close_period_sled', {
- 'date': lang.strftime(lot.shelf_life_expiration_date),
- 'lot': lot.rec_name,
- })
+ raise PeriodCloseError(
+ gettext('stock_lot_sled.msg_period_close_sled',
+ lot=lot.rec_name,
+ date=lang.strftime(lot.shelf_life_expiration_date)))
diff -r 9e3cfa22451d -r ae6a7719df28 tryton.cfg
--- a/tryton.cfg Mon Oct 01 13:15:58 2018 +0200
+++ b/tryton.cfg Sat Dec 29 14:20:29 2018 +0100
@@ -8,3 +8,4 @@
xml:
stock.xml
product.xml
+ message.xml