From 4707fabdf8b37368b9c8a90ff4ae0cec08b4916f 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 | 8 ++++++++ message.xml | 16 ++++++++++++++++ production.py | 19 ++++++------------- work.py | 27 ++++++++++----------------- 4 files changed, 40 insertions(+), 30 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..02315e8fa1 --- /dev/null +++ b/exceptions.py @@ -0,0 +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.exceptions import UserError + + +class PickerError(UserError): + pass diff --git a/message.xml b/message.xml new file mode 100644 index 0000000000..d1d1782580 --- /dev/null +++ b/message.xml @@ -0,0 +1,16 @@ + + + + + + Could not find a work center of category "%(category)s" under "%(parent)s". + + + To delete work "%(work)s" it must be in the "request" or "draft" state. + + + To complete production "%(production)s", work "%(work)s" must be finished. + + + diff --git a/production.py b/production.py index e6f960d7eb..da70d57396 100644 --- a/production.py +++ b/production.py @@ -4,8 +4,10 @@ from decimal import Decimal from sql.aggregate import Sum +from trytond.i18n import gettext from trytond.pool import PoolMeta, Pool from trytond.model import ModelView, Workflow, fields +from trytond.model.exceptions import AccessError from trytond.pyson import Eval, Bool from trytond.transaction import Transaction @@ -36,15 +38,6 @@ class Production(metaclass=PoolMeta): ], depends=['state', 'company']) - @classmethod - def __setup__(cls): - super(Production, cls).__setup__() - cls._error_messages.update({ - 'do_finished_work': ('Production "%(production)s" ' - 'can not be done ' - 'because work "%(work)s" is not finished.'), - }) - def get_cost(self, name): pool = Pool() Work = pool.get('production.work') @@ -134,8 +127,8 @@ class Production(metaclass=PoolMeta): for production in productions: for work in production.works: if work.state != 'finished': - cls.raise_user_error('do_finished_work', { - 'production': production.rec_name, - 'work': work.rec_name, - }) + raise AccessError( + gettext('production_work.msg_do_finished_work', + production=production.rec_name, + work=work.rec_name)) super(Production, cls).done(productions) diff --git a/work.py b/work.py index c29530664f..33bbc88270 100644 --- a/work.py +++ b/work.py @@ -5,14 +5,17 @@ import random from decimal import Decimal from functools import wraps +from trytond.i18n import gettext from trytond.model import ( ModelSQL, ModelView, Workflow, DeactivableMixin, fields, sequence_ordered, tree) +from trytond.model.exceptions import AccessError from trytond.pool import Pool from trytond.pyson import Eval, If, Bool from trytond.transaction import Transaction from trytond.modules.product import price_digits +from .exceptions import PickerError __all__ = ['WorkCenterCategory', 'WorkCenter', 'Work', 'WorkCycle'] @@ -64,10 +67,6 @@ class WorkCenter(DeactivableMixin, tree(separator=' / '), ModelSQL, ModelView): @classmethod def __setup__(cls): super(WorkCenter, cls).__setup__() - cls._error_messages.update({ - 'missing_work_center': ('Could not find a work center ' - 'of category "%(category)s" under "%(parent)s".'), - }) cls._order.insert(0, ('name', 'ASC')) @classmethod @@ -95,10 +94,10 @@ class WorkCenter(DeactivableMixin, tree(separator=' / '), ModelSQL, ModelView): ('category', '=', category.id), ]) if not work_centers: - cls.raise_user_error('missing_work_center', { - 'category': category.rec_name, - 'parent': parent.rec_name, - }) + raise PickerError( + gettext('production_work.msg_missing_work_center', + category=category.rec_name, + parent=parent.rec_name)) cache[key] = work_centers return random.choice(cache[key]) return picker @@ -146,14 +145,6 @@ class Work(sequence_ordered(), ModelSQL, ModelView): ('done', 'Done'), ], 'State', select=True, readonly=True) - @classmethod - def __setup__(cls): - super(Work, cls).__setup__() - cls._error_messages.update({ - 'delete_request': ('Work "%s" can not be deleted ' - 'as it is not in state "request"'), - }) - @fields.depends('operation') def on_change_with_work_center_category(self, name=None): if self.operation and self.operation.work_center_category: @@ -218,7 +209,9 @@ class Work(sequence_ordered(), ModelSQL, ModelView): def delete(cls, works): for work in works: if work.state not in {'request', 'draft'}: - cls.raise_user_error('delete_request', work.rec_name) + raise AccessError( + gettext('production_work.msg_delete_request', + work=work.rec_name)) super(Work, cls).delete(works)