Commit adf944d8 authored by Mikaël Salson's avatar Mikaël Salson
Browse files

vidjil_utils: extract safe encoding

It can be (and will be) useful elsewhere.
parent 875000ff
......@@ -108,10 +108,7 @@ def anon_names(patient_id, first_name, last_name, can_view=None):
if can_view or (can_view == None and auth.can_view_patient_info(patient_id)):
name = last_name + " " + first_name
else:
try:
ln = unicode(last_name, 'utf-8')
except UnicodeDecodeError:
ln = last_name
ln = safe_encoding(last_name)
name = ln[:3]
# Admins also see the patient id
......@@ -120,6 +117,16 @@ def anon_names(patient_id, first_name, last_name, can_view=None):
return name
def safe_encoding(string):
'''
Try to encode the string in UTF-8 but if it fails just
returns the string.
'''
try:
return unicode(string, 'utf-8')
except UnicodeDecodeError:
return string
# take a list of strings to check and a filter_str (list of word to find (or not))
# return true if the string respect the filter list
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment