Skip to content
Snippets Groups Projects
Commit 5f880c2a authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[common][utils] Add ensure_string

If the input is of binary type, returns the decoded version.
parent 804d6924
No related branches found
No related tags found
2 merge requests!2551.4.x,!249Web fixes
......@@ -28,9 +28,9 @@
"""
Reusable help functions
"""
from django.core.exceptions import ValidationError
from django.utils.encoding import force_text
from django.utils import six
from ..ui.templatetags.markup import restructuredtext
......@@ -117,3 +117,14 @@ def ensure_html(text):
return '<pre>{}</pre>'.format(text)
else:
return restructuredtext(text)
def ensure_string(data):
"""
Ensure that we have a str object from data which can be either a str in
python 2 or a bytes in python 3
"""
if data is not None:
if isinstance(data, six.binary_type):
return data.decode('utf-8')
return data
return ''
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment