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

        issue3672
diffstat:

 exceptions.py |   8 ++++++++
 message.xml   |  16 ++++++++++++++++
 party.py      |  21 +++++++--------------
 tryton.cfg    |   1 +
 work.py       |  30 +++++++++++-------------------
 5 files changed, 43 insertions(+), 33 deletions(-)

diffs (146 lines):

diff -r 1e49ab9cab9e -r d7b720489943 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.model.exceptions import ValidationError
+
+
+class WorkValidationError(ValidationError):
+    pass
diff -r 1e49ab9cab9e -r d7b720489943 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_work_invalid_parent_state">
+            <field name="text">To open work "%(child)s", you must open its 
parent "%(parent)s".</field>
+        </record>
+        <record model="ir.message" id="msg_work_invalid_children_state">
+            <field name="text">To close work "%(parent)s", you must close its 
child "%(child)s".</field>
+        </record>
+        <record model="ir.message" id="msg_erase_party_opened_project">
+            <field name="text">You cannot erase party "%(party)s" while they 
have opened projects with company "%(company)s".</field>
+        </record>
+    </data>
+</tryton>
diff -r 1e49ab9cab9e -r d7b720489943 party.py
--- a/party.py  Thu Oct 25 21:01:10 2018 +0200
+++ b/party.py  Sat Dec 29 14:20:29 2018 +0100
@@ -1,7 +1,10 @@
 # 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 PoolMeta, Pool
 
+from trytond.modules.party.exceptions import EraseError
+
 __all__ = ['PartyReplace', 'PartyErase']
 
 
@@ -18,16 +21,6 @@
 class PartyErase(metaclass=PoolMeta):
     __name__ = 'party.erase'
 
-    @classmethod
-    def __setup__(cls):
-        super(PartyErase, cls).__setup__()
-        cls._error_messages.update({
-                'opened_project': (
-                    'The party "%(party)s" can not be erased '
-                    'because he has opened projects '
-                    'for the company "%(company)s".'),
-                })
-
     def check_erase_company(self, party, company):
         pool = Pool()
         Work = pool.get('project.work')
@@ -38,7 +31,7 @@
                 ('state', '!=', 'done'),
                 ])
         if works:
-            self.raise_user_error('opened_project', {
-                    'party': party.rec_name,
-                    'company': company.rec_name,
-                    })
+            raise EraseError(
+                gettext('project.msg_erase_party_opened_project',
+                    party=party.rec_name,
+                    company=company.rec_name))
diff -r 1e49ab9cab9e -r d7b720489943 tryton.cfg
--- a/tryton.cfg        Thu Oct 25 21:01:10 2018 +0200
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -9,3 +9,4 @@
     project.xml
     work.xml
     timesheet.xml
+    message.xml
diff -r 1e49ab9cab9e -r d7b720489943 work.py
--- a/work.py   Thu Oct 25 21:01:10 2018 +0200
+++ b/work.py   Sat Dec 29 14:20:29 2018 +0100
@@ -5,12 +5,15 @@
 
 from sql import Null
 
+from trytond.i18n import gettext
 from trytond.model import ModelView, ModelSQL, fields, sequence_ordered, tree
 from trytond.pyson import Eval
 from trytond.transaction import Transaction
 from trytond.pool import Pool
 from trytond.tools import reduce_ids, grouped_slice
 
+from .exceptions import WorkValidationError
+
 __all__ = ['Work']
 
 
@@ -196,17 +199,6 @@
             table_project_work.drop_column('work')
 
     @classmethod
-    def __setup__(cls):
-        super(Work, cls).__setup__()
-        cls._error_messages.update({
-                'invalid_parent_state': ('Work "%(child)s" can not be opened '
-                    'because its parent work "%(parent)s" is already done.'),
-                'invalid_children_state': ('Work "%(parent)s" can not be '
-                    'done because its child work "%(child)s" is still '
-                    'opened.'),
-                })
-
-    @classmethod
     def index_set_field(cls, name):
         index = super(Work, cls).index_set_field(name)
         if name in {'timesheet_start_date', 'timesheet_end_date'}:
@@ -222,17 +214,17 @@
     def check_state(self):
         if (self.state == 'opened'
                 and (self.parent and self.parent.state == 'done')):
-            self.raise_user_error('invalid_parent_state', {
-                    'child': self.rec_name,
-                    'parent': self.parent.rec_name,
-                    })
+            raise WorkValidationError(
+                gettext('project.msg_work_invalid_parent_state',
+                    child=self.rec_name,
+                    parent=self.parent.rec_name))
         if self.state == 'done':
             for child in self.children:
                 if child.state == 'opened':
-                    self.raise_user_error('invalid_children_state', {
-                            'parent': self.rec_name,
-                            'child': child.rec_name,
-                            })
+                    raise WorkValidationError(
+                        gettext('project.msg_work_invalid_children_state',
+                            parent=self.rec_name,
+                            child=child.rec_name))
 
     @property
     def effort_hours(self):

Reply via email to