# -*- coding: utf8 -*-
"""
FormRender takes care of the rendering of forms to HTML.
"""
HTML_FIELD = u'''
{title}
{h_input}
{errors}
'''
HTML_FIELD_CHECKBOX = u'''
{h_input}
{title}
{errors}
'''
HTML_REQUIRED = u'{} •'
class FormRender(object):
"""
FormRender takes care of the rendering of forms to HTML.
"""
field_tpl = {
"string": u'',
"integer": u'',
"float": u'',
"date": u'',
"file": u'',
"password": u'',
"text": u'',
"radio_option": u'{label}
',
"select_option": u'',
"select": u'',
"checkbox": u'',
}
def __init__(self, form_def):
self.form_def = form_def
def cast_params(self, params):
"""
Casts values in `params` dictionary to the correct types and values for
use in the form rendering.
"""
new_params = params.copy()
if 'required' in new_params:
if new_params['required'] is False:
new_params['required'] = ""
else:
new_params["required"] = "required"
if 'classes' in new_params:
new_params['classes'] = ' '.join(new_params['classes'])
if 'checked' in new_params:
if new_params['checked'] is False:
new_params['checked'] = ""
else:
new_params['checked'] = "checked"
return new_params
def r_field(self, field_type, **kwargs):
"""
Render a generic field to HTML.
"""
params = self.cast_params(kwargs)
method_name = 'r_field_{0}'.format(field_type)
method = getattr(self, method_name, None)
field = method(**params)
if 'required' in kwargs and kwargs['required'] is True:
return HTML_REQUIRED.format(field)
else:
return field
def r_field_string(self, name, value, minlen=None, maxlen=None, size=50,
required=False, classes='', style=""):
"""
Render a string field to HTML.
"""
tpl = self.field_tpl['string']
return tpl.format(name=name, value=value, minlen=minlen, maxlen=maxlen,
size=size, required=required, classes=classes,
style=style)
def r_field_integer(self, name, value, minval=None, maxval=None,
required=False, classes='', style=""):
"""
Render a integer field to HTML.
"""
tpl = self.field_tpl['integer']
return tpl.format(name=name, value=value, minval=minval, maxval=maxval,
required=required, classes=classes, style=style)
def r_field_float(self, name, value, minval=None, maxval=None,
required=False, classes='', style=""):
"""
Render a float field to HTML.
"""
tpl = self.field_tpl['float']
return tpl.format(name=name, value=value, minval=minval, maxval=maxval,
required=required, classes=classes, style=style)
def r_field_date(self, name, value, minval='', maxval='', required=False,
classes='', style=""):
"""
Render a date field to HTML.
"""
tpl = self.field_tpl['date']
return tpl.format(name=name, value=value, minval=minval, maxval=maxval,
required=required, classes=classes, style=style)
def r_field_file(self, name, required=False, classes='', style=""):
"""
Render a file field to HTML.
"""
tpl = self.field_tpl['file']
return tpl.format(name=name, required=required, classes=classes,
style=style)
def r_field_password(self, name, value, minlen=None, required=False,
classes='', style=""):
"""
Render a password field to HTML.
"""
tpl = self.field_tpl['password']
return tpl.format(name=name, value=value, minlen=minlen,
required=required, classes=classes, style=style)
def r_field_text(self, name, value, rows=4, cols=80, minlen=None,
maxlen=None, required=False, classes='', style=""):
"""
Render a text field to HTML.
"""
tpl = self.field_tpl['text']
return tpl.format(name=name, value=value, rows=rows, cols=cols,
minlen=minlen, maxlen=maxlen, required=required,
classes=classes, style=style)
def r_field_radio(self, name, value, options, classes='', style=""):
"""
Render a radio field to HTML.
"""
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,
style=style))
return u''.join(radio_elems)
def r_field_checkbox(self, name, checked, classes='', style=""):
"""
Render a checkbox field to HTML.
"""
tpl = self.field_tpl['checkbox']
return tpl.format(name=name, checked=checked, classes=classes,
style=style)
def r_field_select(self, name, value, options, classes='', style=""):
"""
Render a select field to HTML.
"""
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,
style=style))
tpl = self.field_tpl['select']
return tpl.format(name=name, select_elems=''.join(select_elems),
classes=classes, style=style)
def r_form_line(self, field_type, title, h_input, classes, errors):
"""
Render a line (label + input) to HTML.
"""
if field_type == 'checkbox':
html = HTML_FIELD_CHECKBOX
else:
html = HTML_FIELD
return (html.format(classes=' '.join(classes),
title=title,
h_input=h_input,
errors=u', '.join(errors)))