[FIX] models: Escape `%` characters from error messages of a failed import

To prevent susbtituion exception when creating use error message
e.g. No matching record found for external id 'fo%o' in field 'Bar'
Fixes #5933
This commit is contained in:
Mohammad Alhashash 2015-03-25 10:54:16 +02:00 committed by Martin Trigaux
parent 20dcea3be3
commit 83282f2dea
1 changed files with 5 additions and 2 deletions

View File

@ -1199,8 +1199,11 @@ class BaseModel(object):
type = 'warning' if isinstance(exception, Warning) else 'error'
# logs the logical (not human-readable) field name for automated
# processing of response, but injects human readable in message
record = dict(base, type=type, field=field,
message=unicode(exception.args[0]) % base)
# Before applying string substitution, we need to escape all '%'
# characters that are not part of substitutable reference.
regex = '%%(?!\((%s)\)s)' % '|'.join(base.keys())
message = unicode(re.sub(regex, '%%', exception.args[0])) % base
record = dict(base, type=type, field=field, message=message)
if len(exception.args) > 1 and exception.args[1]:
record.update(exception.args[1])
log(record)