def forms_simple(): str = forms.String()('kukuku') Assert(str.data) == 'kukuku' FT = forms.Tuple(forms.String(), forms.String()) ft = FT() widgets = [widget.render() for widget in ft.widgets()] Assert(widgets) == [ '<input id="form__0" name="form__0" parent="" value="">', '<input id="form__1" name="form__1" parent="" value="">' ] ft = FT(('kuku', 'kuku')) widgets = [widget.render() for widget in ft.widgets()] Assert(widgets) == [ '<input id="form__0" name="form__0" parent="" value="kuku">', '<input id="form__1" name="form__1" parent="" value="kuku">' ] FL = forms.List(forms.String()) fl = FL(['kuku', 'dsfasfd', 'xcvxczvx']) widgets = [widget.render() for widget in fl.widgets() if widget.render()] Assert(widgets) == [ '<input id="form__0" name="form__0" parent="" value="kuku">', '<input id="form__1" name="form__1" parent="" value="dsfasfd">', '<input id="form__2" name="form__2" parent="" value="xcvxczvx">' ]
def forms_dict_field(): FD = forms.Dict({'a': forms.String(), 'b': forms.String()}) fd = FD() widgets = [widget.render() for widget in fd.widgets()] Assert(widgets) == [ '<input id="form__a" name="form__a" parent="" value="">', '<input id="form__b" name="form__b" parent="" value="">' ] fd = FD({'a': 'kuku', 'b': 'may-may'}) widgets = [widget.render() for widget in fd.widgets()] Assert(widgets) == [ '<input id="form__a" name="form__a" parent="" value="kuku">', '<input id="form__b" name="form__b" parent="" value="may-may">' ]
def forms_flat(): FL = forms.List(forms.String()) FD = forms.Dict({'a': forms.String(), 'b': forms.String(), 'c': FL}) flat = { 'form__a': 'kuku', 'form__b': 'may-may', 'form__c__0': 'wer', 'form__c__1': 'kuku' } unflat = FD.unflat(flat) Assert(unflat) == {'a': 'kuku', 'b': 'may-may', 'c': ['kuku', 'wer']} fd = FD(unflat) widgets = [widget.render() for widget in fd.widgets() if widget.render()] Assert(widgets) == [ '<input id="form__a" name="form__a" parent="" value="kuku">', '<input id="form__c__0" name="form__c__0" parent="" value="kuku">', '<input id="form__c__1" name="form__c__1" parent="" value="wer">', '<input id="form__b" name="form__b" parent="" value="may-may">' ] form = FD(flat) Assert(form.is_valid()) == True Assert(form.data) == {'a': 'kuku', 'b': 'may-may', 'c': ['kuku', 'wer']}
class Pet(forms.Declarative): name = forms.String(field_name='Name') species = forms.String(field_name='Species') field_name = 'Pet'
class Form(forms.Declarative): name = forms.String(field_name='Your name') age = forms.Integer(field_name='Age') pets = forms.List(Pet)
from flask import Flask, render_template from flask import request from procrustes import forms app = Flask(__name__) Form = forms.Tuple(forms.String(name='Your name'), forms.Integer(name='Age')) @app.route('/', methods=['POST', 'GET']) def hello(): if request.form: form = Form(request.form, False) print 'Validate' if form.is_valid(): print 'Cool!' # Use form.data as validated structure else: form = Form(validate=False) return render_template('form2.html', form=form) if __name__ == "__main__": app.run()