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

        issue3672
diffstat:

 exceptions.py |   8 ++++++++
 message.xml   |  16 ++++++++++++++++
 production.py |  19 ++++++-------------
 work.py       |  27 ++++++++++-----------------
 4 files changed, 40 insertions(+), 30 deletions(-)

diffs (148 lines):

diff -r 71f91e3478a0 -r db2c7f27b3ed 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,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.exceptions import UserError
+
+
+class PickerError(UserError):
+    pass
diff -r 71f91e3478a0 -r db2c7f27b3ed 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_missing_work_center">
+            <field name="text">Could not find a work center of category 
"%(category)s" under "%(parent)s".</field>
+        </record>
+        <record model="ir.message" id="msg_delete_request">
+            <field name="text">To delete work "%(work)s" it must be in the 
"request" or "draft" state.</field>
+        </record>
+        <record model="ir.message" id="msg_do_finished_work">
+            <field name="text">To complete production "%(production)s", work 
"%(work)s" must be finished.</field>
+        </record>
+    </data>
+</tryton>
diff -r 71f91e3478a0 -r db2c7f27b3ed production.py
--- a/production.py     Mon Oct 01 15:28:25 2018 +0200
+++ b/production.py     Sat Dec 29 14:20:29 2018 +0100
@@ -4,8 +4,10 @@
 
 from sql.aggregate import Sum
 
+from trytond.i18n import gettext
 from trytond.pool import PoolMeta, Pool
 from trytond.model import ModelView, Workflow, fields
+from trytond.model.exceptions import AccessError
 from trytond.pyson import Eval, Bool
 from trytond.transaction import Transaction
 
@@ -36,15 +38,6 @@
             ],
         depends=['state', 'company'])
 
-    @classmethod
-    def __setup__(cls):
-        super(Production, cls).__setup__()
-        cls._error_messages.update({
-                'do_finished_work': ('Production "%(production)s" '
-                    'can not be done '
-                    'because work "%(work)s" is not finished.'),
-                })
-
     def get_cost(self, name):
         pool = Pool()
         Work = pool.get('production.work')
@@ -134,8 +127,8 @@
         for production in productions:
             for work in production.works:
                 if work.state != 'finished':
-                    cls.raise_user_error('do_finished_work', {
-                            'production': production.rec_name,
-                            'work': work.rec_name,
-                            })
+                    raise AccessError(
+                        gettext('production_work.msg_do_finished_work',
+                            production=production.rec_name,
+                            work=work.rec_name))
         super(Production, cls).done(productions)
diff -r 71f91e3478a0 -r db2c7f27b3ed work.py
--- a/work.py   Mon Oct 01 15:28:25 2018 +0200
+++ b/work.py   Sat Dec 29 14:20:29 2018 +0100
@@ -5,14 +5,17 @@
 from decimal import Decimal
 from functools import wraps
 
+from trytond.i18n import gettext
 from trytond.model import (
     ModelSQL, ModelView, Workflow, DeactivableMixin, fields, sequence_ordered,
     tree)
+from trytond.model.exceptions import AccessError
 from trytond.pool import Pool
 from trytond.pyson import Eval, If, Bool
 from trytond.transaction import Transaction
 
 from trytond.modules.product import price_digits
+from .exceptions import PickerError
 
 __all__ = ['WorkCenterCategory', 'WorkCenter', 'Work', 'WorkCycle']
 
@@ -64,10 +67,6 @@
     @classmethod
     def __setup__(cls):
         super(WorkCenter, cls).__setup__()
-        cls._error_messages.update({
-                'missing_work_center': ('Could not find a work center '
-                    'of category "%(category)s" under "%(parent)s".'),
-                })
         cls._order.insert(0, ('name', 'ASC'))
 
     @classmethod
@@ -95,10 +94,10 @@
                         ('category', '=', category.id),
                         ])
                 if not work_centers:
-                    cls.raise_user_error('missing_work_center', {
-                            'category': category.rec_name,
-                            'parent': parent.rec_name,
-                            })
+                    raise PickerError(
+                        gettext('production_work.msg_missing_work_center',
+                            category=category.rec_name,
+                            parent=parent.rec_name))
                 cache[key] = work_centers
             return random.choice(cache[key])
         return picker
@@ -146,14 +145,6 @@
             ('done', 'Done'),
             ], 'State', select=True, readonly=True)
 
-    @classmethod
-    def __setup__(cls):
-        super(Work, cls).__setup__()
-        cls._error_messages.update({
-                'delete_request': ('Work "%s" can not be deleted '
-                    'as it is not in state "request"'),
-                })
-
     @fields.depends('operation')
     def on_change_with_work_center_category(self, name=None):
         if self.operation and self.operation.work_center_category:
@@ -218,7 +209,9 @@
     def delete(cls, works):
         for work in works:
             if work.state not in {'request', 'draft'}:
-                cls.raise_user_error('delete_request', work.rec_name)
+                raise AccessError(
+                    gettext('production_work.msg_delete_request',
+                        work=work.rec_name))
         super(Work, cls).delete(works)
 
 

Reply via email to