Unicode and utf8.

pull/7/head
Ferry Boender 10 years ago
parent c1b611894d
commit 55f3253453
  1. 54
      src/scriptform.py

@ -503,12 +503,12 @@ class FormDefinition:
def validate_file(self, field_def, form_values): def validate_file(self, field_def, form_values):
value = form_values[field_def['name']] value = form_values[field_def['name']]
field_name = 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('.') upload_fname_ext = os.path.splitext(upload_fname)[-1].lstrip('.')
extensions = field_def.get('extensions', None) extensions = field_def.get('extensions', None)
if extensions is not None and upload_fname_ext not in extensions: 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 return value
@ -653,7 +653,7 @@ class ScriptFormWebApp(WebAppHandler):
output = html_list.format( output = html_list.format(
header=html_header.format(title=form_config.title), header=html_header.format(title=form_config.title),
footer=html_footer, footer=html_footer,
form_list=''.join(h_form_list) form_list=u''.join(h_form_list)
) )
self.send_response(200) self.send_response(200)
self.send_header('Content-type', 'text/html') self.send_header('Content-type', 'text/html')
@ -669,22 +669,22 @@ class ScriptFormWebApp(WebAppHandler):
return return
field_tpl = { field_tpl = {
"string": '<input {0} type="text" name="{1}" />', "string": u'<input {0} type="text" name="{1}" />',
"number": '<input {0} type="number" min="{1}" max="{2}" name="{3}" />', "number": u'<input {0} type="number" min="{1}" max="{2}" name="{3}" />',
"integer": '<input {0} type="number" min="{1}" max="{2}" name="{3}" />', "integer": u'<input {0} type="number" min="{1}" max="{2}" name="{3}" />',
"float": '<input {0} type="number" min="{1}" max="{2}" step="any" name="{3}" />', "float": u'<input {0} type="number" min="{1}" max="{2}" step="any" name="{3}" />',
"date": '<input {0} type="date" name="{1}" />', "date": u'<input {0} type="date" name="{1}" />',
"file": '<input {0} type="file" name="{1}" />', "file": u'<input {0} type="file" name="{1}" />',
"password": '<input {0} type="password" min="{1}" name="{2}" />', "password": u'<input {0} type="password" min="{1}" name="{2}" />',
"text": '<textarea {0} name="{1}" rows="{2}" cols="{3}"></textarea>', "text": u'<textarea {0} name="{1}" rows="{2}" cols="{3}"></textarea>',
"select": '<option value="{0}">{1}</option>', "select": u'<option value="{0}">{1}</option>',
"radio": '<input {0} type="radio" name="{1}" value="{2}">{3}<br/>', "radio": u'<input {0} type="radio" name="{1}" value="{2}">{3}<br/>',
} }
def render_field(field, errors): def render_field(field, errors):
tpl = field_tpl[field['type']] tpl = field_tpl[field['type']]
required = '' required = u''
if field.get('required', None): if field.get('required', None):
required = 'required' required = 'required'
@ -704,11 +704,11 @@ class ScriptFormWebApp(WebAppHandler):
input = tpl.format(required, field.get('minlen', ''), field['name']) input = tpl.format(required, field.get('minlen', ''), field['name'])
elif field['type'] == 'radio': elif field['type'] == 'radio':
radio_elems = [] radio_elems = []
checked = 'checked' checked = u'checked'
for option in field['options']: for option in field['options']:
radio_elems.append(tpl.format(checked, field['name'], option[0], option[1])) radio_elems.append(tpl.format(checked, field['name'], option[0], option[1]))
checked = '' # Check first radio option checked = u'' # Check first radio option
input = ''.join(radio_elems) input = u''.join(radio_elems)
elif field['type'] == 'text': elif field['type'] == 'text':
rows = field.get('rows', 5) rows = field.get('rows', 5)
cols = field.get('cols', 80) cols = field.get('cols', 80)
@ -719,8 +719,8 @@ class ScriptFormWebApp(WebAppHandler):
cols cols
) )
elif field['type'] == 'select': elif field['type'] == 'select':
options = ''.join([tpl.format(o[0], o[1]) for o in field['options']]) options = u''.join([tpl.format(o[0], o[1]) for o in field['options']])
input = '<select {0} name="{1}">{2}</select>'.format(required, field['name'], options) input = u'<select {0} name="{1}">{2}</select>'.format(required, field['name'], options)
else: else:
raise ValueError("Unsupported field type: {0}".format( raise ValueError("Unsupported field type: {0}".format(
field['type']) field['type'])
@ -733,7 +733,7 @@ class ScriptFormWebApp(WebAppHandler):
</li> </li>
'''.format(title=field['title'], '''.format(title=field['title'],
input=input, input=input,
errors=', '.join(errors))) errors=u', '.join(errors)))
# Make sure the user is allowed to access this form. # Make sure the user is allowed to access this form.
form_def = form_config.get_form_def(form_name) 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") self.send_error(401, "You're not authorized to view this form")
return return
html_errors = '' html_errors = u''
if errors: if errors:
html_errors = '<ul>' html_errors = u'<ul>'
for error in errors: for error in errors:
html_errors += '<li class="error">{0}</li>'.format(error) html_errors += u'<li class="error">{0}</li>'.format(error)
html_errors += '</ul>' html_errors += u'</ul>'
output = html_form.format( output = html_form.format(
header=html_header.format(title=form_config.title), header=html_header.format(title=form_config.title),
@ -756,7 +756,7 @@ class ScriptFormWebApp(WebAppHandler):
description=form_def.description, description=form_def.description,
errors=html_errors, errors=html_errors,
name=form_def.name, 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 submit_title=form_def.submit_title
) )
self.send_response(200) self.send_response(200)
@ -821,10 +821,10 @@ class ScriptFormWebApp(WebAppHandler):
result = form_config.callback(form_name, form_values, self) result = form_config.callback(form_name, form_values, self)
if result: if result:
if result['exitcode'] != 0: if result['exitcode'] != 0:
msg = '<span class="error">{0}</span>'.format(cgi.escape(result['stderr'])) msg = u'<span class="error">{0}</span>'.format(cgi.escape(result['stderr']))
else: else:
if form_def.output == 'escaped': if form_def.output == 'escaped':
msg = '<pre>{0}</pre>'.format(cgi.escape(result['stdout'])) msg = u'<pre>{0}</pre>'.format(cgi.escape(result['stdout']))
else: else:
msg = result['stdout'] msg = result['stdout']

Loading…
Cancel
Save