Delegate form rendering to separate class.

pull/7/head
Ferry Boender 10 years ago
parent d62a3a04ef
commit e2547f9aad
  1. 180
      src/scriptform.py

@ -594,6 +594,104 @@ class WebAppHandler(BaseHTTPRequestHandler):
raise raise
class FormRender():
field_tpl = {
"string": u'<input {required} type="text" name="{name}" value="{value}" class="{classes}" />',
"number": u'<input {required} type="number" min="{min}" max="{max}" name="{name}" value="{value}" class="{classes}" />',
"integer": u'<input {required} type="number" min="{min}" max="{max}" name="{name}" value="{value}" class="{classes}" />',
"float": u'<input {required} type="number" min="{min}" max="{max}" step="any" name="{name}" value="{value}" class="{classes}" />',
"date": u'<input {required} type="date" name="{name}" value="{value}" class="{classes}" />',
"file": u'<input {required} type="file" name="{name}" class="{classes}" />',
"password": u'<input {required} type="password" min="{min}" name="{name}" value="{value}" class="{classes}" />',
"text": u'<textarea {required} name="{name}" rows="{rows}" cols="{cols}" class="{classes}">{value}</textarea>',
"radio_option": u'<input {checked} type="radio" name="{name}" value="{value}" class="{classes}">{label}<br/>',
"select_option": u'<option value="{value}" {selected}>{label}</option>',
"select": u'<select name="{name}" class="{classes}">{select_elems}</select>',
"checkbox": u'<input {checked} type="checkbox" name="{name}" value="on" class="{classes}" />',
}
def __init__(self, form_def):
self.form_def = form_def
def cast_params(self, params):
new_params = params.copy()
if not params.get('required', False):
new_params['required'] = ""
if not params.get('min', False):
new_params["min"] = ""
if not params.get('max', False):
new_params["max"] = ""
return new_params
def r_field(self, type, **kwargs):
method_name = 'r_field_{0}'.format(type)
method = getattr(self, method_name, None)
return method(**kwargs)
def r_field_input(self, type, **kwargs):
params = self.cast_params(kwargs)
tpl = self.field_tpl[type]
return tpl.format(**params)
def r_field_string(self, name, value, required=False, classes=[]):
tpl = self.field_tpl['string']
return tpl.format(name=name, value=value, required=required, classes=classes)
def r_field_number(self, name, value, min=None, max=None, required=False, classes=[]):
tpl = self.field_tpl['number']
return tpl.format(name=name, value=value, min=min, max=max, required=required, classes=classes)
def r_field_integer(self, name, value, min=None, max=None, required=False, classes=[]):
tpl = self.field_tpl['integer']
return tpl.format(name=name, value=value, min=min, max=max, required=required, classes=classes)
def r_field_float(self, name, value, min=None, max=None, required=False, classes=[]):
tpl = self.field_tpl['integer']
return tpl.format(name=name, value=value, min=min, max=max, required=required, classes=classes)
def r_field_date(self, name, value, required=False, classes=[]):
tpl = self.field_tpl['date']
return tpl.format(name=name, value=value, required=required, classes=classes)
def r_field_file(self, name, required=False, classes=[]):
tpl = self.field_tpl['file']
return tpl.format(name=name, required=required, classes=classes)
def r_field_password(self, name, value, min=None, required=False, classes=[]):
tpl = self.field_tpl['password']
return tpl.format(name=name, value=value, min=min, required=required, classes=classes)
def r_field_text(self, name, value, rows=4, cols=80, required=False, classes=[]):
tpl = self.field_tpl['text']
return tpl.format(name=name, value=value, rows=rows, cols=cols, required=required, classes=classes)
def r_field_radio(self, name, value, options, classes=[]):
tpl_option = self.field_tpl['radio_option']
radio_elems = []
for o_value, o_label in options:
checked = ''
if o_value == value:
checked = 'checked'
radio_elems.append(tpl_option.format(name=name, value=value, checked=checked, label=o_label, classes=classes))
return u''.join(radio_elems)
def r_field_checkbox(self, name, checked, classes=''):
tpl = self.field_tpl['checkbox']
return tpl.format(name=name, checked=checked, classes=classes)
def r_field_select(self, name, value, options, classes=[]):
tpl_option = self.field_tpl['select_option']
select_elems = []
for o_value, o_label in options:
selected = ''
if o_value == value:
selected = 'selected'
select_elems.append(tpl_option.format(value=o_value, selected=selected, label=o_label))
tpl = self.field_tpl['select']
return tpl.format(name=name, select_elems=''.join(select_elems), classes=classes)
class ScriptFormWebApp(WebAppHandler): class ScriptFormWebApp(WebAppHandler):
""" """
This class is a request handler for WebSrv. This class is a request handler for WebSrv.
@ -701,63 +799,49 @@ class ScriptFormWebApp(WebAppHandler):
"radio": u'<input {required} {checked} type="radio" name="{name}" value="{value}" class="{classes}">{title}<br/>', "radio": u'<input {required} {checked} type="radio" name="{name}" value="{value}" class="{classes}">{title}<br/>',
} }
fr = FormRender(None)
def render_field(field, errors): def render_field(field, errors):
tpl = field_tpl[field['type']] tpl = field_tpl[field['type']]
params = { params = {
"required": u"", 'name': field['name'],
"value": form_values.get(field['name'], ''), 'classes': [],
"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):
required = 'required'
if field.get('hidden', None): if field.get('hidden', None):
params['classes'] += 'hidden ' params['classes'].append('hidden')
if field['type'] not in ('file', 'checkbox'):
params['value'] = form_values.get(field['name'], ''),
if field['type'] not in ('radio', 'checkbox', 'select'):
params['required'] = field.get('required', False),
if field['type'] in ('number', 'integer', 'float', 'password'):
params['min'] = field.get("min", '')
if field['type'] in ('number', 'integer', 'float'):
params['max'] = field.get("max", '')
if field['type'] in ('string', 'number', 'integer', 'float', 'date', 'file', 'password', 'text'): if field['type'] in ('text'):
input = tpl.format(**params) params['rows'] = field.get("rows", ''),
elif field['type'] == 'radio': params['cols'] = field.get("cols", ''),
# Check first radio button if no other one is checked
if field['type'] == 'radio':
if not form_values.get(field['name'], None): if not form_values.get(field['name'], None):
checked_value = field['options'][0][0] params['value'] = field['options'][0][0]
else: params['options'] = field['options'],
checked_value = form_values[field['name']]
radio_elems = [] if field['type'] in ('radio', 'select'):
for option in field['options']: params['options'] = field['options']
params['value'] = option[0]
params['title'] = option[1] if field['type'] == 'checkbox':
if checked_value == option[0]: params['checked'] = False
params['checked'] = u'checked'
else:
params['checked'] = u''
radio_elems.append(tpl.format(**params))
input = u''.join(radio_elems)
elif field['type'] == 'checkbox':
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':
params['checked'] = u'checked' params['checked'] = True
input = tpl.format(**params)
elif field['type'] == 'select': input = fr.r_field(field['type'], **params)
options = []
selected = ''
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]:
params['selected'] = u'selected'
options.append(tpl.format(**params))
params['options'] = ''.join(options)
input = u'<select {required} name="{name}">{options}</select>'.format(**params)
else:
raise ValueError("Unsupported field type: {0}".format(
field['type'])
)
if field['type'] != 'checkbox': if field['type'] != 'checkbox':
html = html_field html = html_field

Loading…
Cancel
Save