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

        issue3672
diffstat:

 commission.py |  24 +++++++++---------------
 exceptions.py |   8 ++++++++
 message.xml   |  13 +++++++++++++
 party.py      |  21 +++++++--------------
 tryton.cfg    |   1 +
 5 files changed, 38 insertions(+), 29 deletions(-)

diffs (135 lines):

diff -r cc511f4700fd -r 7ec751691778 commission.py
--- a/commission.py     Mon Oct 01 13:26:15 2018 +0200
+++ b/commission.py     Sat Dec 29 14:20:29 2018 +0100
@@ -11,6 +11,7 @@
     Null = None
 from sql.aggregate import Sum
 
+from trytond.i18n import gettext
 from trytond.model import ModelView, ModelSQL, MatchMixin, fields, \
     sequence_ordered
 from trytond.pyson import Eval, Bool, If, Id, PYSONEncoder
@@ -21,6 +22,8 @@
 
 from trytond.modules.product import price_digits
 
+from .exceptions import FormulaError
+
 __all__ = ['Agent', 'Plan', 'PlanLines', 'Commission',
     'CreateInvoice', 'CreateInvoiceAsk']
 
@@ -174,15 +177,6 @@
         help=('Python expression that will be evaluated with:\n'
             '- amount: the original amount'))
 
-    @classmethod
-    def __setup__(cls):
-        super(PlanLines, cls).__setup__()
-        cls._error_messages.update({
-                'invalid_formula': ('Invalid formula "%(formula)s" in '
-                    'commission plan line "%(line)s" with exception '
-                    '"%(exception)s".'),
-                })
-
     @staticmethod
     def default_formula():
         return 'amount'
@@ -199,12 +193,12 @@
         try:
             if not isinstance(self.get_amount(**context), Decimal):
                 raise ValueError
-        except ValueError as exception:
-            self.raise_user_error('invalid_formula', {
-                    'formula': self.formula,
-                    'line': self.rec_name,
-                    'exception': exception,
-                    })
+        except Exception as exception:
+            raise FormulaError(
+                gettext('commission.msg_plan_line_invalid_formula',
+                    formula=self.formula,
+                    line=self.rec_name,
+                    exception=exception)) from exception
 
     def get_amount(self, **context):
         'Return amount (as Decimal)'
diff -r cc511f4700fd -r 7ec751691778 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 FormulaError(ValidationError):
+    pass
diff -r cc511f4700fd -r 7ec751691778 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,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_plan_line_invalid_formula">
+            <field name="text">Invalid formula "%(formula)s" in commission 
plan line "%(line)s" with exception "%(exception)s".</field>
+        </record>
+        <record model="ir.message" id="msg_erase_party_pending_commission">
+            <field name="text">You cannot erase party "%(party)s" while they 
have pending commissions with company "%(company)s".</field>
+        </record>
+    </data>
+</tryton>
diff -r cc511f4700fd -r 7ec751691778 party.py
--- a/party.py  Mon Oct 01 13:26:15 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({
-                'pending_commission': (
-                    'The party "%(party)s" can not be erased '
-                    'because he has pending commissions '
-                    'for the company "%(company)s".'),
-                })
-
     def check_erase_company(self, party, company):
         pool = Pool()
         Commission = pool.get('commission')
@@ -38,7 +31,7 @@
                 ('invoice_line', '=', None),
                 ])
         if commissions:
-            self.raise_user_error('pending_commission', {
-                    'party': party.rec_name,
-                    'company': company.rec_name,
-                    })
+            raise EraseError(
+                gettext('commission.msg_erase_party_pending_commission',
+                    party=party.rec_name,
+                    company=company.rec_name))
diff -r cc511f4700fd -r 7ec751691778 tryton.cfg
--- a/tryton.cfg        Mon Oct 01 13:26:15 2018 +0200
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -16,3 +16,4 @@
     sale.xml
     account.xml
     product.xml
+    message.xml

Reply via email to