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

        issue3672
diffstat:

 bom.py        |   7 +------
 exceptions.py |   8 ++++++++
 message.xml   |  16 ++++++++++++++++
 product.py    |  14 +++++---------
 production.py |  18 ++++++++----------
 tryton.cfg    |   2 +-
 6 files changed, 39 insertions(+), 26 deletions(-)

diffs (154 lines):

diff -r de1a1e95b805 -r b19c33a8ada6 bom.py
--- a/bom.py    Mon Oct 01 12:57:15 2018 +0200
+++ b/bom.py    Sat Dec 29 14:20:29 2018 +0100
@@ -76,12 +76,8 @@
         t = cls.__table__()
         cls._sql_constraints = [
             ('product_bom_uniq', Unique(t, t.product, t.bom),
-                'product_bom_uniq'),
+                'production.msg_product_bom_unique'),
             ]
-        cls._error_messages.update({
-                'product_bom_uniq': 'Product must be unique per BOM.',
-                'recursive_bom': 'You can not create recursive BOMs.',
-                })
 
     @fields.depends('product', 'uom')
     def on_change_product(self):
@@ -92,7 +88,6 @@
                 self.unit_digits = self.product.default_uom.digits
         else:
             self.uom = None
-            self.unit_digits = 2
 
     @fields.depends('product')
     def on_change_with_uom_category(self, name=None):
diff -r de1a1e95b805 -r b19c33a8ada6 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 CostError(ValidationError):
+    pass
diff -r de1a1e95b805 -r b19c33a8ada6 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_recursive_bom">
+            <field name="text">You cannot create a recursive BOM for product 
"%(product)s".</field>
+        </record>
+        <record model="ir.message" id="msg_product_bom_unique">
+            <field name="text">Products can only appear once on each 
BOM.</field>
+        </record>
+        <record model="ir.message" id="msg_uneven_costs">
+            <field name="text">The costs of the outputs "%(outputs)s" of 
production "%(production)s" do not match the cost of the production 
"%(costs)s".</field>
+        </record>
+    </data>
+</tryton>
diff -r de1a1e95b805 -r b19c33a8ada6 product.py
--- a/product.py        Mon Oct 01 12:57:15 2018 +0200
+++ b/product.py        Sat Dec 29 14:20:29 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.i18n import gettext
 from trytond.model import ModelView, ModelSQL, MatchMixin, fields, \
     sequence_ordered
+from trytond.model.exceptions import RecursionError
 from trytond.pyson import Eval, Get, If, Bool
 from trytond.pool import PoolMeta
 
@@ -35,14 +37,6 @@
         depends=['type'])
 
     @classmethod
-    def __setup__(cls):
-        super(Product, cls).__setup__()
-        cls._error_messages.update({
-                'recursive_bom': ('You are trying to create a recursive BOM '
-                    'with product "%s" which is not allowed.'),
-                })
-
-    @classmethod
     def validate(cls, products):
         super(Product, cls).validate(products)
         for product in products:
@@ -58,7 +52,9 @@
             for input_ in product_bom.bom.inputs:
                 if (input_.product == product or
                         input_.product.check_bom_recursion(product=product)):
-                    self.raise_user_error('recursive_bom', (product.rec_name,))
+                    raise RecursionError(
+                        gettext('production.msg_recursive_bom',
+                            product=product.rec_name))
 
     @classmethod
     def copy(cls, products, default=None):
diff -r de1a1e95b805 -r b19c33a8ada6 production.py
--- a/production.py     Mon Oct 01 12:57:15 2018 +0200
+++ b/production.py     Sat Dec 29 14:20:29 2018 +0100
@@ -4,6 +4,7 @@
 
 from sql import Null
 
+from trytond.i18n import gettext
 from trytond.model import ModelView, ModelSQL, Workflow, fields
 from trytond.wizard import Wizard, StateTransition, StateView, Button
 from trytond.pyson import Eval, Bool, If, Id
@@ -12,6 +13,8 @@
 
 from trytond.modules.product import price_digits
 
+from .exceptions import CostError
+
 __all__ = ['Production', 'AssignFailed', 'Assign']
 
 BOM_CHANGES = ['bom', 'product', 'quantity', 'uom', 'warehouse', 'location',
@@ -152,11 +155,6 @@
     @classmethod
     def __setup__(cls):
         super(Production, cls).__setup__()
-        cls._error_messages.update({
-                'uneven_costs': ('The costs of the outputs (%(outputs)s) of '
-                    'production "%(production)s" do not match the cost of the '
-                    'production (%(costs)s).')
-                })
         cls._transitions |= set((
                 ('request', 'draft'),
                 ('draft', 'waiting'),
@@ -489,11 +487,11 @@
             return
         cost_price = self.output_cost
         if not self.company.currency.is_zero(self.cost - cost_price):
-            self.raise_user_error('uneven_costs', {
-                    'production': self.rec_name,
-                    'costs': self.cost,
-                    'outputs': cost_price,
-                    })
+            raise CostError(gettext(
+                    'production.msg_uneven_costs',
+                    production=self.rec_name,
+                    costs=self.cost,
+                    outputs=cost_price))
 
     @classmethod
     def create(cls, vlist):
diff -r de1a1e95b805 -r b19c33a8ada6 tryton.cfg
--- a/tryton.cfg        Mon Oct 01 12:57:15 2018 +0200
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -12,4 +12,4 @@
     bom.xml
     product.xml
     stock.xml
-
+    message.xml

Reply via email to