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

        issue3672
diffstat:

 exceptions.py                      |  12 ++++++++++++
 message.xml                        |  13 +++++++++++++
 sale.py                            |  20 ++++++++------------
 stock.py                           |  18 ++++++------------
 tests/scenario_advance_payment.rst |   2 +-
 tryton.cfg                         |   1 +
 6 files changed, 41 insertions(+), 25 deletions(-)

diffs (147 lines):

diff -r 5a7767165eae -r 33a3107ef240 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, AccessError
+
+
+class FormulaError(ValidationError):
+    pass
+
+
+class ShippingBlocked(AccessError):
+    pass
diff -r 5a7767165eae -r 33a3107ef240 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,13 @@
+<?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_term_line_invalid_formula">
+            <field name="text">Invalid formula "%(formula)s" in term line 
"%(term_line)s" with exception "%(exception)s".</field>
+        </record>
+        <record model="ir.message" id="msg_shipping_blocked">
+            <field name="text">To pack shipments the customer must paid the 
advance payment for sale "%(sale)s".</field>
+        </record>
+    </data>
+</tryton>
diff -r 5a7767165eae -r 33a3107ef240 sale.py
--- a/sale.py   Mon Oct 01 15:17:02 2018 +0200
+++ b/sale.py   Sat Dec 29 14:20:29 2018 +0100
@@ -4,6 +4,7 @@
 from simpleeval import simple_eval
 from itertools import chain
 
+from trytond.i18n import gettext
 from trytond.pool import Pool, PoolMeta
 from trytond.model import (
     ModelSQL, ModelView, Workflow, DeactivableMixin, fields)
@@ -12,6 +13,8 @@
 from trytond.transaction import Transaction
 from trytond.modules.company.model import CompanyMultiValueMixin, 
CompanyValueMixin
 
+from .exceptions import FormulaError
+
 __all__ = [
     'AdvancePaymentTerm', 'AdvancePaymentTermLine',
     'AdvancePaymentTermLineAccount',
@@ -80,14 +83,6 @@
             "- total_amount: The total amount of the sale.\n"
             "- untaxed_amount: The total untaxed amount of the sale.")
 
-    @classmethod
-    def __setup__(cls):
-        super(AdvancePaymentTermLine, cls).__setup__()
-        cls._error_messages.update({
-                'invalid_formula': ('Invalid formula "%(formula)s" with'
-                    ' exception "%(exception)s".'),
-                })
-
     @fields.depends('formula')
     def pre_validate(self, **names):
         super(AdvancePaymentTermLine, self).pre_validate()
@@ -96,10 +91,11 @@
             if not isinstance(self.compute_amount(**names), Decimal):
                 raise Exception('The formula does not return a Decimal')
         except Exception as exception:
-            self.raise_user_error('invalid_formula', {
-                    'formula': self.formula,
-                    'exception': exception,
-                    })
+            raise FormulaError(
+                gettext('sale_advance_payment.msg_term_line_invalid_formula',
+                    formula=self.formula,
+                    term_line=self.rec_name,
+                    exception=exception)) from exception
 
     def get_compute_amount_context(self, **names):
         return {
diff -r 5a7767165eae -r 33a3107ef240 stock.py
--- a/stock.py  Mon Oct 01 15:17:02 2018 +0200
+++ b/stock.py  Sat Dec 29 14:20:29 2018 +0100
@@ -1,9 +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.i18n import gettext
 from trytond.model import ModelView, Workflow
 from trytond.pool import Pool, PoolMeta
 
+from .exceptions import ShippingBlocked
+
 __all__ = ['ShipmentOut']
 
 
@@ -11,15 +14,6 @@
     __name__ = 'stock.shipment.out'
 
     @classmethod
-    def __setup__(cls):
-        super(ShipmentOut, cls).__setup__()
-        cls._error_messages.update({
-                'advance_payment_not_paid': ('The customer has not paid the'
-                    ' required advance payment amount for the sale'
-                    ' "%(sale)s".'),
-                })
-
-    @classmethod
     @ModelView.button
     @Workflow.transition('packed')
     def pack(cls, shipments):
@@ -32,7 +26,7 @@
             if isinstance(move.origin, SaleLine)}
         for sale in Sale.browse([s.id for s in sales]):
             if sale.shipping_blocked:
-                cls.raise_user_error('advance_payment_not_paid', {
-                        'sale': sale.rec_name,
-                        })
+                raise ShippingBlocked(
+                    gettext('sale_advance_payment.msg_shipping_blocked',
+                        sale=sale.rec_name))
         super(ShipmentOut, cls).pack(shipments)
diff -r 5a7767165eae -r 33a3107ef240 tests/scenario_advance_payment.rst
--- a/tests/scenario_advance_payment.rst        Mon Oct 01 15:17:02 2018 +0200
+++ b/tests/scenario_advance_payment.rst        Sat Dec 29 14:20:29 2018 +0100
@@ -252,7 +252,7 @@
     >>> shipment.click('pack')  # doctest: +IGNORE_EXCEPTION_DETAIL
     Traceback (most recent call last):
         ...
-    UserError: ...
+    ShippingBlocked: ...
 
 Let's pay the advance payment invoice::
 
diff -r 5a7767165eae -r 33a3107ef240 tryton.cfg
--- a/tryton.cfg        Mon Oct 01 15:17:02 2018 +0200
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -9,3 +9,4 @@
     sale_supply
 xml:
     sale.xml
+    message.xml

Reply via email to