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

        issue3672
diffstat:

 exceptions.py |   7 +++++++
 message.xml   |  22 ++++++++++++++++++++++
 timesheet.py  |  16 ++++++----------
 tryton.cfg    |   1 +
 work.py       |  36 ++++++++++++++++++++++++------------
 5 files changed, 60 insertions(+), 22 deletions(-)

diffs (179 lines):

diff -r b46504e9fff0 -r 7eee3b5afdfa 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,7 @@
+# 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 InvoicingError(UserError):
+    pass
diff -r b46504e9fff0 -r 7eee3b5afdfa 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,22 @@
+<?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_modify_invoiced_line">
+            <field name="text">You cannot modify an invoiced line.</field>
+        </record>
+        <record model="ir.message" id="msg_delete_invoiced_line">
+            <field name="text">You cannot delete an invoiced line.</field>
+        </record>
+        <record model="ir.message" id="msg_missing_product">
+            <field name="text">There is no product on work "%(work)s".</field>
+        </record>
+        <record model="ir.message" id="msg_missing_list_price">
+            <field name="text">There is no list price on work 
"%(work)s".</field>
+        </record>
+        <record model="ir.message" id="msg_missing_party">
+            <field name="text">There is no party on work "%(work)s".</field>
+        </record>
+    </data>
+</tryton>
diff -r b46504e9fff0 -r 7eee3b5afdfa timesheet.py
--- a/timesheet.py      Mon Oct 01 13:37:48 2018 +0200
+++ b/timesheet.py      Sat Dec 29 14:20:29 2018 +0100
@@ -1,6 +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.i18n import gettext
 from trytond.model import fields
+from trytond.model.exceptions import AccessError
 from trytond.pool import PoolMeta
 
 
@@ -13,14 +15,6 @@
         readonly=True)
 
     @classmethod
-    def __setup__(cls):
-        super(TimesheetLine, cls).__setup__()
-        cls._error_messages.update({
-                'modify_invoiced_line': 'You can not modify invoiced line.',
-                'delete_invoiced_line': 'You can not delete invoiced line.',
-                })
-
-    @classmethod
     def copy(cls, records, default=None):
         if default is None:
             default = {}
@@ -35,11 +29,13 @@
         for lines, values in zip(actions, actions):
             if (('duration' in values or 'work' in values)
                     and any(l.invoice_line for l in lines)):
-                cls.raise_user_error('modify_invoiced_line')
+                raise AccessError(
+                    gettext('project_invoice.msg_modify_invoiced_line'))
         super(TimesheetLine, cls).write(*args)
 
     @classmethod
     def delete(cls, records):
         if any(r.invoice_line for r in records):
-            cls.raise_user_error('delete_invoiced_line')
+            raise AccessError(
+                gettext('project_invoice.msg_delete_invoiced_line'))
         super(TimesheetLine, cls).delete(records)
diff -r b46504e9fff0 -r 7eee3b5afdfa tryton.cfg
--- a/tryton.cfg        Mon Oct 01 13:37:48 2018 +0200
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -12,3 +12,4 @@
     project.xml
     work.xml
     timesheet.xml
+    message.xml
diff -r b46504e9fff0 -r 7eee3b5afdfa work.py
--- a/work.py   Mon Oct 01 13:37:48 2018 +0200
+++ b/work.py   Sat Dec 29 14:20:29 2018 +0100
@@ -11,6 +11,7 @@
 from sql.aggregate import Sum
 from sql.operators import Concat
 
+from trytond.i18n import gettext
 from trytond.model import ModelSQL, ModelView, fields
 from trytond.pool import PoolMeta
 from trytond.pyson import Eval, Bool, PYSONEncoder
@@ -19,6 +20,8 @@
 from trytond.wizard import Wizard, StateAction
 from trytond.tools import reduce_ids, grouped_slice
 
+from .exceptions import InvoicingError
+
 
 __all__ = ['Work', 'WorkInvoicedProgress', 'OpenInvoice']
 
@@ -79,11 +82,6 @@
                         'duration_to_invoice']
                     },
                 })
-        cls._error_messages.update({
-                'missing_product': 'There is no product on work "%s".',
-                'missing_list_price': 'There is no list price on work "%s".',
-                'missing_party': 'There is no party on work "%s".',
-                })
 
     @staticmethod
     def default_project_invoice_method():
@@ -407,7 +405,9 @@
             journal = None
 
         if not self.party:
-            self.raise_user_error('missing_party', (self.rec_name,))
+            raise InvoicingError(
+                gettext('project_invoice.msg_missing_party',
+                    work=self.rec_name))
 
         return Invoice(
             company=self.company,
@@ -485,9 +485,13 @@
                 and self.effort_hours
                 and self.state == 'done'):
             if not self.product:
-                self.raise_user_error('missing_product', (self.rec_name,))
+                raise InvoicingError(
+                    gettext('project_invoice.msg_missing_product',
+                        work=self.rec_name))
             elif self.list_price is None:
-                self.raise_user_error('missing_list_price', (self.rec_name,))
+                raise InvoicingError(
+                    gettext('project_invoice.msg_missing_list_price',
+                        work=self.rec_name))
             return [{
                     'product': self.product,
                     'quantity': self.effort_hours,
@@ -516,9 +520,13 @@
                 hour, quantity, self.product.default_uom)
         if quantity > 0:
             if not self.product:
-                self.raise_user_error('missing_product', (self.rec_name,))
+                raise InvoicingError(
+                    gettext('project_invoice.msg_missing_product',
+                        work=self.rec_name))
             elif self.list_price is None:
-                self.raise_user_error('missing_list_price', (self.rec_name,))
+                raise InvoicingError(
+                    gettext('project_invoice.msg_missing_list_price',
+                        work=self.rec_name))
             invoiced_progress = InvoicedProgress(work=self,
                 effort_duration=datetime.timedelta(hours=quantity))
             return [{
@@ -540,9 +548,13 @@
         if (self.timesheet_works
                 and any(tw.timesheet_lines for tw in self.timesheet_works)):
             if not self.product:
-                self.raise_user_error('missing_product', (self.rec_name,))
+                raise InvoicingError(
+                    gettext('project_invoice.msg_missing_product',
+                        work=self.rec_name))
             elif self.list_price is None:
-                self.raise_user_error('missing_list_price', (self.rec_name,))
+                raise InvoicingError(
+                    gettext('project_invoice.msg_missing_list_price',
+                        work=self.rec_name))
             return [{
                     'product': self.product,
                     'quantity': l.hours,

Reply via email to