2015-12-02 06:59:58 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.utils.functional import SimpleLazyObject
|
2016-08-16 18:07:03 +08:00
|
|
|
from django.utils.text import format_lazy
|
2015-12-02 06:59:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
def prefix_validation_error(error, prefix, code, params):
|
|
|
|
"""
|
|
|
|
Prefix a validation error message while maintaining the existing
|
|
|
|
validation data structure.
|
|
|
|
"""
|
|
|
|
if error.error_list == [error]:
|
|
|
|
error_params = error.params or {}
|
|
|
|
return ValidationError(
|
|
|
|
# We can't simply concatenate messages since they might require
|
|
|
|
# their associated parameters to be expressed correctly which
|
2016-08-16 18:07:03 +08:00
|
|
|
# is not something `format_lazy` does. For example, proxied
|
2017-01-27 03:58:33 +08:00
|
|
|
# ngettext calls require a count parameter and are converted
|
2015-12-02 06:59:58 +08:00
|
|
|
# to an empty string if they are missing it.
|
2016-08-16 18:07:03 +08:00
|
|
|
message=format_lazy(
|
|
|
|
'{}{}',
|
2015-12-02 06:59:58 +08:00
|
|
|
SimpleLazyObject(lambda: prefix % params),
|
|
|
|
SimpleLazyObject(lambda: error.message % error_params),
|
|
|
|
),
|
|
|
|
code=code,
|
2017-12-11 20:08:45 +08:00
|
|
|
params={**error_params, **params},
|
2015-12-02 06:59:58 +08:00
|
|
|
)
|
|
|
|
return ValidationError([
|
|
|
|
prefix_validation_error(e, prefix, code, params) for e in error.error_list
|
|
|
|
])
|