From fd8883bc3dd0a5c30211462de3c998709f02b2dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 29 Dec 2018 14:20:28 +0100 Subject: [PATCH] Add ir.message and use custom exceptions issue3672 --- 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(-) create mode 100644 exceptions.py create mode 100644 message.xml diff --git a/account.py b/account.py index 7729564d26..00127874c4 100644 --- a/account.py +++ b/account.py @@ -8,6 +8,7 @@ from sql.conditionals import Case, Coalesce 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.transaction import Transaction 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 @@ class PayLine(Wizard): ]) 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 @@ class PayLine(Wizard): 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 @@ class PayLine(Wizard): 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 --git a/exceptions.py b/exceptions.py new file mode 100644 index 0000000000..bd9bb8758b --- /dev/null +++ b/exceptions.py @@ -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 --git a/message.xml b/message.xml new file mode 100644 index 0000000000..dc4aa73573 --- /dev/null +++ b/message.xml @@ -0,0 +1,19 @@ + + + + + + To delete payment "%(payment)s" you must reset it to draft state. + + + Payment "%(payment)s" overpays line "%(line)s". + + + You cannot erase party "%(party)s" while they have pending payments with company "%(company)s". + + + Line "%(line)s" is blocked for payment. + + + diff --git a/party.py b/party.py index 20d36113a0..776bde854f 100644 --- a/party.py +++ b/party.py @@ -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 PartyReplace(metaclass=PoolMeta): 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 @@ class PartyErase(metaclass=PoolMeta): ('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 --git a/payment.py b/payment.py index f50e3682ff..efe2a9a7be 100644 --- a/payment.py +++ b/payment.py @@ -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 @@ class Payment(Workflow, ModelSQL, ModelView): 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 @@ class Payment(Workflow, ModelSQL, ModelView): 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 @@ class ProcessPayment(Wizard): ]) 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 @@ class ProcessPayment(Wizard): 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 --git a/tests/scenario_account_payment.rst b/tests/scenario_account_payment.rst index 3dcc3deade..e4d835a819 100644 --- a/tests/scenario_account_payment.rst +++ b/tests/scenario_account_payment.rst @@ -127,4 +127,4 @@ Try to pay blocked line:: >>> pay_line.execute('start') # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... - UserWarning: ... + BlockedWarning: ... diff --git a/tryton.cfg b/tryton.cfg index 213939761d..0f58300b80 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -13,3 +13,4 @@ xml: payment.xml account.xml party.xml + message.xml