try:
from html import escape
except ImportError:
from cgi import escape
# from wtforms import widgets
from wtforms.widgets import html_params
from wtforms.fields.core import Field
from wtforms.compat import text_type, izip
from markupsafe import Markup
class MySelect(object):
"""
Renders a select field.
If `multiple` is True, then the `size` property should be specified on
rendering to make the field useful.
The field must provide an `iter_choices()` method which the widget will
call on rendering; this method must yield tuples of
`(value, label, selected)`.
"""
def __init__(self, multiple=False):
self.multiple = multiple
def __call__(self, field, **kwargs):
kwargs.setdefault('id', field.id)
if self.multiple:
kwargs['multiple'] = True
html = ['')
return Markup(''.join(html))
@classmethod
def render_option(cls, value, label, selected, **kwargs):
if value is True:
# Handle the special case of a 'True' value.
value = text_type(value)
options = dict(kwargs, value=value)
if selected:
options['selected'] = True
return Markup('' % (html_params(**options), escape(text_type(label), quote=False)))
@classmethod
def render_optgroup(cls, previous_label, label, **kwargs):
options = dict(kwargs)
if previous_label is None:
return Markup(
'')
else:
return Markup(
'