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

        issue3672
diffstat:

 exceptions.py |   8 ++++++++
 message.xml   |  25 +++++++++++++++++++++++++
 uom.py        |  34 +++++++++++++++-------------------
 3 files changed, 48 insertions(+), 19 deletions(-)

diffs (117 lines):

diff -r f64edf550ebe -r 8c885973a93f 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 UOMValidationError(ValidationError):
+    pass
diff -r f64edf550ebe -r 8c885973a93f 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,25 @@
+<?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_uom_modify_factor">
+            <field name="text">You cannot modify the factor of UOM 
"%(uom)s".</field>
+        </record>
+        <record model="ir.message" id="msg_uom_modify_rate">
+            <field name="text">You cannot modify the rate of UOM 
"%(uom)s".</field>
+        </record>
+        <record model="ir.message" id="msg_uom_modify_category">
+            <field name="text">You cannot modify the category of UOM 
"%(uom)s".</field>
+        </record>
+        <record model="ir.message" id="msg_uom_modify_options">
+            <field name="text">If the UOM is still not used, you can delete it 
otherwise you can deactivate it and create a new one.</field>
+        </record>
+        <record model="ir.message" id="msg_uom_incompatible_factor_rate">
+            <field name="text">Incompatible factor and rate values on UOM 
"%(uom)s".</field>
+        </record>
+        <record model="ir.message" id="msg_uom_no_zero_factor_rate">
+            <field name="text">Rate and factor can not be both equal to 
zero.</field>
+        </record>
+    </data>
+</tryton>
diff -r f64edf550ebe -r 8c885973a93f uom.py
--- a/uom.py    Mon Oct 01 12:43:43 2018 +0200
+++ b/uom.py    Sat Dec 29 14:20:29 2018 +0100
@@ -4,10 +4,14 @@
 from decimal import Decimal
 from math import ceil, floor, log10
 
+from trytond.i18n import gettext
 from trytond.model import ModelView, ModelSQL, DeactivableMixin, fields, Check
+from trytond.model.exceptions import AccessError
 from trytond.pyson import Eval
 from trytond.transaction import Transaction
 
+from .exceptions import UOMValidationError
+
 __all__ = ['UomCategory', 'Uom']
 
 STATES = {
@@ -59,18 +63,9 @@
         t = cls.__table__()
         cls._sql_constraints += [
             ('non_zero_rate_factor', Check(t, (t.rate != 0) | (t.factor != 0)),
-                'Rate and factor can not be both equal to zero.')
+                'product.msg_uom_no_zero_factor_rate')
             ]
         cls._order.insert(0, ('name', 'ASC'))
-        cls._error_messages.update({
-                'change_uom_rate_title': ('You cannot change Rate, Factor or '
-                    'Category on a Unit of Measure.'),
-                'change_uom_rate': ('If the UOM is still not used, you can '
-                    'delete it otherwise you can deactivate it '
-                    'and create a new one.'),
-                'invalid_factor_and_rate': (
-                    'Invalid Factor and Rate values in UOM "%s".'),
-                })
 
     @classmethod
     def check_xml_record(cls, records, values):
@@ -141,8 +136,9 @@
                     1.0 / self.factor, self.__class__.rate.digits[1])
                 and self.factor != round(
                     1.0 / self.rate, self.__class__.factor.digits[1])):
-            self.raise_user_error('invalid_factor_and_rate', (
-                        self.rec_name,))
+            raise UOMValidationError(
+                gettext('product.msg_uom_incompatible_factor_rate',
+                    uom=self.rec_name))
 
     @classmethod
     def write(cls, *args):
@@ -158,18 +154,18 @@
                 continue
             all_uoms += uoms
 
-        old_uom = dict((uom.id, (uom.factor, uom.rate, uom.category.id))
+        old_uom = dict((uom.id, (uom.factor, uom.rate, uom.category))
             for uom in all_uoms)
 
         super(Uom, cls).write(*args)
 
         for uom in all_uoms:
-            if uom.factor != old_uom[uom.id][0] \
-                    or uom.rate != old_uom[uom.id][1] \
-                    or uom.category.id != old_uom[uom.id][2]:
-
-                cls.raise_user_error('change_uom_rate_title',
-                    error_description='change_uom_rate')
+            for i, field in ['factor', 'rate', 'category']:
+                if getattr(uom, field) != old_uom[uom.id][i]:
+                    raise AccessError(
+                        gettext('product.msg_uom_modify_%s' % field,
+                            uom=uom.rec_name),
+                        gettext('product.msg_uom_modify_options'))
 
     @property
     def accurate_field(self):

Reply via email to