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 573cabd6b1
commit 18b20cfabb
5 files changed
+43 -33

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.model.exceptions import ValidationError
class WorkValidationError(ValidationError):
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_work_invalid_parent_state">
<field name="text">To open work "%(child)s", you must open its parent "%(parent)s".</field>
</record>
<record model="ir.message" id="msg_work_invalid_children_state">
<field name="text">To close work "%(parent)s", you must close its child "%(child)s".</field>
</record>
<record model="ir.message" id="msg_erase_party_opened_project">
<field name="text">You cannot erase party "%(party)s" while they have opened projects with company "%(company)s".</field>
</record>
</data>
</tryton>
+7 -14
View File
@@ -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({
'opened_project': (
'The party "%(party)s" can not be erased '
'because he has opened projects '
'for the company "%(company)s".'),
})
def check_erase_company(self, party, company):
pool = Pool()
Work = pool.get('project.work')
@@ -38,7 +31,7 @@ class PartyErase(metaclass=PoolMeta):
('state', '!=', 'done'),
])
if works:
self.raise_user_error('opened_project', {
'party': party.rec_name,
'company': company.rec_name,
})
raise EraseError(
gettext('project.msg_erase_party_opened_project',
party=party.rec_name,
company=company.rec_name))
+1
View File
@@ -9,3 +9,4 @@ xml:
project.xml
work.xml
timesheet.xml
message.xml
+11 -19
View File
@@ -5,12 +5,15 @@ import datetime
from sql import Null
from trytond.i18n import gettext
from trytond.model import ModelView, ModelSQL, fields, sequence_ordered, tree
from trytond.pyson import Eval
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.tools import reduce_ids, grouped_slice
from .exceptions import WorkValidationError
__all__ = ['Work']
@@ -195,17 +198,6 @@ class Work(sequence_ordered(), tree(separator='\\'), ModelSQL, ModelView):
where=timesheet.id == work_id))
table_project_work.drop_column('work')
@classmethod
def __setup__(cls):
super(Work, cls).__setup__()
cls._error_messages.update({
'invalid_parent_state': ('Work "%(child)s" can not be opened '
'because its parent work "%(parent)s" is already done.'),
'invalid_children_state': ('Work "%(parent)s" can not be '
'done because its child work "%(child)s" is still '
'opened.'),
})
@classmethod
def index_set_field(cls, name):
index = super(Work, cls).index_set_field(name)
@@ -222,17 +214,17 @@ class Work(sequence_ordered(), tree(separator='\\'), ModelSQL, ModelView):
def check_state(self):
if (self.state == 'opened'
and (self.parent and self.parent.state == 'done')):
self.raise_user_error('invalid_parent_state', {
'child': self.rec_name,
'parent': self.parent.rec_name,
})
raise WorkValidationError(
gettext('project.msg_work_invalid_parent_state',
child=self.rec_name,
parent=self.parent.rec_name))
if self.state == 'done':
for child in self.children:
if child.state == 'opened':
self.raise_user_error('invalid_children_state', {
'parent': self.rec_name,
'child': child.rec_name,
})
raise WorkValidationError(
gettext('project.msg_work_invalid_children_state',
parent=self.rec_name,
child=child.rec_name))
@property
def effort_hours(self):