changeset 59b299ed2fa2 in modules/stock_lot_unit:default
details: 
https://hg.tryton.org/modules/stock_lot_unit?cmd=changeset;node=59b299ed2fa2
description:
        Add ir.message and use custom exceptions

        issue3672
diffstat:

 exceptions.py                     |   8 +++++
 message.xml                       |  16 ++++++++++
 stock.py                          |  61 +++++++++++++++-----------------------
 tests/scenario_stock_lot_unit.rst |   6 +-
 tryton.cfg                        |   1 +
 5 files changed, 53 insertions(+), 39 deletions(-)

diffs (192 lines):

diff -r a4cc6151c031 -r 59b299ed2fa2 exceptions.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/exceptions.py     Sat Dec 29 14:20:30 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.model.exceptions import ValidationError
+
+
+class LotUnitQuantityError(ValidationError):
+    pass
diff -r a4cc6151c031 -r 59b299ed2fa2 message.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/message.xml       Sat Dec 29 14:20:30 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_change_unit">
+            <field name="text">You cannot change the unit of a stock lot which 
has stock moves.</field>
+        </record>
+        <record model="ir.message" id="msg_change_unit_quantity">
+            <field name="text">You cannot change the quantity of a stock lot 
which has stock moves.</field>
+        </record>
+        <record model="ir.message" id="msg_lot_unit_quantity_greater">
+            <field name="text">You cannot use more than %(quantity)s%(unit)s 
of lot "%(lot)s" in "%(name)s".</field>
+        </record>
+    </data>
+</tryton>
diff -r a4cc6151c031 -r 59b299ed2fa2 stock.py
--- a/stock.py  Wed Oct 31 18:17:35 2018 +0100
+++ b/stock.py  Sat Dec 29 14:20:30 2018 +0100
@@ -2,10 +2,13 @@
 # this repository contains the full copyright notices and license terms.
 from collections import defaultdict
 
+from trytond.i18n import gettext
 from trytond.model import ModelView, Workflow, fields
 from trytond.pool import PoolMeta, Pool
 from trytond.pyson import Eval, Bool
 
+from .exceptions import LotUnitQuantityError
+
 __all__ = [
     'Lot', 'Move', 'Inventory', 'InventoryCount',
     'ShipmentIn', 'ShipmentInReturn',
@@ -43,13 +46,10 @@
     @classmethod
     def __setup__(cls):
         super(Lot, cls).__setup__()
-        cls._error_messages.update({
-                'change_unit': ("You cannot change the unit of a lot "
-                    "which is associated to done moves."),
-                })
         cls._modify_no_move += [
-            ('unit', 'done', 'change_unit'),
-            ('unit_quantity', 'done', 'change_unit'),
+            ('unit', 'done', 'stock_lot_unit.msg_change_unit'),
+            ('unit_quantity', 'done',
+                'stock_lot_unit.msg_change_unit_quantity'),
             ]
 
     @fields.depends('product', methods=['on_change_unit'])
@@ -82,15 +82,6 @@
 class Move(metaclass=PoolMeta):
     __name__ = 'stock.move'
 
-    @classmethod
-    def __setup__(cls):
-        super(Move, cls).__setup__()
-        cls._error_messages.update({
-                'lot_unit_quantity_greater': (
-                    'The quantity in "%(name)s" of the lot "%(lot)s" '
-                    'cannot be greater than %(quantity)s%(unit)s.'),
-                })
-
     def check_lot(self):
         pool = Pool()
         UoM = pool.get('product.uom')
@@ -102,12 +93,12 @@
                 self.uom, self.quantity,
                 self.lot.unit, round=False)
             if quantity > self.lot.unit_quantity:
-                self.raise_user_error('lot_unit_quantity_greater', {
-                        'lot': self.lot.rec_name,
-                        'name': self.rec_name,
-                        'quantity': self.lot.unit_quantity,
-                        'unit': self.lot.unit.symbol,
-                        })
+                raise LotUnitQuantityError(
+                    gettext('stock_lot_unit.msg_lot_unit_quantity_greater',
+                        quantity=self.lot.unit_quantity,
+                        unit=self.lot.unit.symbol,
+                        lot=self.lot.rec_name,
+                        name=self.rec_name))
 
 
 class Inventory(metaclass=PoolMeta):
@@ -117,18 +108,16 @@
     @ModelView.button
     @Workflow.transition('done')
     def confirm(cls, inventories):
-        pool = Pool()
-        Move = pool.get('stock.move')
         for inventory in inventories:
             for line in inventory.lines:
                 if (line.lot and line.lot.unit
                         and line.quantity > line.lot.unit_quantity):
-                    Move.raise_user_error('lot_unit_quantity_greater', {
-                            'lot': line.lot.rec_name,
-                            'name': line.rec_name,
-                            'quantity': line.lot.unit_quantity,
-                            'unit': line.lot.unit.symbol,
-                            })
+                    raise LotUnitQuantityError(
+                        gettext('stock_lot_unit.msg_lot_unit_quantity_greater',
+                            quantity=line.lot.unit_quantity,
+                            unit=line.lot.unit.symbol,
+                            lot=line.lot.rec_name,
+                            name=line.rec_name))
         super(Inventory, cls).confirm(inventories)
 
 
@@ -154,7 +143,6 @@
     @classmethod
     def validate(cls, shipments):
         pool = Pool()
-        Move = pool.get('stock.move')
         UoM = pool.get('product.uom')
 
         super(LotUnitMixin, cls).validate(shipments)
@@ -172,12 +160,13 @@
                         move.lot.unit, round=False)
                 for lot, quantity in lot_quantities.items():
                     if quantity > lot.unit_quantity:
-                        Move.raise_user_error('lot_unit_quantity_greater', {
-                                'lot': lot.rec_name,
-                                'name': shipment.rec_name,
-                                'quantity': lot.unit_quantity,
-                                'unit': lot.unit.symbol,
-                                })
+                        raise LotUnitQuantityError(
+                            gettext('stock_lot_unit'
+                                '.msg_lot_unit_quantity_greater',
+                                quantity=lot.unit_quantity,
+                                unit=lot.unit.symbol,
+                                lot=lot.rec_name,
+                                name=shipment.rec_name))
 
 
 class ShipmentIn(LotUnitMixin, metaclass=PoolMeta):
diff -r a4cc6151c031 -r 59b299ed2fa2 tests/scenario_stock_lot_unit.rst
--- a/tests/scenario_stock_lot_unit.rst Wed Oct 31 18:17:35 2018 +0100
+++ b/tests/scenario_stock_lot_unit.rst Sat Dec 29 14:20:30 2018 +0100
@@ -128,7 +128,7 @@
     >>> shipment.click('done')  # doctest: +IGNORE_EXCEPTION_DETAIL
     Traceback (most recent call last):
         ...
-    UserError: ...
+    LotUnitQuantityError: ...
 
 Now let's ship one move with a quantity bigger than lot unit quantity::
 
@@ -151,7 +151,7 @@
     >>> shipment.click('done')  # doctest: +IGNORE_EXCEPTION_DETAIL
     Traceback (most recent call last):
         ...
-    UserError: ...
+    LotUnitQuantityError: ...
 
 Make an inventory::
 
@@ -170,7 +170,7 @@
     >>> inventory.click('confirm')  # doctest: +IGNORE_EXCEPTION_DETAIL
     Traceback (most recent call last):
         ...
-    UserError: ...
+    LotUnitQuantityError: ...
 
     >>> line, = inventory.lines
     >>> line.quantity = 2
diff -r a4cc6151c031 -r 59b299ed2fa2 tryton.cfg
--- a/tryton.cfg        Wed Oct 31 18:17:35 2018 +0100
+++ b/tryton.cfg        Sat Dec 29 14:20:30 2018 +0100
@@ -11,3 +11,4 @@
 xml:
     product.xml
     stock.xml
+    message.xml

Reply via email to