Use dict string formatting instead of positional arguments for rendering the form fields.

pull/7/head
Ferry Boender 10 years ago
parent 06d1ead881
commit 190a21ebbb
  1. 106
      src/scriptform.py

@ -9,6 +9,8 @@
# - Send responses using self.send_ if possible # - Send responses using self.send_ if possible
# - Maintain order of forms in form configuration. # - Maintain order of forms in form configuration.
# - NOt possible right now to auto prefir dates. # - NOt possible right now to auto prefir dates.
# - Visually distinguish required fields.
# - Allow custom CSS
import sys import sys
import optparse import optparse
@ -686,92 +688,82 @@ class ScriptFormWebApp(WebAppHandler):
return return
field_tpl = { field_tpl = {
"string": u'<input {0} type="text" name="{1}" value="{2}" />', "string": u'<input {required} type="text" name="{name}" value="{value}" class="{classes}" />',
"number": u'<input {0} type="number" min="{1}" max="{2}" name="{3}" value="{4}" />', "number": u'<input {required} type="number" min="{min}" max="{max}" name="{name}" value="{value}" class="{classes}" />',
"integer": u'<input {0} type="number" min="{1}" max="{2}" name="{3}" value="{4}" />', "integer": u'<input {required} type="number" min="{min}" max="{max}" name="{name}" value="{value}" class="{classes}" />',
"float": u'<input {0} type="number" min="{1}" max="{2}" step="any" name="{3}" value="{4}" />', "float": u'<input {required} type="number" min="{min}" max="{max}" step="any" name="{name}" value="{value}" class="{classes}" />',
"date": u'<input {0} type="date" name="{1}" value="{2}" />', "date": u'<input {required} type="date" name="{name}" value="{value}" class="{classes}" />',
"file": u'<input {0} type="file" name="{1}" value="{2}" />', "file": u'<input {required} type="file" name="{name}" value="{value}" class="{classes}" />',
"password": u'<input {0} type="password" min="{1}" name="{2}" value="{3}" />', "password": u'<input {required} type="password" min="{min}" name="{name}" value="{value}" class="{classes}" />',
"text": u'<textarea {0} name="{1}" rows="{2}" cols="{3}">{4}</textarea>', "text": u'<textarea {required} name="{name}" rows="{rows}" cols="{cols}" class="{classes}">{value}</textarea>',
"select": u'<option value="{0}" {1}>{2}</option>', "select": u'<option value="{value}" {selected} class="{classes}">{title}</option>',
"checkbox": u'<input {0} {1} type="checkbox" name="{1}" value="on" />', "checkbox": u'<input {required} {checked} type="checkbox" name="{name}" value="on" class="{classes}" />',
"radio": u'<input {0} type="radio" name="{1}" value="{2}">{3}<br/>', "radio": u'<input {required} {checked} type="radio" name="{name}" value="{value}" class="{classes}">{title}<br/>',
} }
def render_field(field, errors): def render_field(field, errors):
tpl = field_tpl[field['type']] tpl = field_tpl[field['type']]
field_value = form_values.get(field['name'], '')
required = u'' params = {
"required": u"",
"value": form_values.get(field['name'], ''),
"classes": "",
"name": field['name'],
"min": field.get("min", ''), # number, float
"max": field.get("max", ''), # number, float
"rows": field.get('rows', 5), # text
"cols": field.get('cols', 80), # text
}
if field.get('required', None): if field.get('required', None):
required = 'required' required = 'required'
if field.get('hidden', None):
params['classes'] += 'hidden '
if field['type'] == 'string': if field['type'] in ('string', 'number', 'integer', 'float', 'date', 'file', 'password', 'text'):
input = tpl.format(required, field['name'], field_value) input = tpl.format(**params)
elif field['type'] == 'number' or \
field['type'] == 'integer' or \
field['type'] == 'float':
input = tpl.format(required, field.get('min', ''),
field.get('max', ''),
field['name'],
field_value)
elif field['type'] == 'date':
input = tpl.format(required, field['name'], field_value)
elif field['type'] == 'file':
input = tpl.format(required, field['name'], field_value)
elif field['type'] == 'password':
input = tpl.format(required, field.get('minlen', ''), field['name'], field_value)
elif field['type'] == 'radio': elif field['type'] == 'radio':
# Check first radio button if no other one is checked
if not form_values.get(field['name'], None):
checked_value = field['options'][0][0]
else:
checked_value = form_values[field['name']]
radio_elems = [] radio_elems = []
checked = u'checked'
for option in field['options']: for option in field['options']:
if field['name'] in form_values: params['value'] = option[0]
# If a value was passed in, set the radio to checked if params['title'] = option[1]
# this is that value. if checked_value == option[0]:
if form_values[field['name']] == option[0]: params['checked'] = u'checked'
checked = u'checked'
else: else:
checked = u'' params['checked'] = u''
radio_elems.append(tpl.format(checked, field['name'], option[0], option[1], field_value)) radio_elems.append(tpl.format(**params))
checked = u'' # Check first radio option
input = u''.join(radio_elems) input = u''.join(radio_elems)
elif field['type'] == 'checkbox': elif field['type'] == 'checkbox':
checked = '' params['checked'] = ''
if field['name'] in form_values and form_values[field['name']] == 'on': if field['name'] in form_values and form_values[field['name']] == 'on':
checked = 'checked' params['checked'] = u'checked'
input = tpl.format(required, checked, field['name']) input = tpl.format(**params)
elif field['type'] == 'text':
rows = field.get('rows', 5)
cols = field.get('cols', 80)
input = tpl.format(required,
field['name'],
rows,
cols,
field_value)
elif field['type'] == 'select': elif field['type'] == 'select':
options = [] options = []
selected = '' selected = ''
for option in field['options']: for option in field['options']:
params['value'] = option[0]
params['title'] = option[1]
params['selected'] = u''
if field['name'] in form_values and form_values[field['name']] == option[0]: if field['name'] in form_values and form_values[field['name']] == option[0]:
selected = 'selected' params['selected'] = u'selected'
options.append(tpl.format(option[0], selected, option[1])) options.append(tpl.format(**params))
selected = '' params['options'] = ''.join(options)
input = u'<select {0} name="{1}">{2}</select>'.format(required, field['name'], options) input = u'<select {required} name="{name}">{options}</select>'.format(**params)
else: else:
raise ValueError("Unsupported field type: {0}".format( raise ValueError("Unsupported field type: {0}".format(
field['type']) field['type'])
) )
classes = ''
if 'hidden' in field and field['hidden']:
classes += 'hidden '
if field['type'] != 'checkbox': if field['type'] != 'checkbox':
html = html_field html = html_field
else: else:
html = html_field_checkbox html = html_field_checkbox
return (html.format(classes=classes, return (html.format(classes=params['classes'],
title=field['title'], title=field['title'],
input=input, input=input,
errors=u', '.join(errors))) errors=u', '.join(errors)))

Loading…
Cancel
Save