Add ir.message and use custom exceptions

issue3672
This commit is contained in:
Cédric Krier committed 2018-12-29 14:20:29 +01:00
1 parent 7a237ddff3
commit 4707fabdf8
4 files changed
+40 -30

No files matched your search

+8
View File
@@ -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
+16
View File
@@ -0,0 +1,16 @@
<?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_missing_work_center">
<field name="text">Could not find a work center of category "%(category)s" under "%(parent)s".</field>
</record>
<record model="ir.message" id="msg_delete_request">
<field name="text">To delete work "%(work)s" it must be in the "request" or "draft" state.</field>
</record>
<record model="ir.message" id="msg_do_finished_work">
<field name="text">To complete production "%(production)s", work "%(work)s" must be finished.</field>
</record>
</data>
</tryton>
+6 -13
View File
@@ -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)
+10 -17
View File
@@ -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)