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

        issue3672
diffstat:

 exceptions.py |  12 ++++++++++++
 message.xml   |  13 +++++++++++++
 stock.py      |  29 +++++++++++++++--------------
 tryton.cfg    |   1 +
 4 files changed, 41 insertions(+), 14 deletions(-)

diffs (110 lines):

diff -r 0bb8396d6cab -r f818c00b56d2 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,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 PackWarning(UserWarning):
+    pass
+
+
+class PackingValidationError(UserError):
+    pass
diff -r 0bb8396d6cab -r f818c00b56d2 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,13 @@
+<?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_shipment_not_packed">
+            <field name="text">To create shipping labels for shipment 
"%(shipment)s", you must pack it.</field>
+        </record>
+        <record model="ir.message" id="msg_shipment_without_carrier">
+            <field name="text">The shipment "%(shipment)s" does not have a 
carrier set.</field>
+        </record>
+    </data>
+</tryton>
diff -r 0bb8396d6cab -r f818c00b56d2 stock.py
--- a/stock.py  Mon Oct 01 15:26:47 2018 +0200
+++ b/stock.py  Sat Dec 29 14:20:30 2018 +0100
@@ -1,12 +1,16 @@
 # 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.model import Workflow, ModelView, fields
+from trytond.model.exceptions import AccessError
 from trytond.pool import PoolMeta, Pool
 from trytond.pyson import Eval, Bool, Id
 from trytond.transaction import Transaction
 from trytond.wizard import Wizard, StateTransition
 
+from .exceptions import PackWarning
+
 __all__ = ['PackageType', 'Package', 'ShipmentOut', 'CreateShipping']
 
 
@@ -131,12 +135,6 @@
                         'root_packages'],
                     },
                 })
-        cls._error_messages.update({
-                'shipment_not_packed': ('The shipment "%(shipment)s" must be'
-                    ' packed before creating the shipping labels'),
-                'shipment_without_carrier': ('The shipment "%(shipment)s"'
-                    ' does not have a carrier set'),
-                })
 
     @classmethod
     def __register__(cls, module):
@@ -175,14 +173,17 @@
     @classmethod
     @Workflow.transition('packed')
     def pack(cls, shipments):
+        pool = Pool()
+        Warning = pool.get('res.user.warning')
         super(ShipmentOut, cls).pack(shipments)
         for shipment in shipments:
             if not shipment.carrier:
-                cls.raise_user_warning(
-                    'shipment_out_no_carrier_%s' % shipment.id,
-                    'shipment_without_carrier', {
-                        'shipment': shipment.rec_name,
-                        })
+                name = 'shipment_out_no_carrier_%s' % shipment.id
+                if Warning.check(name):
+                    raise PackWarning(name,
+                        gettext('stock_package_shipping'
+                            '.msg_shipment_without_carrier',
+                            shipment=shipment.rec_name))
 
     @classmethod
     @ModelView.button_action(
@@ -190,9 +191,9 @@
     def create_shipping(cls, shipments):
         for shipment in shipments:
             if shipment.state not in {'packed', 'done'}:
-                cls.raise_user_error('shipment_not_packed', {
-                        'shipment': shipment.rec_name,
-                        })
+                raise AccessError(
+                    gettext('stock_package_shipping.msg_shipment_not_packed',
+                        shipment=shipment.rec_name))
 
 
 # TODO Implement ShipmentInReturn
diff -r 0bb8396d6cab -r f818c00b56d2 tryton.cfg
--- a/tryton.cfg        Mon Oct 01 15:26:47 2018 +0200
+++ b/tryton.cfg        Sat Dec 29 14:20:30 2018 +0100
@@ -13,3 +13,4 @@
 xml:
     carrier.xml
     stock.xml
+    message.xml

Reply via email to