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

        issue3672
diffstat:

 exceptions.py |  12 ++++++++++++
 line.py       |  15 +++++++--------
 message.xml   |  19 +++++++++++++++++++
 tryton.cfg    |   1 +
 work.py       |  15 +++++++--------
 5 files changed, 46 insertions(+), 16 deletions(-)

diffs (140 lines):

diff -r e7268f76086b -r b064b4f02c25 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.model.exceptions import ValidationError
+
+
+class CompanyValidationError(ValidationError):
+    pass
+
+
+class DurationValidationError(ValidationError):
+    pass
diff -r e7268f76086b -r b064b4f02c25 line.py
--- a/line.py   Mon Oct 01 12:52:17 2018 +0200
+++ b/line.py   Sat Dec 29 14:20:30 2018 +0100
@@ -7,12 +7,15 @@
 from sql.aggregate import Max, Sum
 from sql.functions import Extract, CharLength
 
+from trytond.i18n import gettext
 from trytond.model import ModelView, ModelSQL, fields, Unique
 from trytond.wizard import Wizard, StateView, StateAction, Button
 from trytond.pyson import Eval, PYSONEncoder, Date
 from trytond.transaction import Transaction
 from trytond.pool import Pool
 
+from .exceptions import DurationValidationError
+
 __all__ = ['Line', 'EnterLinesStart', 'EnterLines',
     'HoursEmployee', 'HoursEmployeeContext',
     'HoursEmployeeWeekly', 'HoursEmployeeMonthly']
@@ -64,13 +67,9 @@
         t = cls.__table__()
         cls._sql_constraints = [
             ('uuid_unique', Unique(t, t.uuid),
-                "The UUID of timesheet line must be unique."),
+                'timesheet.msg_line_uuid_unique'),
             ]
         cls._order.insert(0, ('date', 'DESC'))
-        cls._error_messages.update({
-                'duration_positive': (
-                    'Duration of line "%(line)s" must be positive.'),
-                })
 
     @classmethod
     def __register__(cls, module_name):
@@ -134,9 +133,9 @@
 
     def check_duration(self):
         if self.duration < datetime.timedelta():
-            self.raise_user_error('duration_positive', {
-                    'line': self.rec_name,
-                    })
+            raise DurationValidationError(
+                gettext('timesheet.msg_line_duration_positive',
+                    line=self.rec_name))
 
     @property
     def hours(self):
diff -r e7268f76086b -r b064b4f02c25 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,19 @@
+<?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_company_different_origin">
+            <field name="text">The companies associated with work "%(work)s" 
and its origin differ.</field>
+        </record>
+        <record model="ir.message" id="msg_line_duration_positive">
+            <field name="text">The duration of line "%(line)s" must be 
positive.</field>
+        </record>
+        <record model="ir.message" id="msg_work_origin_unique_company">
+            <field name="text">Work origin must be unique by company.</field>
+        </record>
+        <record model="ir.message" id="msg_line_uuid_unique">
+            <field name="text">The UUID of timesheet line must be 
unique.</field>
+        </record>
+    </data>
+</tryton>
diff -r e7268f76086b -r b064b4f02c25 tryton.cfg
--- a/tryton.cfg        Mon Oct 01 12:52:17 2018 +0200
+++ b/tryton.cfg        Sat Dec 29 14:20:30 2018 +0100
@@ -9,3 +9,4 @@
     timesheet.xml
     work.xml
     line.xml
+    message.xml
diff -r e7268f76086b -r b064b4f02c25 work.py
--- a/work.py   Mon Oct 01 12:52:17 2018 +0200
+++ b/work.py   Sat Dec 29 14:20:30 2018 +0100
@@ -6,12 +6,15 @@
 from sql import Literal
 from sql.aggregate import Sum
 
+from trytond.i18n import gettext
 from trytond.model import ModelView, ModelSQL, DeactivableMixin, fields, Unique
 from trytond.pyson import Not, Bool, Eval, If
 from trytond.transaction import Transaction
 from trytond.pool import Pool
 from trytond.tools import reduce_ids, grouped_slice
 
+from .exceptions import CompanyValidationError
+
 __all__ = ['Work', 'WorkContext']
 
 
@@ -70,12 +73,8 @@
         t = cls.__table__()
         cls._sql_constraints += [
             ('origin_unique', Unique(t, t.origin, t.company),
-                'The origin must be unique per company.'),
+                'timesheet.msg_work_origin_unique_company'),
             ]
-        cls._error_messages.update({
-                'mismatch_company': ('The company of the work "%(work)s" '
-                    'is different than the origin\'s company'),
-                })
 
     @classmethod
     def __register__(cls, module_name):
@@ -204,9 +203,9 @@
         super(Work, cls).validate(works)
         for work in works:
             if work.origin and not work._validate_company():
-                cls.raise_user_error('mismatch_company', {
-                        'work': work.rec_name,
-                        })
+                raise CompanyValidationError(
+                    gettext('timesheet.msg_work_company_different_origin',
+                        work=work.rec_name))
 
     def _validate_company(self):
         return True

Reply via email to