Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko
Created March 19, 2012 14:54
Show Gist options
  • Save mitsuhiko/2115179 to your computer and use it in GitHub Desktop.
Save mitsuhiko/2115179 to your computer and use it in GitHub Desktop.
flask-script with wtforms
import sys
from werkzeug.datastructures import ImmutableDict, MultiDict
from flask import request
from flaskext.script import Manager
from postfixmgmt import app, db, __version__
from postfixmgmt.forms import DomainAddForm
manager = Manager(app)
def validate_fields(fields):
for name, field in fields._fields.iteritems():
return validate_field(field)
def validate_field(field):
if field.validate(field.raw_data):
return True
return False
def print_field_errors(fields):
for name, field in fields._fields.iteritems():
for error in field.errors:
print error
sys.exit()
@manager.option("-d", "--desc", help="Description of the domain. [default: '']")
@manager.option("-n", "--name", help="Domain name", required=True)
@manager.option("-m", "--method", choices=('add', 'edit', 'delete'), required=True)
def add_domain(**values):
request.form = DomainAddForm(MultiDict(values))
validate_fields(request.form)
if __name__ == "__main__":
manager.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment