Convert dynamic UI form names to string type

In case form name is in unicode in python2 or bytes in python3,
we need to convert it to str to make form creation possible.

Change-Id: I9ddf4928db9ecf0242f0503888bba56a2a0fdf56
Closes-bug: #1596177
This commit is contained in:
Valerii Kovalchuk 2016-06-30 17:54:27 +03:00
parent d9c70b3463
commit adab734e8f
3 changed files with 41 additions and 4 deletions

View File

@ -141,3 +141,14 @@ def int2base(x, base):
digits.append('-')
digits.reverse()
return ''.join(digits)
def to_str(text):
if not isinstance(text, str):
# unicode in python2
if isinstance(text, six.text_type):
text = text.encode('utf-8')
# bytes in python3
elif isinstance(text, six.binary_type):
text = text.decode('utf-8')
return text

View File

@ -76,10 +76,12 @@ class Service(object):
for key, value in six.iteritems(kwargs):
setattr(self, key, value)
if forms:
for counter, data in enumerate(forms):
name, field_specs, validators = self.extract_form_data(data)
self._add_form(name, field_specs, validators)
for form in forms:
name, field_specs, validators = self.extract_form_data(form)
# NOTE(kzaitsev) should be str (not unicode) under python2
# however it also works as str under python3
name = helpers.to_str(name)
self._add_form(name, field_specs, validators)
# Add ManageWorkflowForm
workflow_form = catalog_forms.WorkflowManagementForm()

View File

@ -0,0 +1,24 @@
# Copyright (c) 2016 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import testtools
from muranodashboard.dynamic_ui import helpers
class TestHelper(testtools.TestCase):
def test_to_str(self):
names = ['string', b'ascii', u'ascii',
u'\u043d\u0435 \u0430\u0441\u043a\u0438']
for name in names:
self.assertIsInstance(helpers.to_str(name), str)