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

        issue3672
diffstat:

 account.py                      |  28 +++++++++++-----------------
 message.xml                     |  19 +++++++++++++++++++
 sale.py                         |  22 +++++-----------------
 tests/scenario_sale_payment.rst |   8 ++++----
 tryton.cfg                      |   1 +
 5 files changed, 40 insertions(+), 38 deletions(-)

diffs (180 lines):

diff -r aa89c192ff01 -r e3b122c29552 account.py
--- a/account.py        Mon Oct 01 13:32:27 2018 +0200
+++ b/account.py        Sat Dec 29 14:20:29 2018 +0100
@@ -2,8 +2,11 @@
 # this repository contains the full copyright notices and license terms.
 import functools
 
+from trytond.i18n import gettext
 from trytond.pool import Pool, PoolMeta
 
+from trytond.modules.account_payment.exceptions import PaymentValidationError
+
 __all__ = ['Payment', 'Invoice']
 
 
@@ -28,15 +31,6 @@
     __name__ = 'account.payment'
 
     @classmethod
-    def __setup__(cls):
-        super(Payment, cls).__setup__()
-        cls._error_messages.update({
-                'payment_on_cancelled_sale': (
-                    'Payment cannot be approved because sale "%(sale)s" '
-                    'is cancelled.'),
-                })
-
-    @classmethod
     def _get_origin(cls):
         return super(Payment, cls)._get_origin() + ['sale.sale']
 
@@ -55,15 +49,15 @@
             # Do not prevent succeeding payment
             return
         if self.state != 'failed' and self.origin.state == 'draft':
-            self.raise_user_error('payment_on_draft_sale', {
-                    'payment': self.rec_name,
-                    'sale': self.origin.rec_name,
-                    })
+            raise PaymentValidationError(
+                gettext('sale_payment.msg_payment_sale_draft',
+                    sale=self.origin.rec_name,
+                    payment=self.rec_name))
         elif self.state == 'draft' and self.origin.state == 'cancel':
-            self.raise_user_error('payment_on_cancelled_sale', {
-                    'payment': self.rec_name,
-                    'sale': self.origin.rec_name,
-                    })
+            raise PaymentValidationError(
+                gettext('sale_payment.msg_payment_sale_cancel',
+                    sale=self.origin.rec_name,
+                    payment=self.rec_name))
 
     @classmethod
     def create(cls, vlist):
diff -r aa89c192ff01 -r e3b122c29552 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,19 @@
+<?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_sale_cancel_payment">
+            <field name="text">You cannot cancel sale "%(sale)s" because it 
has payments.</field>
+        </record>
+        <record model="ir.message" id="msg_sale_draft_payment">
+            <field name="text">You cannot reset to draft sale "%(sale)s" 
because it has payments.</field>
+        </record>
+        <record model="ir.message" id="msg_payment_sale_draft">
+            <field name="text">You cannot use sale "%(sale)s" on payment 
"%(payment)s" because it is a draft sale.</field>
+        </record>
+        <record model="ir.message" id="msg_payment_sale_cancel">
+            <field name="text">You cannot use sale "%(sale)s" on payment 
"%(payment)s" because it is a cancelled sale.</field>
+        </record>
+    </data>
+</tryton>
diff -r aa89c192ff01 -r e3b122c29552 sale.py
--- a/sale.py   Mon Oct 01 13:32:27 2018 +0200
+++ b/sale.py   Sat Dec 29 14:20:29 2018 +0100
@@ -2,7 +2,9 @@
 # this repository contains the full copyright notices and license terms.
 import functools
 
+from trytond.i18n import gettext
 from trytond.model import ModelView, Workflow, fields
+from trytond.model.exceptions import AccessError
 from trytond.pool import PoolMeta
 from trytond.pyson import Eval, If
 
@@ -15,9 +17,7 @@
         def wrapper(cls, sales, *args, **kwargs):
             for sale in sales:
                 if not all((p.state == 'failed' for p in sale.payments)):
-                    cls.raise_user_error(error, {
-                            'sale': sale.rec_name,
-                            })
+                    raise AccessError(gettext(error, sale=sale.rec_name))
             return func(cls, sales, *args, **kwargs)
         return wrapper
     return decorator
@@ -42,28 +42,16 @@
         depends=['company', 'total_amount', 'party', 'currency', 'state'])
 
     @classmethod
-    def __setup__(cls):
-        super(Sale, cls).__setup__()
-        cls._error_messages.update({
-                'cancel_with_payment': (
-                    'The sale "%(sale)s" can not be canceled because '
-                    'it has payments.'),
-                'draft_with_payment': (
-                    'The sale "%(sale)s" can not be reset to draft because '
-                    'it has payments.'),
-                })
-
-    @classmethod
     @ModelView.button
     @Workflow.transition('cancel')
-    @no_payment('cancel_with_payment')
+    @no_payment('sale_payment.msg_sale_cancel_payment')
     def cancel(cls, sales):
         super(Sale, cls).cancel(sales)
 
     @classmethod
     @ModelView.button
     @Workflow.transition('draft')
-    @no_payment('draft_with_payment')
+    @no_payment('sale_payment.msg_sale_draft_payment')
     def draft(cls, sales):
         super(Sale, cls).draft(sales)
 
diff -r aa89c192ff01 -r e3b122c29552 tests/scenario_sale_payment.rst
--- a/tests/scenario_sale_payment.rst   Mon Oct 01 13:32:27 2018 +0200
+++ b/tests/scenario_sale_payment.rst   Sat Dec 29 14:20:29 2018 +0100
@@ -103,7 +103,7 @@
     >>> sale.click('draft')  # doctest: +IGNORE_EXCEPTION_DETAIL
     Traceback (most recent call last):
         ...
-    UserError: ...
+    AccessError: ...
     >>> sale.state
     'quotation'
 
@@ -112,7 +112,7 @@
     >>> sale.click('cancel')  # doctest: +IGNORE_EXCEPTION_DETAIL
     Traceback (most recent call last):
         ...
-    UserError: ...
+    AccessError: ...
     >>> sale.state
     'quotation'
 
@@ -138,7 +138,7 @@
     >>> payment.save()  # doctest: +IGNORE_EXCEPTION_DETAIL
     Traceback (most recent call last):
         ...
-    UserError: ...
+    PaymentValidationError: ...
 
 Cancel the sale::
 
@@ -157,7 +157,7 @@
     >>> payment.save()  # doctest: +IGNORE_EXCEPTION_DETAIL
     Traceback (most recent call last):
         ...
-    UserError: ...
+    PaymentValidationError: ...
 
 Revive the sale::
 
diff -r aa89c192ff01 -r e3b122c29552 tryton.cfg
--- a/tryton.cfg        Mon Oct 01 13:32:27 2018 +0200
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -10,3 +10,4 @@
     account_payment_clearing
 xml:
     sale.xml
+    message.xml

Reply via email to