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

        issue3672
diffstat:

 account.py    |  35 ++++++++++++++---------------------
 exceptions.py |  12 ++++++++++++
 line.py       |  23 +----------------------
 message.xml   |  16 ++++++++++++++++
 tryton.cfg    |   1 +
 5 files changed, 44 insertions(+), 43 deletions(-)

diffs (169 lines):

diff -r 627267730bb4 -r 056f5a821c03 account.py
--- a/account.py        Mon Oct 01 12:46:21 2018 +0200
+++ b/account.py        Sat Dec 29 14:20:29 2018 +0100
@@ -8,6 +8,7 @@
 from sql.conditionals import Coalesce
 
 from trytond import backend
+from trytond.i18n import gettext
 from trytond.model import (ModelView, ModelSQL, DeactivableMixin, fields,
     Unique, tree)
 from trytond.wizard import Wizard, StateView, StateAction, Button
@@ -15,6 +16,8 @@
 from trytond.transaction import Transaction
 from trytond.pool import Pool
 
+from .exceptions import AccountValidationError, RootValidationError
+
 __all__ = ['Account', 'AccountDistribution',
     'OpenChartAccountStart', 'OpenChartAccount',
     'AnalyticAccountEntry', 'AnalyticMixin']
@@ -109,11 +112,6 @@
         super(Account, cls).__setup__()
         cls._order.insert(0, ('code', 'ASC'))
         cls._order.insert(1, ('name', 'ASC'))
-        cls._error_messages.update({
-                'invalid_distribution': (
-                    'The distribution sum of account "%(account)s" '
-                    'is not 100%%.'),
-                })
 
     @classmethod
     def __register__(cls, module_name):
@@ -153,9 +151,9 @@
         if self.type != 'distribution':
             return
         if sum((d.ratio for d in self.distributions)) != 1:
-            self.raise_user_error('invalid_distribution', {
-                    'account': self.rec_name,
-                    })
+            raise AccountValidationError(
+                gettext('analytic_account.msg_invalid_distribution',
+                    account=self.rec_name))
 
     @fields.depends('company')
     def on_change_with_currency(self, name=None):
@@ -447,7 +445,7 @@
         t = cls.__table__()
         cls._sql_constraints += [
             ('root_origin_uniq', Unique(t, t.origin, t.root),
-                'Only one account is allowed per analytic root and origin.'),
+                'analytic_account.msg_root_origin_unique'),
             ]
 
     @classmethod
@@ -503,14 +501,6 @@
             'Analytic Accounts Size'), 'get_analytic_accounts_size')
 
     @classmethod
-    def __setup__(cls):
-        super(AnalyticMixin, cls).__setup__()
-        cls._error_messages.update({
-                'root_account': ('Some mandatory root account are missing '
-                    'on "%(name)s"'),
-                })
-
-    @classmethod
     def __register__(cls, module_name):
         pool = Pool()
         AccountEntry = pool.get('analytic.account.entry')
@@ -591,7 +581,10 @@
             for mandatory in all_mandatory_roots:
                 if mandatory.company in companies:
                     mandatory_roots.add(mandatory)
-            if not mandatory_roots <= analytic_roots:
-                cls.raise_user_error('root_account', {
-                        'name': analytic.rec_name,
-                        })
+            mandatory_roots.difference_update(analytic_roots)
+            if mandatory_roots:
+                raise RootValidationError(
+                    gettext('analytic_account.msg_missing_root_account',
+                        name=analytic.rec_name,
+                        analytics=', '.join(
+                            (r.rec_name for r in mandatory_roots))))
diff -r 627267730bb4 -r 056f5a821c03 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,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 AccountValidationError(ValidationError):
+    pass
+
+
+class RootValidationError(ValidationError):
+    pass
diff -r 627267730bb4 -r 056f5a821c03 line.py
--- a/line.py   Mon Oct 01 12:46:21 2018 +0200
+++ b/line.py   Sat Dec 29 14:20:29 2018 +0100
@@ -46,15 +46,8 @@
             ('credit_debit',
                 Check(t,
                     (t.credit * t.debit == 0) & (t.credit + t.debit >= 0)),
-                'Wrong credit/debit values.'),
+                'account.msg_line_debit_credit'),
             ]
-        cls._error_messages.update({
-                'line_on_view_account': (
-                    'You can not create a move line using '
-                    'view account "%s".'),
-                'line_on_inactive_account': ('You can not create a move line '
-                    'using inactive account "%s".'),
-                })
         cls._order.insert(0, ('date', 'ASC'))
 
     @classmethod
@@ -144,20 +137,6 @@
         MoveLine.set_analytic_state(move_lines)
         MoveLine.save(move_lines)
 
-    @classmethod
-    def validate(cls, lines):
-        super(Line, cls).validate(lines)
-        for line in lines:
-            line.check_account()
-
-    def check_account(self):
-        if self.account.type == 'view':
-            self.raise_user_error('line_on_view_account',
-                (self.account.rec_name,))
-        if not self.account.active:
-            self.raise_user_error('line_on_inactive_account',
-                (self.account.rec_name,))
-
 
 class Move(metaclass=PoolMeta):
     __name__ = 'account.move'
diff -r 627267730bb4 -r 056f5a821c03 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_invalid_distribution">
+            <field name="text">To save account "%(account)s" the sum of their 
distributions must be 100%%.</field>
+        </record>
+        <record model="ir.message" id="msg_missing_root_account">
+            <field name="text">To save "%(name)s" you must set a value for 
analytics "%(analytics)s".</field>
+        </record>
+        <record model="ir.message" id="msg_root_origin_unique">
+            <field name="text">Only one account is allowed by analytic root 
and origin.</field>
+        </record>
+    </data>
+</tryton>
diff -r 627267730bb4 -r 056f5a821c03 tryton.cfg
--- a/tryton.cfg        Mon Oct 01 12:46:21 2018 +0200
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -12,3 +12,4 @@
     account.xml
     line.xml
     rule.xml
+    message.xml

Reply via email to