Files
tryton/trytond/error.py
T

109 lines
4.2 KiB
Python
Raw Normal View History

2014-12-02 12:54:01 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2011-03-08 18:44:35 +01:00
from trytond.transaction import Transaction
2011-07-03 13:20:58 +02:00
from trytond.pool import Pool
2011-09-05 20:30:26 +02:00
from trytond.exceptions import UserError, UserWarning
2011-03-08 18:44:35 +01:00
class WarningErrorMixin(object):
2013-09-06 22:32:37 +02:00
@classmethod
def _get_error_messages(cls):
return cls._error_messages.values()
2012-09-24 22:16:53 +02:00
@classmethod
def raise_user_error(cls, error, error_args=None,
2011-03-08 18:44:35 +01:00
error_description='', error_description_args=None,
raise_exception=True):
'''
Raise an exception that will be displayed as an error message
in the client.
:param error: the key of the dictionary _error_messages used
for error message
:param error_args: the arguments that will be used
for "%"-based substitution
:param error_description: the key of the dictionary
_error_messages used for error description
:param error_description_args: the arguments that will be used
for "%"-based substitution
:param raise_exception: if set to False return the error string
(or tuple if error_description is not empty) instead of raising an
exception.
'''
2012-09-24 22:16:53 +02:00
Translation = Pool().get('ir.translation')
2011-03-08 18:44:35 +01:00
2012-09-24 22:16:53 +02:00
error = cls._error_messages.get(error, error)
2011-03-08 18:44:35 +01:00
2012-01-27 21:11:06 +01:00
language = Transaction().language
2012-09-24 22:16:53 +02:00
res = Translation.get_source(cls.__name__, 'error', language, error)
2012-03-06 12:29:28 +01:00
if not res:
2012-09-24 22:16:53 +02:00
res = Translation.get_source(error, 'error', language)
2011-03-08 18:44:35 +01:00
if res:
error = res
2013-09-21 00:34:15 +02:00
if error_args is not None:
2011-03-08 18:44:35 +01:00
try:
error = error % error_args
except TypeError:
pass
if error_description:
2012-09-24 22:16:53 +02:00
error_description = cls._error_messages.get(error_description,
2011-03-08 18:44:35 +01:00
error_description)
2012-09-24 22:16:53 +02:00
res = Translation.get_source(cls.__name__, 'error', language,
error_description)
2012-03-06 12:29:28 +01:00
if not res:
2012-09-24 22:16:53 +02:00
res = Translation.get_source(error_description, 'error',
2012-03-06 12:29:28 +01:00
language)
2011-03-08 18:44:35 +01:00
if res:
error_description = res
if error_description_args:
try:
2013-02-27 22:50:31 +01:00
error_description = (error_description
% error_description_args)
2011-03-08 18:44:35 +01:00
except TypeError:
pass
if raise_exception:
2011-09-05 20:30:26 +02:00
raise UserError(error, error_description)
2011-03-08 18:44:35 +01:00
else:
return (error, error_description)
if raise_exception:
2011-09-05 20:30:26 +02:00
raise UserError(error)
2011-03-08 18:44:35 +01:00
else:
return error
2012-09-24 22:16:53 +02:00
@classmethod
def raise_user_warning(cls, warning_name, warning,
2011-03-08 18:44:35 +01:00
warning_args=None, warning_description='',
warning_description_args=None):
'''
Raise an exception that will be displayed as a warning message
in the client, if the user has not yet bypassed it.
:param warning_name: the unique warning name
:param warning: the key of the dictionary _error_messages used
for warning message
:param warning_args: the arguments that will be used for
"%"-based substitution
:param warning_description: the key of the dictionary
_error_messages used for warning description
:param warning_description_args: the arguments that will be used
for "%"-based substitution
'''
2012-09-24 22:16:53 +02:00
Warning_ = Pool().get('res.user.warning')
if Warning_.check(warning_name):
2011-03-08 18:44:35 +01:00
if warning_description:
2012-09-24 22:16:53 +02:00
warning, warning_description = cls.raise_user_error(warning,
2011-03-08 18:44:35 +01:00
error_args=warning_args,
error_description=warning_description,
error_description_args=warning_description_args,
raise_exception=False)
2011-09-05 20:30:26 +02:00
raise UserWarning(warning_name, warning, warning_description)
2011-03-08 18:44:35 +01:00
else:
2012-09-24 22:16:53 +02:00
warning = cls.raise_user_error(warning,
2011-03-08 18:44:35 +01:00
error_args=warning_args, raise_exception=False)
2011-09-05 20:30:26 +02:00
raise UserWarning(warning_name, warning)