Source

pyconjp2011 / deform / pyconjp2011 / deform_demo / __init__.py

import os
from webob import Request, Response
from webob.dec import wsgify
from sqlalchemy import engine_from_config
from mako.lookup import TemplateLookup
from deform.form import Form
import colander as c
import deform.widget as w
from deform import FileData

from .. import models

here = os.path.dirname(__file__)
templates_dir = os.path.join(here, "templates")

templates = TemplateLookup(directories=[templates_dir])

class UserSchema(c.MappingSchema):
    username = c.SchemaNode(c.String())
    password = c.SchemaNode(c.String(),
            widget=w.PasswordWidget())
    user_image = c.SchemaNode(FileData(),
            widget=w.FileUploadWidget(os.path.join(here, 'deform_tmpdata')))

@wsgify
def index(request):
    user_form = Form(UserSchema())
    tmpl = templates.get_template('index.mak')
    return Response(tmpl.render(form=user_form))


def main(global_conf, **settings):
    engine = engine_from_config(settings)
    models.init_db(engine)
    application = index
    
    return application