Last active
January 8, 2024 01:40
-
-
Save felipeblassioli/43a57eaa575679463d01 to your computer and use it in GitHub Desktop.
Creating custom wtforms.widgets in a Flask.app. Custom Form, Custom Field and Custom Widget.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from wtforms import Form, Field, TextField | |
from wtforms.widgets import TextInput, html_params | |
# http://wtforms.simplecodes.com/docs/0.6/widgets.html#custom-widgets | |
def render_ul_users(field, ul_class='awesome_class', **kwargs): | |
kwargs.setdefault('id', field.id) | |
if hasattr(field, 'users'): | |
html = [u'<ul %s>' % html_params(id=field.id, class_=ul_class)] | |
# A sure hope users is iterable: | |
for u in field.users: | |
params = dict(kwargs,id=u['_id']) | |
html.append(u'<li %s>%s</li>' % (html_params(**params),u['name'])) | |
html.append('<li>Muhahahaha <--- This is hard-code</li>') | |
html.append(u'</ul>') | |
else: | |
html = '<h1>Too bad, so sad</h1>' | |
return u''.join(html) | |
class CoxinhaWidget(object): | |
_js = """alert('{} says: Torresmo eh mto da hora.');""" | |
html_params = staticmethod(html_params) | |
def __init__(self, input_type='submit', text=''): | |
self.input_type = input_type | |
self.text = text | |
def __call__(self, field, **kwargs): | |
kwargs.setdefault('id', field.id) | |
if hasattr(field, 'users'): | |
html = [u'<ul %s>' % html_params(id=field.id)] | |
# A sure hope users is iterable: | |
for u in field.users: | |
params = dict(kwargs,id=u['_id'],onclick=self._js.format(u['name'])) | |
html.append(u'<li %s>%s</li>' % (html_params(**params),u['name'])) | |
html.append('<li>TUKA EH + TORRESMO</li>') | |
html.append(u'</ul>') | |
else: | |
html = '<h1>Too bad, so sad</h1>' | |
return u''.join(html) | |
class CoxinhaField(Field): | |
widget = CoxinhaWidget() | |
def __init__(self, label=None, validators=None, text='Save', **kwargs): | |
super(CoxinhaField, self).__init__(label, validators, **kwargs) | |
class SomeRandomForm(Form): | |
name = TextField('Name') | |
h4x0r_field = Field(widget=render_ul_users) | |
neat_field = CoxinhaField() | |
def __init__(self, users, *args, **kwargs): | |
super(SomeRandomForm, self).__init__(*args,**kwargs) | |
self.h4x0r_field.users = users | |
self.neat_field.users = users | |
app = Flask(__name__, static_url_path='') | |
import itertools | |
from random import randint | |
def _gen_users(s): | |
return [ dict(_id=randint(1,101111), name=n) for n in list(map("".join, itertools.permutations(s)))[0:10] ] | |
@app.route('/') | |
def index(): | |
users = _gen_users('tutuca') | |
form = SomeRandomForm(users) | |
print form.h4x0r_field | |
print form.neat_field | |
return form.neat_field() | |
@app.route('/h4x') | |
def h4x(): | |
users = _gen_users('tuanny') | |
form = SomeRandomForm(users) | |
return form.h4x0r_field() | |
@app.route('/neat') | |
def neat(): | |
users = _gen_users('hipopotuka') | |
form = SomeRandomForm(users) | |
return form.neat_field() | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment