Created
March 19, 2012 14:54
-
-
Save mitsuhiko/2115179 to your computer and use it in GitHub Desktop.
flask-script with wtforms
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
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