From 55f3253453499ea7302f6a4749443e9bc70adb77 Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Sat, 18 Apr 2015 09:04:35 +0200 Subject: [PATCH] Unicode and utf8. --- src/scriptform.py | 54 +++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/scriptform.py b/src/scriptform.py index 17c6000..1909e67 100755 --- a/src/scriptform.py +++ b/src/scriptform.py @@ -503,12 +503,12 @@ class FormDefinition: def validate_file(self, field_def, form_values): value = form_values[field_def['name']] field_name = field_def['name'] - upload_fname = form_values['{0}__name'.format(field_name)] + upload_fname = form_values[u'{0}__name'.format(field_name)] upload_fname_ext = os.path.splitext(upload_fname)[-1].lstrip('.') extensions = field_def.get('extensions', None) if extensions is not None and upload_fname_ext not in extensions: - raise ValidationError("Only file types allowed: {0}".format(','.join(extensions))) + raise ValidationError("Only file types allowed: {0}".format(u','.join(extensions))) return value @@ -653,7 +653,7 @@ class ScriptFormWebApp(WebAppHandler): output = html_list.format( header=html_header.format(title=form_config.title), footer=html_footer, - form_list=''.join(h_form_list) + form_list=u''.join(h_form_list) ) self.send_response(200) self.send_header('Content-type', 'text/html') @@ -669,22 +669,22 @@ class ScriptFormWebApp(WebAppHandler): return field_tpl = { - "string": '', - "number": '', - "integer": '', - "float": '', - "date": '', - "file": '', - "password": '', - "text": '', - "select": '', - "radio": '{3}
', + "string": u'', + "number": u'', + "integer": u'', + "float": u'', + "date": u'', + "file": u'', + "password": u'', + "text": u'', + "select": u'', + "radio": u'{3}
', } def render_field(field, errors): tpl = field_tpl[field['type']] - required = '' + required = u'' if field.get('required', None): required = 'required' @@ -704,11 +704,11 @@ class ScriptFormWebApp(WebAppHandler): input = tpl.format(required, field.get('minlen', ''), field['name']) elif field['type'] == 'radio': radio_elems = [] - checked = 'checked' + checked = u'checked' for option in field['options']: radio_elems.append(tpl.format(checked, field['name'], option[0], option[1])) - checked = '' # Check first radio option - input = ''.join(radio_elems) + checked = u'' # Check first radio option + input = u''.join(radio_elems) elif field['type'] == 'text': rows = field.get('rows', 5) cols = field.get('cols', 80) @@ -719,8 +719,8 @@ class ScriptFormWebApp(WebAppHandler): cols ) elif field['type'] == 'select': - options = ''.join([tpl.format(o[0], o[1]) for o in field['options']]) - input = ''.format(required, field['name'], options) + options = u''.join([tpl.format(o[0], o[1]) for o in field['options']]) + input = u''.format(required, field['name'], options) else: raise ValueError("Unsupported field type: {0}".format( field['type']) @@ -733,7 +733,7 @@ class ScriptFormWebApp(WebAppHandler): '''.format(title=field['title'], input=input, - errors=', '.join(errors))) + errors=u', '.join(errors))) # Make sure the user is allowed to access this form. form_def = form_config.get_form_def(form_name) @@ -742,12 +742,12 @@ class ScriptFormWebApp(WebAppHandler): self.send_error(401, "You're not authorized to view this form") return - html_errors = '' + html_errors = u'' if errors: - html_errors = '' output = html_form.format( header=html_header.format(title=form_config.title), @@ -756,7 +756,7 @@ class ScriptFormWebApp(WebAppHandler): description=form_def.description, errors=html_errors, name=form_def.name, - fields=''.join([render_field(f, errors.get(f['name'], [])) for f in form_def.fields]), + fields=u''.join([render_field(f, errors.get(f['name'], [])) for f in form_def.fields]), submit_title=form_def.submit_title ) self.send_response(200) @@ -821,10 +821,10 @@ class ScriptFormWebApp(WebAppHandler): result = form_config.callback(form_name, form_values, self) if result: if result['exitcode'] != 0: - msg = '{0}'.format(cgi.escape(result['stderr'])) + msg = u'{0}'.format(cgi.escape(result['stderr'])) else: if form_def.output == 'escaped': - msg = '
{0}
'.format(cgi.escape(result['stdout'])) + msg = u'
{0}
'.format(cgi.escape(result['stdout'])) else: msg = result['stdout']