summaryrefslogtreecommitdiff
path: root/django/contrib/postgres/utils.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2015-12-01 17:59:58 -0500
committerSimon Charette <charette.s@gmail.com>2015-12-17 20:25:04 -0500
commit3738e4ac46688a0f13139c0b9058fc81c1aac424 (patch)
tree37d6d9f541cbc86fd55f9f665240cf17eccbec00 /django/contrib/postgres/utils.py
parent86eccdc8b67728d84440a46e5bf62c78f2eddf6d (diff)
downloaddjango-3738e4ac46688a0f13139c0b9058fc81c1aac424.tar.gz
Fixed #25841 -- Handled base array fields validation errors with params.
Thanks to Trac alias benzid-wael for the report.
Diffstat (limited to 'django/contrib/postgres/utils.py')
-rw-r--r--django/contrib/postgres/utils.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/django/contrib/postgres/utils.py b/django/contrib/postgres/utils.py
new file mode 100644
index 0000000000..1563149c7e
--- /dev/null
+++ b/django/contrib/postgres/utils.py
@@ -0,0 +1,30 @@
+from __future__ import unicode_literals
+
+from django.core.exceptions import ValidationError
+from django.utils.functional import SimpleLazyObject
+from django.utils.translation import string_concat
+
+
+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
+ # is not something `string_concat` does. For example, proxied
+ # ungettext calls require a count parameter and are converted
+ # to an empty string if they are missing it.
+ message=string_concat(
+ SimpleLazyObject(lambda: prefix % params),
+ SimpleLazyObject(lambda: error.message % error_params),
+ ),
+ code=code,
+ params=dict(error_params, **params),
+ )
+ return ValidationError([
+ prefix_validation_error(e, prefix, code, params) for e in error.error_list
+ ])