Add ir.message and use custom exceptions

issue3672
This commit is contained in:
Cédric Krier
2018-12-29 14:20:30 +01:00
parent 74dba7e53b
commit f940e07313
5 changed files with 46 additions and 16 deletions
+12
View File
@@ -0,0 +1,12 @@
# 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 CompanyValidationError(ValidationError):
pass
class DurationValidationError(ValidationError):
pass
+7 -8
View File
@@ -7,12 +7,15 @@ from sql import Literal
from sql.aggregate import Max, Sum
from sql.functions import Extract, CharLength
from trytond.i18n import gettext
from trytond.model import ModelView, ModelSQL, fields, Unique
from trytond.wizard import Wizard, StateView, StateAction, Button
from trytond.pyson import Eval, PYSONEncoder, Date
from trytond.transaction import Transaction
from trytond.pool import Pool
from .exceptions import DurationValidationError
__all__ = ['Line', 'EnterLinesStart', 'EnterLines',
'HoursEmployee', 'HoursEmployeeContext',
'HoursEmployeeWeekly', 'HoursEmployeeMonthly']
@@ -64,13 +67,9 @@ class Line(ModelSQL, ModelView):
t = cls.__table__()
cls._sql_constraints = [
('uuid_unique', Unique(t, t.uuid),
"The UUID of timesheet line must be unique."),
'timesheet.msg_line_uuid_unique'),
]
cls._order.insert(0, ('date', 'DESC'))
cls._error_messages.update({
'duration_positive': (
'Duration of line "%(line)s" must be positive.'),
})
@classmethod
def __register__(cls, module_name):
@@ -134,9 +133,9 @@ class Line(ModelSQL, ModelView):
def check_duration(self):
if self.duration < datetime.timedelta():
self.raise_user_error('duration_positive', {
'line': self.rec_name,
})
raise DurationValidationError(
gettext('timesheet.msg_line_duration_positive',
line=self.rec_name))
@property
def hours(self):
+19
View File
@@ -0,0 +1,19 @@
<?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_company_different_origin">
<field name="text">The companies associated with work "%(work)s" and its origin differ.</field>
</record>
<record model="ir.message" id="msg_line_duration_positive">
<field name="text">The duration of line "%(line)s" must be positive.</field>
</record>
<record model="ir.message" id="msg_work_origin_unique_company">
<field name="text">Work origin must be unique by company.</field>
</record>
<record model="ir.message" id="msg_line_uuid_unique">
<field name="text">The UUID of timesheet line must be unique.</field>
</record>
</data>
</tryton>
+1
View File
@@ -9,3 +9,4 @@ xml:
timesheet.xml
work.xml
line.xml
message.xml
+7 -8
View File
@@ -6,12 +6,15 @@ import datetime
from sql import Literal
from sql.aggregate import Sum
from trytond.i18n import gettext
from trytond.model import ModelView, ModelSQL, DeactivableMixin, fields, Unique
from trytond.pyson import Not, Bool, Eval, If
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.tools import reduce_ids, grouped_slice
from .exceptions import CompanyValidationError
__all__ = ['Work', 'WorkContext']
@@ -70,12 +73,8 @@ class Work(DeactivableMixin, ModelSQL, ModelView):
t = cls.__table__()
cls._sql_constraints += [
('origin_unique', Unique(t, t.origin, t.company),
'The origin must be unique per company.'),
'timesheet.msg_work_origin_unique_company'),
]
cls._error_messages.update({
'mismatch_company': ('The company of the work "%(work)s" '
'is different than the origin\'s company'),
})
@classmethod
def __register__(cls, module_name):
@@ -204,9 +203,9 @@ class Work(DeactivableMixin, ModelSQL, ModelView):
super(Work, cls).validate(works)
for work in works:
if work.origin and not work._validate_company():
cls.raise_user_error('mismatch_company', {
'work': work.rec_name,
})
raise CompanyValidationError(
gettext('timesheet.msg_work_company_different_origin',
work=work.rec_name))
def _validate_company(self):
return True