diff --git a/exceptions.py b/exceptions.py new file mode 100644 index 0000000000..ef503d5730 --- /dev/null +++ b/exceptions.py @@ -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 diff --git a/line.py b/line.py index b3cfe17429..6e8c39fab4 100644 --- a/line.py +++ b/line.py @@ -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): diff --git a/message.xml b/message.xml new file mode 100644 index 0000000000..49459b83ab --- /dev/null +++ b/message.xml @@ -0,0 +1,19 @@ + + + + + + The companies associated with work "%(work)s" and its origin differ. + + + The duration of line "%(line)s" must be positive. + + + Work origin must be unique by company. + + + The UUID of timesheet line must be unique. + + + diff --git a/tryton.cfg b/tryton.cfg index 922b0026a0..0b160bdcfe 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -9,3 +9,4 @@ xml: timesheet.xml work.xml line.xml + message.xml diff --git a/work.py b/work.py index 11192ee7ca..c87935d485 100644 --- a/work.py +++ b/work.py @@ -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