From b3eafebeb48980e1a0d378c0de764350be90abde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 29 Dec 2018 14:20:29 +0100 Subject: [PATCH] Add ir.message and use custom exceptions issue3672 --- exceptions.py | 13 +++++++++++++ message.xml | 22 ++++++++++++++++++++++ party.py | 22 ++++++++-------------- recurrence.py | 32 ++++++++++---------------------- subscription.py | 37 ++++++++++++++++++++----------------- tryton.cfg | 1 + 6 files changed, 74 insertions(+), 53 deletions(-) create mode 100644 exceptions.py create mode 100644 message.xml diff --git a/exceptions.py b/exceptions.py new file mode 100644 index 0000000000..b3f38557ab --- /dev/null +++ b/exceptions.py @@ -0,0 +1,13 @@ +# 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 +from trytond.model.exceptions import ValidationError + + +class InvoiceError(UserError): + pass + + +class RecurrenceRuleValidationError(ValidationError): + pass diff --git a/message.xml b/message.xml new file mode 100644 index 0000000000..706603860c --- /dev/null +++ b/message.xml @@ -0,0 +1,22 @@ + + + + + + You cannot modify invoiced consumption "%(consumption)s". + + + You cannot delete invoiced consumption "%(consumption)s". + + + To invoice consumptions you must define an account revenue for product "%(product)s". + + + You cannot erase party "%(party)s" while they have pending subscriptions with company "%(company)s". + + + Invalid "%(field)s" "%(value)s" in recurence rule "%(recurrence_rule)s" with exception "%(exception)s". + + + diff --git a/party.py b/party.py index fd143dfaae..0554d94b33 100644 --- a/party.py +++ b/party.py @@ -1,7 +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.modules.party.exceptions import EraseError + __all__ = ['PartyReplace', 'PartyErase'] @@ -18,16 +21,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_subscription': ( - 'The party "%(party)s" can not be erased ' - 'because he has pending subscriptions ' - 'for the company "%(company)s".'), - }) - def check_erase_company(self, party, company): pool = Pool() Subscription = pool.get('sale.subscription') @@ -38,7 +31,8 @@ class PartyErase(metaclass=PoolMeta): ('state', 'not in', ['closed', 'canceled']), ]) if subscriptions: - self.raise_user_error('pending_subscription', { - 'party': party.rec_name, - 'company': company.rec_name, - }) + raise EraseError( + gettext('sale_subscription' + '.msg_erase_party_pending_subscription', + party=party.rec_name, + company=company.rec_name)) diff --git a/recurrence.py b/recurrence.py index 600cb177a6..b595e43113 100644 --- a/recurrence.py +++ b/recurrence.py @@ -4,9 +4,12 @@ from dateutil.rrule import MO, TU, WE, TH, FR, SA, SU from dateutil.rrule import YEARLY, MONTHLY, WEEKLY, DAILY from dateutil.rrule import rrule, rruleset +from trytond.i18n import gettext from trytond.model import ModelSQL, ModelView, fields from trytond.transaction import Transaction +from .exceptions import RecurrenceRuleValidationError + __all__ = ['RecurrenceRuleSet', 'RecurrenceRule'] WEEKDAYS = { 'MO': MO, @@ -100,24 +103,6 @@ class RecurrenceRule(ModelSQL, ModelView): exclusive = fields.Boolean("Exclusive") - @classmethod - def __setup__(cls): - super(RecurrenceRule, cls).__setup__() - cls._error_messages.update({ - 'invalid_byweekday': ( - '"By Week Day" (%(byweekday)s) is not valid.'), - 'invalid_bymonthday': ( - '"By Month Day" (%(bymonthday)s) is not valid.'), - 'invalid_byyearday': ( - '"By Year Day" (%(byyearday)s) is not valid.'), - 'invalid_byweekno': ( - '"By Week Number" (%(byweekno)s) is not valid.'), - 'invalid_bymonth': ( - '"By Month" (%(bymonth)s) is not valid.'), - 'invalid_bysetpos': ( - '"By Position" (%(bysetpos)s) is not valid.'), - }) - @classmethod def default_interval(cls): return 1 @@ -214,7 +199,10 @@ class RecurrenceRule(ModelSQL, ModelView): def check_by(self, name): try: getattr(self, '_%s' % name) - except ValueError: - self.raise_user_error('invalid_%s' % name, { - name: getattr(self, name), - }) + except ValueError as exception: + raise RecurrenceRuleValidationError( + gettext('sale_subscription.msg_recurrence_rule_invalid_by', + value=getattr(self, name), + recurrence_rule=self.rec_name, + exception=exception, + **self._get_error_args(name))) from exception diff --git a/subscription.py b/subscription.py index e5576129f6..4bd0566bc3 100644 --- a/subscription.py +++ b/subscription.py @@ -8,8 +8,10 @@ from sql import operators, Literal, Null from sql.conditionals import Coalesce from trytond import backend +from trytond.i18n import gettext from trytond.model import ModelSQL, ModelView, Workflow, fields, \ sequence_ordered +from trytond.model.exceptions import AccessError from trytond.pool import Pool from trytond.pyson import Eval, If, Bool from trytond.transaction import Transaction @@ -17,6 +19,7 @@ from trytond.wizard import Wizard, StateView, StateAction, StateTransition, \ Button from trytond.modules.product import price_digits +from .exceptions import InvoiceError __all__ = ['Subscription', 'Line', 'LineConsumption', 'CreateLineConsumption', 'CreateLineConsumptionStart', @@ -726,14 +729,6 @@ class LineConsumption(ModelSQL, ModelView): def __setup__(cls): super(LineConsumption, cls).__setup__() cls._order.insert(0, ('date', 'DESC')) - cls._error_messages.update({ - 'modify_invoiced_consumption': ( - "You can not modify invoiced consumption."), - 'delete_invoiced_consumption': ( - "You can not delete invoiced consumption."), - 'missing_account_revenue': ('Product "%(product)s" ' - 'misses a revenue account.'), - }) @fields.depends('line') def on_change_with_unit_digits(self, name=None): @@ -752,16 +747,23 @@ class LineConsumption(ModelSQL, ModelView): @classmethod def write(cls, *args): - if any(c.invoice_line - for consumptions in args[::2] - for c in consumptions): - cls.raise_user_error('modify_invoiced_consumption') + for consumptions in args[::2]: + for consumption in consumptions: + if consumption.invoice_line: + raise AccessError( + gettext('sale_subscription' + '.msg_consumption_modify_invoiced', + consumption=consumption.rec_name)) super(LineConsumption, cls).write(*args) @classmethod def delete(cls, consumptions): - if any(c.invoice_line for c in consumptions): - cls.raise_user_error('delete_invoiced_consumption') + for consumption in consumptions: + if consumption.invoice_line: + raise AccessError( + gettext('sale_subscription' + '.msg_consumption_modify_invoiced', + consumption=consumption.rec_name)) super(LineConsumption, cls).delete(consumptions) @classmethod @@ -782,9 +784,10 @@ class LineConsumption(ModelSQL, ModelView): line.account = line.product.account_revenue_used if not line.account: - cls.raise_user_error('missing_account_revenue', { - 'product': line.product.rec_name, - }) + raise InvoiceError( + gettext('sale_subscription' + '.msg_consumption_invoice_missing_account_revenue', + product=line.product.rec_name)) taxes = [] pattern = line._get_tax_rule_pattern() diff --git a/tryton.cfg b/tryton.cfg index 220e323c4b..d7f506adfa 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -14,3 +14,4 @@ xml: recurrence.xml service.xml subscription.xml + message.xml