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

        issue3672
diffstat:

 exceptions.py |  12 ++++++++++++
 message.xml   |  13 +++++++++++++
 party.py      |  47 +++++++++++++++++++++++++++--------------------
 tryton.cfg    |   1 +
 4 files changed, 53 insertions(+), 20 deletions(-)

diffs (136 lines):

diff -r acbae9bd1418 -r de6a1dbbf8bb exceptions.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/exceptions.py     Sat Dec 29 14:20:28 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.exceptions import UserError, UserWarning
+
+
+class CreditLimitError(UserError):
+    pass
+
+
+class CreditLimitWarning(UserWarning):
+    pass
diff -r acbae9bd1418 -r de6a1dbbf8bb message.xml
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/message.xml       Sat Dec 29 14:20:28 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_party_credit_limit_amount">
+            <field name="text">Party "%(party)s" has reached their credit 
limit of %(limit)s.</field>
+        </record>
+        <record model="ir.message" id="msg_party_credit_limit_dunning">
+            <field name="text">Party "%(party)s" has reached the credit limit 
level of %(dunning)s.</field>
+        </record>
+    </data>
+</tryton>
diff -r acbae9bd1418 -r de6a1dbbf8bb party.py
--- a/party.py  Mon Oct 01 12:53:02 2018 +0200
+++ b/party.py  Sat Dec 29 14:20:28 2018 +0100
@@ -1,6 +1,7 @@
 # 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 import backend
+from trytond.i18n import gettext
 from trytond.model import ModelSQL, fields
 from trytond.pool import Pool, PoolMeta
 from trytond.transaction import Transaction
@@ -8,6 +9,8 @@
 from trytond.tools.multivalue import migrate_property
 from trytond.modules.company.model import CompanyValueMixin
 
+from .exceptions import CreditLimitError, CreditLimitWarning
+
 __all__ = ['Party', 'PartyCreditLimitAmount']
 
 
@@ -28,16 +31,6 @@
         'party.party.credit_limit_amount', 'party', "Credit Limit Amounts")
 
     @classmethod
-    def __setup__(cls):
-        super(Party, cls).__setup__()
-        cls._error_messages.update({
-                'credit_limit_amount': (
-                    '"%s" has reached the credit limit amount (%s)'),
-                'credit_limit_dunning': (
-                    '"%s" has reached the dunning credit limit (%s)'),
-                })
-
-    @classmethod
     def default_credit_limit_amount(cls, **pattern):
         pool = Pool()
         Configuration = pool.get('account.configuration')
@@ -70,6 +63,7 @@
         Group = pool.get('res.group')
         Company = pool.get('company.company')
         Lang = pool.get('ir.lang')
+        Warning = pool.get('res.user.warning')
 
         if self.credit_limit_amount is None:
             return
@@ -92,14 +86,20 @@
         if self.credit_limit_amount < self.credit_amount + amount:
             company = Company(Transaction().context.get('company'))
             lang = Lang.get()
+            limit = lang.currency(self.credit_limit_amount, company.currency)
             if not in_group():
-                self.raise_user_error('credit_limit_amount',
-                    (self.rec_name, lang.currency(
-                            self.credit_limit_amount, company.currency)))
+                raise CreditLimitError(
+                    gettext('account_credit_limit'
+                        '.msg_party_credit_limit_amount',
+                        party=self.rec_name,
+                        limit=limit))
             warning_name = 'credit_limit_amount_%s' % origin
-            self.raise_user_warning(warning_name, 'credit_limit_amount',
-                (self.rec_name, lang.currency(
-                        self.credit_limit_amount, company.currency)))
+            if Warning.check(warning_name):
+                raise CreditLimitWarning(warning_name,
+                    gettext('account_credit_limit'
+                        '.msg_party_credit_limit_amount',
+                        party=self.rec_name,
+                        limit=limit))
 
         if Dunning:
             dunnings = Dunning.search([
@@ -110,11 +110,18 @@
             if dunnings:
                 dunning = dunnings[0]
                 if not in_group():
-                    self.raise_user_error('credit_limit_dunning',
-                        (self.rec_name, dunning.rec_name))
+                    raise CreditLimitError(
+                        gettext('account_credit_limit'
+                            '.msg_party_credit_limit_dunning',
+                            party=self.rec_name,
+                            dunning=dunning.rec_name))
                 warning_name = 'credit_limit_dunning_%s' % origin
-                self.raise_user_warning(warning_name, 'credit_limit_dunning',
-                    (self.rec_name, dunning.rec_name))
+                if Warning.check(warning_name):
+                    raise CreditLimitWarning(warning_name,
+                        gettext('account_credit_limit'
+                            '.msg_party_credit_limit_dunning',
+                            party=self.rec_name,
+                            dunning=dunning.rec_name))
 
     def get_credit_limit_digits(self, name):
         pool = Pool()
diff -r acbae9bd1418 -r de6a1dbbf8bb tryton.cfg
--- a/tryton.cfg        Mon Oct 01 12:53:02 2018 +0200
+++ b/tryton.cfg        Sat Dec 29 14:20:28 2018 +0100
@@ -10,3 +10,4 @@
 xml:
     account.xml
     party.xml
+    message.xml

Reply via email to