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

        issue3672
diffstat:

 currency.py   |  18 ++++++++----------
 exceptions.py |   8 ++++++++
 message.xml   |  16 ++++++++++++++++
 tryton.cfg    |   1 +
 4 files changed, 33 insertions(+), 10 deletions(-)

diffs (97 lines):

diff -r 29c51ade0606 -r 2e00af3c94e0 currency.py
--- a/currency.py       Fri Nov 02 20:02:53 2018 +0100
+++ b/currency.py       Sat Dec 29 14:20:29 2018 +0100
@@ -7,6 +7,7 @@
 from sql import Window
 from sql.functions import NthValue
 
+from trytond.i18n import gettext
 from trytond.model import (
     ModelView, ModelSQL, DeactivableMixin, fields, Unique, Check)
 from trytond.transaction import Transaction
@@ -14,6 +15,8 @@
 from trytond.rpc import RPC
 from trytond.pyson import Eval
 
+from .exceptions import RateError
+
 __all__ = ['Currency', 'Rate']
 
 
@@ -42,10 +45,6 @@
     def __setup__(cls):
         super(Currency, cls).__setup__()
         cls._order.insert(0, ('code', 'ASC'))
-        cls._error_messages.update({
-                'no_rate': ('No rate found for currency "%(currency)s" on '
-                    '"%(date)s"'),
-                })
         cls.__rpc__.update({
                 'compute': RPC(instantiate=slice(0, 3, 2)),
                 })
@@ -167,10 +166,9 @@
                 name = to_currency.name
 
             lang = Lang.get()
-            cls.raise_user_error('no_rate', {
-                    'currency': name,
-                    'date': lang.strftime(date)
-                    })
+            raise RateError(gettext('currency.msg_no_rate',
+                    currency=name,
+                    date=lang.strftime(date)))
         if round:
             return to_currency.round(
                 amount * to_currency.rate / from_currency.rate)
@@ -229,9 +227,9 @@
         t = cls.__table__()
         cls._sql_constraints = [
             ('date_currency_uniq', Unique(t, t.date, t.currency),
-                'A currency can only have one rate by date.'),
+                'currency.msg_currency_unique_rate_date'),
             ('check_currency_rate', Check(t, t.rate >= 0),
-                'The currency rate must greater than or equal to 0'),
+                'currency.msg_currency_rate_positive'),
             ]
         cls._order.insert(0, ('date', 'DESC'))
 
diff -r 29c51ade0606 -r 2e00af3c94e0 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.exceptions import UserError
+
+
+class RateError(UserError):
+    pass
diff -r 29c51ade0606 -r 2e00af3c94e0 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_no_rate">
+            <field name="text">No rate found for currency "%(currency)s" on 
"%(date)s".</field>
+        </record>
+        <record model="ir.message" id="msg_currency_unique_rate_date">
+            <field name="text">A currency can only have one rate by 
date.</field>
+        </record>
+        <record model="ir.message" id="msg_currency_rate_positive">
+            <field name="text">A currency rate must be positive.</field>
+        </record>
+    </data>
+</tryton>
diff -r 29c51ade0606 -r 2e00af3c94e0 tryton.cfg
--- a/tryton.cfg        Fri Nov 02 20:02:53 2018 +0100
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -6,3 +6,4 @@
 xml:
     data.xml
     currency.xml
+    message.xml

Reply via email to