From 24bb7c20813b9cfed856245b850b4f0d63ed565f 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 | 7 +++++++ message.xml | 22 ++++++++++++++++++++++ timesheet.py | 16 ++++++---------- tryton.cfg | 1 + work.py | 36 ++++++++++++++++++++++++------------ 5 files changed, 60 insertions(+), 22 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..c281451a60 --- /dev/null +++ b/exceptions.py @@ -0,0 +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.exceptions import UserError + + +class InvoicingError(UserError): + pass diff --git a/message.xml b/message.xml new file mode 100644 index 0000000000..f1ffaed84a --- /dev/null +++ b/message.xml @@ -0,0 +1,22 @@ + + + + + + You cannot modify an invoiced line. + + + You cannot delete an invoiced line. + + + There is no product on work "%(work)s". + + + There is no list price on work "%(work)s". + + + There is no party on work "%(work)s". + + + diff --git a/timesheet.py b/timesheet.py index 2e99544a8b..3ae71b0820 100644 --- a/timesheet.py +++ b/timesheet.py @@ -1,6 +1,8 @@ # 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.model import fields +from trytond.model.exceptions import AccessError from trytond.pool import PoolMeta @@ -12,14 +14,6 @@ class TimesheetLine(metaclass=PoolMeta): invoice_line = fields.Many2One('account.invoice.line', 'Invoice Line', readonly=True) - @classmethod - def __setup__(cls): - super(TimesheetLine, cls).__setup__() - cls._error_messages.update({ - 'modify_invoiced_line': 'You can not modify invoiced line.', - 'delete_invoiced_line': 'You can not delete invoiced line.', - }) - @classmethod def copy(cls, records, default=None): if default is None: @@ -35,11 +29,13 @@ class TimesheetLine(metaclass=PoolMeta): for lines, values in zip(actions, actions): if (('duration' in values or 'work' in values) and any(l.invoice_line for l in lines)): - cls.raise_user_error('modify_invoiced_line') + raise AccessError( + gettext('project_invoice.msg_modify_invoiced_line')) super(TimesheetLine, cls).write(*args) @classmethod def delete(cls, records): if any(r.invoice_line for r in records): - cls.raise_user_error('delete_invoiced_line') + raise AccessError( + gettext('project_invoice.msg_delete_invoiced_line')) super(TimesheetLine, cls).delete(records) diff --git a/tryton.cfg b/tryton.cfg index ed6307f0e8..769f76f53e 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -12,3 +12,4 @@ xml: project.xml work.xml timesheet.xml + message.xml diff --git a/work.py b/work.py index aacfad497a..fcc41a2f88 100644 --- a/work.py +++ b/work.py @@ -11,6 +11,7 @@ from sql import Null from sql.aggregate import Sum from sql.operators import Concat +from trytond.i18n import gettext from trytond.model import ModelSQL, ModelView, fields from trytond.pool import PoolMeta from trytond.pyson import Eval, Bool, PYSONEncoder @@ -19,6 +20,8 @@ from trytond.transaction import Transaction from trytond.wizard import Wizard, StateAction from trytond.tools import reduce_ids, grouped_slice +from .exceptions import InvoicingError + __all__ = ['Work', 'WorkInvoicedProgress', 'OpenInvoice'] @@ -79,11 +82,6 @@ class Work(metaclass=PoolMeta): 'duration_to_invoice'] }, }) - cls._error_messages.update({ - 'missing_product': 'There is no product on work "%s".', - 'missing_list_price': 'There is no list price on work "%s".', - 'missing_party': 'There is no party on work "%s".', - }) @staticmethod def default_project_invoice_method(): @@ -407,7 +405,9 @@ class Work(metaclass=PoolMeta): journal = None if not self.party: - self.raise_user_error('missing_party', (self.rec_name,)) + raise InvoicingError( + gettext('project_invoice.msg_missing_party', + work=self.rec_name)) return Invoice( company=self.company, @@ -485,9 +485,13 @@ class Work(metaclass=PoolMeta): and self.effort_hours and self.state == 'done'): if not self.product: - self.raise_user_error('missing_product', (self.rec_name,)) + raise InvoicingError( + gettext('project_invoice.msg_missing_product', + work=self.rec_name)) elif self.list_price is None: - self.raise_user_error('missing_list_price', (self.rec_name,)) + raise InvoicingError( + gettext('project_invoice.msg_missing_list_price', + work=self.rec_name)) return [{ 'product': self.product, 'quantity': self.effort_hours, @@ -516,9 +520,13 @@ class Work(metaclass=PoolMeta): hour, quantity, self.product.default_uom) if quantity > 0: if not self.product: - self.raise_user_error('missing_product', (self.rec_name,)) + raise InvoicingError( + gettext('project_invoice.msg_missing_product', + work=self.rec_name)) elif self.list_price is None: - self.raise_user_error('missing_list_price', (self.rec_name,)) + raise InvoicingError( + gettext('project_invoice.msg_missing_list_price', + work=self.rec_name)) invoiced_progress = InvoicedProgress(work=self, effort_duration=datetime.timedelta(hours=quantity)) return [{ @@ -540,9 +548,13 @@ class Work(metaclass=PoolMeta): if (self.timesheet_works and any(tw.timesheet_lines for tw in self.timesheet_works)): if not self.product: - self.raise_user_error('missing_product', (self.rec_name,)) + raise InvoicingError( + gettext('project_invoice.msg_missing_product', + work=self.rec_name)) elif self.list_price is None: - self.raise_user_error('missing_list_price', (self.rec_name,)) + raise InvoicingError( + gettext('project_invoice.msg_missing_list_price', + work=self.rec_name)) return [{ 'product': self.product, 'quantity': l.hours,