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

        issue3672
diffstat:

 account.py                       |  18 ++++++------------
 asset.py                         |  11 ++++++-----
 invoice.py                       |   2 +-
 message.xml                      |  22 ++++++++++++++++++++++
 purchase.py                      |  18 ++++++++----------
 tests/scenario_account_asset.rst |   2 +-
 tryton.cfg                       |   1 +
 7 files changed, 45 insertions(+), 29 deletions(-)

diffs (178 lines):

diff -r bb9caedd368a -r 61705395ce1e account.py
--- a/account.py        Mon Oct 01 13:25:06 2018 +0200
+++ b/account.py        Sat Dec 29 14:20:28 2018 +0100
@@ -1,7 +1,9 @@
 # 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 import backend
+from trytond.i18n import gettext
 from trytond.model import ModelSQL, fields
+from trytond.model.exceptions import AccessError
 from trytond.pyson import Eval
 from trytond.pool import PoolMeta, Pool
 from trytond.tools.multivalue import migrate_property
@@ -134,14 +136,6 @@
 class Period(metaclass=PoolMeta):
     __name__ = 'account.period'
 
-    @classmethod
-    def __setup__(cls):
-        super(Period, cls).__setup__()
-        cls._error_messages.update({
-            'running_assets_close': ('Some asset lines "%(names)s" are still '
-                'running for this period "%(period)s".'),
-            })
-
     def check_asset_line_running(self):
         """
         Check if it exist some asset lines without account move for the curent
@@ -161,10 +155,10 @@
             names = ', '.join(a.rec_name for a in assets[:5])
             if len(assets) > 5:
                 names += '...'
-            self.raise_user_error('running_assets_close', {
-                    'period': self.rec_name,
-                    'names': names,
-                    })
+            raise AccessError(
+                gettext('account_asset.msg_asset_running_close_period',
+                    period=self.rec_name,
+                    assets=names))
 
     @classmethod
     def close(cls, periods):
diff -r bb9caedd368a -r 61705395ce1e asset.py
--- a/asset.py  Mon Oct 01 13:25:06 2018 +0200
+++ b/asset.py  Sat Dec 29 14:20:28 2018 +0100
@@ -8,7 +8,9 @@
 from itertools import groupby
 from cached_property import cached_property
 
+from trytond.i18n import gettext
 from trytond.model import Workflow, ModelSQL, ModelView, fields, Unique
+from trytond.model.exceptions import AccessError
 from trytond.pyson import Eval, Bool, If
 from trytond.pool import Pool
 from trytond.transaction import Transaction
@@ -173,11 +175,8 @@
         table = cls.__table__()
         cls._sql_constraints = [
             ('invoice_line_uniq', Unique(table, table.supplier_invoice_line),
-                'Supplier Invoice Line can be used only once on asset.'),
+                'account_asset.msg_asset_invoice_line_unique'),
             ]
-        cls._error_messages.update({
-                'delete_draft': 'Asset "%s" must be in draft to be deleted.',
-                })
         cls._transitions |= set((
                 ('draft', 'running'),
                 ('running', 'closed'),
@@ -630,7 +629,9 @@
     def delete(cls, assets):
         for asset in assets:
             if asset.state != 'draft':
-                cls.raise_user_error('delete_draft', asset.rec_name)
+                raise AccessError(
+                    gettext('account_asset.msg_delete_draft',
+                        asset=asset.rec_name))
         return super(Asset, cls).delete(assets)
 
 
diff -r bb9caedd368a -r 61705395ce1e invoice.py
--- a/invoice.py        Mon Oct 01 13:25:06 2018 +0200
+++ b/invoice.py        Sat Dec 29 14:20:28 2018 +0100
@@ -30,7 +30,7 @@
         table = cls.__table__()
         cls._sql_constraints += [
             ('asset_uniq', Unique(table, table.asset),
-                'Asset can be used only once on invoice line!'),
+                'account_asset.msg_invoice_line_asset_unique'),
             ]
 
     @fields.depends('product', 'invoice', 'invoice_type',
diff -r bb9caedd368a -r 61705395ce1e message.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/message.xml       Sat Dec 29 14:20:28 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_asset_delete_draft">
+            <field name="text">You cannot delete asset "%(asset)s" because it 
is not in "draft" state.</field>
+        </record>
+        <record model="ir.message" id="msg_asset_running_close_period">
+            <field name="text">You cannot close period "%(period)s" because 
some assets "%(assets)s" still have lines without move.</field>
+        </record>
+        <record model="ir.message" id="msg_asset_invoice_line_unique">
+            <field name="text">Invoice line can be used only once on 
asset.</field>
+        </record>
+        <record model="ir.message" 
id="msg_purchase_product_missing_account_asset">
+            <field name="text">To invoice purchase "%(purchase)s", you must 
set an account asset on product "%(product)s".</field>
+        </record>
+        <record model="ir.message" id="msg_invoice_line_asset_unique">
+            <field name="text">Asset can be used only once on invoice 
line.</field>
+        </record>
+    </data>
+</tryton>
diff -r bb9caedd368a -r 61705395ce1e purchase.py
--- a/purchase.py       Mon Oct 01 13:25:06 2018 +0200
+++ b/purchase.py       Sat Dec 29 14:20:28 2018 +0100
@@ -1,21 +1,16 @@
 # 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
 
+from trytond.modules.account_product.exceptions import AccountError
+
 __all__ = ['PurchaseLine']
 
 
 class PurchaseLine(metaclass=PoolMeta):
     __name__ = 'purchase.line'
 
-    @classmethod
-    def __setup__(cls):
-        super(PurchaseLine, cls).__setup__()
-        cls._error_messages.update({
-                'missing_account_asset': ('It misses '
-                    'an "Account Asset" on product "%s".'),
-                })
-
     def get_invoice_line(self):
         invoice_lines = super(PurchaseLine, self).get_invoice_line()
         if (self.product
@@ -25,6 +20,9 @@
                 if invoice_line.product == self.product:
                     invoice_line.account = self.product.account_asset_used
                     if not invoice_line.account:
-                        self.raise_user_error('missing_account_asset',
-                            error_args=(self.product.rec_name,))
+                        raise AccountError(
+                            gettext('account_asset'
+                                '.msg_purchase_product_missing_account_asset',
+                                purchase=self.purchase.rec_name,
+                                product=self.product.rec_name))
         return invoice_lines
diff -r bb9caedd368a -r 61705395ce1e tests/scenario_account_asset.rst
--- a/tests/scenario_account_asset.rst  Mon Oct 01 13:25:06 2018 +0200
+++ b/tests/scenario_account_asset.rst  Sat Dec 29 14:20:28 2018 +0100
@@ -150,7 +150,7 @@
     >>> period.click('close') # doctest: +IGNORE_EXCEPTION_DETAIL
     Traceback (most recent call last):
         ...
-    UserError: ...
+    AccessError: ...
 
 Create Moves for 3 months::
 
diff -r bb9caedd368a -r 61705395ce1e tryton.cfg
--- a/tryton.cfg        Mon Oct 01 13:25:06 2018 +0200
+++ b/tryton.cfg        Sat Dec 29 14:20:28 2018 +0100
@@ -24,3 +24,4 @@
     minimal_chart_pt.xml
     minimal_chart_ru.xml
     minimal_chart_sl.xml
+    message.xml

Reply via email to