Source

pyconjp2011 / tw2 / pyconjp2011 / tw2_demo / __init__.py

Full commit
import os
from webob import Request, Response
from webob.dec import wsgify
from sqlalchemy import engine_from_config
from mako.lookup import TemplateLookup
import tw2.core
import tw2.forms
import tw2.sqla

from .. import models

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

templates = TemplateLookup(directories=[templates_dir])

models.Group.query = models.DBSession.query(models.Group)

class UserForm(tw2.sqla.DbFormPage):
    entity = models.User
    class child(tw2.forms.TableForm):
        user_name = tw2.forms.TextField(validator=tw2.core.Required)
        password = tw2.forms.PasswordField(validator=tw2.core.Required)
        user_image = tw2.forms.FileField()
        groups = tw2.sqla.DbSingleSelectField(entity=models.Group)

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



from tw.api import make_middleware

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

    return application