Skip to content

Commit

Permalink
[#3865] schema.py: use get_validator; use unicode_safe
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Oct 13, 2017
1 parent 215b5ef commit cb8dd57
Show file tree
Hide file tree
Showing 2 changed files with 381 additions and 285 deletions.
38 changes: 37 additions & 1 deletion ckan/lib/navl/validators.py
Expand Up @@ -2,7 +2,7 @@

import ckan.lib.navl.dictization_functions as df

from ckan.common import _
from ckan.common import _, json

missing = df.missing
StopOnError = df.StopOnError
Expand Down Expand Up @@ -123,3 +123,39 @@ def unicode_only(value):
if not isinstance(value, unicode):
raise Invalid(_('Must be a Unicode string value'))
return value

def unicode_safe(value):
'''
Make sure value passed is treated as unicode, but don't raise
an error if it's not, just make a reasonable attempt to
convert other types passed.
This validator is a safer alternative to the old ckan idiom
of using the unicode() function as a validator. It tries
not to pollute values with Python repr garbage e.g. when passed
a list of strings (uses json format instead). It also
converts binary strings assuming either UTF-8 or CP1252
encodings (not ASCII, with occasional decoding errors)
'''
if isinstance(value, unicode):
return value
if hasattr(value, 'filename'):
# cgi.FieldStorage instance for uploaded files, show the name
value = value.filename
if value is missing or value is None:
return u''
if isinstance(value, bytes):
# bytes only arrive when core ckan or plugins call
# actions from Python code
try:
return value.decode(u'utf8')
except UnicodeDecodeError:
return value.decode(u'cp1252')
try:
return json.dumps(value, sort_keys=True, ensure_ascii=False)
except Exception:
# at this point we have given up. Just don't error out
try:
return unicode(value)
except Exception:
return u'\N{REPLACEMENT CHARACTER}'

0 comments on commit cb8dd57

Please sign in to comment.