changeset b46797e3a313 in modules/account_payment:default
details:
https://hg.tryton.org/modules/account_payment?cmd=changeset;node=b46797e3a313
description:
Add ir.message and use custom exceptions
issue3672
diffstat:
account.py | 19 ++++++++-----------
exceptions.py | 21 +++++++++++++++++++++
message.xml | 19 +++++++++++++++++++
party.py | 20 ++++++--------------
payment.py | 31 +++++++++++++------------------
tests/scenario_account_payment.rst | 2 +-
tryton.cfg | 1 +
7 files changed, 69 insertions(+), 44 deletions(-)
diffs (245 lines):
diff -r 839a51daeeec -r b46797e3a313 account.py
--- a/account.py Mon Oct 01 13:10:07 2018 +0200
+++ b/account.py Sat Dec 29 14:20:28 2018 +0100
@@ -8,6 +8,7 @@
from sql.functions import Abs
from trytond import backend
+from trytond.i18n import gettext
from trytond.pool import Pool, PoolMeta
from trytond.model import ModelSQL, ModelView, fields
from trytond.pyson import Eval, If, Bool
@@ -17,6 +18,7 @@
from trytond.tools.multivalue import migrate_property
from trytond.modules.company.model import CompanyValueMixin
+from .exceptions import BlockedWarning
from .payment import KINDS
__all__ = ['MoveLine', 'PayLine', 'PayLineAskJournal',
@@ -202,13 +204,6 @@
])
pay = StateAction('account_payment.act_payment_form')
- @classmethod
- def __setup__(cls):
- super(PayLine, cls).__setup__()
- cls._error_messages.update({
- 'blocked': 'The Line "%(line)s" is blocked.',
- })
-
def _get_journals(self):
journals = {}
for journal in getattr(self.ask_journal, 'journals', []):
@@ -287,6 +282,7 @@
pool = Pool()
Line = pool.get('account.move.line')
Payment = pool.get('account.payment')
+ Warning = pool.get('res.user.warning')
lines = Line.browse(Transaction().context['active_ids'])
journals = self._get_journals()
@@ -294,10 +290,11 @@
payments = []
for line in lines:
if line.payment_blocked:
- self.raise_user_warning('blocked:%s' % line,
- 'blocked', {
- 'line': line.rec_name,
- })
+ warning_name = 'blocked:%s' % line
+ if Warning.check(warning_name):
+ raise BlockedWarning(warning_name,
+ gettext('account_payment.msg_pay_line_blocked',
+ line=line.rec_name))
payments.append(self.get_payment(line, journals))
Payment.save(payments)
return action, {
diff -r 839a51daeeec -r b46797e3a313 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,21 @@
+# 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
+from trytond.model.exceptions import ValidationError
+
+
+class ProcessError(UserError):
+ pass
+
+
+class OverpayWarning(UserWarning):
+ pass
+
+
+class PaymentValidationError(ValidationError):
+ pass
+
+
+class BlockedWarning(UserWarning):
+ pass
diff -r 839a51daeeec -r b46797e3a313 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,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_payment_delete_draft">
+ <field name="text">To delete payment "%(payment)s" you must reset
it to draft state.</field>
+ </record>
+ <record model="ir.message" id="msg_payment_overpay">
+ <field name="text">Payment "%(payment)s" overpays line
"%(line)s".</field>
+ </record>
+ <record model="ir.message" id="msg_erase_party_pending_payment">
+ <field name="text">You cannot erase party "%(party)s" while they
have pending payments with company "%(company)s".</field>
+ </record>
+ <record model="ir.message" id="msg_pay_line_blocked">
+ <field name="text">Line "%(line)s" is blocked for payment.</field>
+ </record>
+ </data>
+</tryton>
diff -r 839a51daeeec -r b46797e3a313 party.py
--- a/party.py Mon Oct 01 13:10:07 2018 +0200
+++ b/party.py Sat Dec 29 14:20:28 2018 +0100
@@ -1,8 +1,10 @@
# 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.pool import PoolMeta, Pool
from trytond.model import ModelSQL, fields
from trytond.modules.company.model import CompanyValueMixin
+from trytond.modules.party.exceptions import EraseError
__all__ = ['Party', 'PartyPaymentDirectDebit', 'PartyReplace', 'PartyErase']
payment_direct_debit = fields.Boolean(
@@ -42,16 +44,6 @@
class PartyErase(metaclass=PoolMeta):
__name__ = 'party.erase'
- @classmethod
- def __setup__(cls):
- super(PartyErase, cls).__setup__()
- cls._error_messages.update({
- 'pending_payment': (
- 'The party "%(party)s" can not be erased '
- 'because he has pending payments '
- 'for the company "%(company)s".'),
- })
-
def check_erase_company(self, party, company):
pool = Pool()
Payment = pool.get('account.payment')
@@ -62,7 +54,7 @@
('state', 'not in', ['succeeded', 'failed']),
])
if payments:
- self.raise_user_error('pending_payment', {
- 'party': party.rec_name,
- 'company': company.rec_name,
- })
+ raise EraseError(
+ gettext('account_payment.msg_erase_party_pending_payment',
+ party=party.rec_name,
+ company=company.rec_name))
diff -r 839a51daeeec -r b46797e3a313 payment.py
--- a/payment.py Mon Oct 01 13:10:07 2018 +0200
+++ b/payment.py Sat Dec 29 14:20:28 2018 +0100
@@ -2,13 +2,17 @@
# this repository contains the full copyright notices and license terms.
from itertools import groupby
+from trytond.i18n import gettext
from trytond.model import Workflow, ModelView, ModelSQL, fields
+from trytond.model.exceptions import AccessError
from trytond.pyson import Eval, If
from trytond.rpc import RPC
from trytond.transaction import Transaction
from trytond.wizard import Wizard, StateView, StateAction, Button
from trytond.pool import Pool
+from .exceptions import OverpayWarning
+
__all__ = ['Journal', 'Group', 'Payment',
'ProcessPaymentStart', 'ProcessPayment']
@@ -183,10 +187,6 @@
def __setup__(cls):
super(Payment, cls).__setup__()
cls._order.insert(0, ('date', 'DESC'))
- cls._error_messages.update({
- 'delete_draft': ('Payment "%s" must be in draft before '
- 'deletion.'),
- })
cls._transitions |= set((
('draft', 'approved'),
('approved', 'processing'),
@@ -290,7 +290,9 @@
def delete(cls, payments):
for payment in payments:
if payment.state != 'draft':
- cls.raise_user_error('delete_draft', (payment.rec_name))
+ raise AccessError(
+ gettext('account_payment.msg_payment_delete_draft',
+ payment=payment.rec_name))
super(Payment, cls).delete(payments)
@classmethod
@@ -350,14 +352,6 @@
])
process = StateAction('account_payment.act_payment_group_form')
- @classmethod
- def __setup__(cls):
- super(ProcessPayment, cls).__setup__()
- cls._error_messages.update({
- 'overpay': 'The Payment "%(payment)s" overpays '
- 'the Line "%(line)s".',
- })
-
def _group_payment_key(self, payment):
return (('journal', payment.journal.id), ('kind', payment.kind))
@@ -369,15 +363,16 @@
def do_process(self, action):
pool = Pool()
Payment = pool.get('account.payment')
+ Warning = pool.get('res.user.warning')
payments = Payment.browse(Transaction().context['active_ids'])
for payment in payments:
if payment.line and payment.line.payment_amount < 0:
- self.raise_user_warning(str(payment),
- 'overpay', {
- 'payment': payment.rec_name,
- 'line': payment.line.rec_name,
- })
+ if Warning.check(str(payment)):
+ raise OverpayWarning(str(payment),
+ gettext('account_payment.msg_payment_overpay',
+ payment=payment.rec_name,
+ line=payment.line.rec_name))
groups = []
payments = sorted(payments, key=self._group_payment_key)
diff -r 839a51daeeec -r b46797e3a313 tests/scenario_account_payment.rst
--- a/tests/scenario_account_payment.rst Mon Oct 01 13:10:07 2018 +0200
+++ b/tests/scenario_account_payment.rst Sat Dec 29 14:20:28 2018 +0100
@@ -127,4 +127,4 @@
>>> pay_line.execute('start') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
- UserWarning: ...
+ BlockedWarning: ...
diff -r 839a51daeeec -r b46797e3a313 tryton.cfg
--- a/tryton.cfg Mon Oct 01 13:10:07 2018 +0200
+++ b/tryton.cfg Sat Dec 29 14:20:28 2018 +0100
@@ -13,3 +13,4 @@
payment.xml
account.xml
party.xml
+ message.xml