From 5f880c2a4ea953d3e90ae359f03ac8bf76cfd244 Mon Sep 17 00:00:00 2001 From: Samuel Gaist <samuel.gaist@idiap.ch> Date: Wed, 29 Aug 2018 15:18:18 +0200 Subject: [PATCH] [common][utils] Add ensure_string If the input is of binary type, returns the decoded version. --- beat/web/common/utils.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/beat/web/common/utils.py b/beat/web/common/utils.py index 122c25fab..a2c5886e3 100644 --- a/beat/web/common/utils.py +++ b/beat/web/common/utils.py @@ -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 -- GitLab