initialize_sql(engine)
config = Configurator(settings=settings)
config.add_static_view('static', 'xonstat:static')
- config.add_route('home', '/', view='xonstat.views.my_view',
- view_renderer='templates/mytemplate.pt')
return config.make_wsgi_app()
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()
-class MyModel(Base):
- __tablename__ = 'models'
- id = Column(Integer, primary_key=True)
- name = Column(Unicode(255), unique=True)
- value = Column(Integer)
-
- def __init__(self, name, value):
- self.name = name
- self.value = value
-
-def populate():
- session = DBSession()
- model = MyModel(name=u'root', value=55)
- session.add(model)
- session.flush()
- transaction.commit()
-
def initialize_sql(engine):
DBSession.configure(bind=engine)
Base.metadata.bind = engine
Base.metadata.create_all(engine)
- try:
- populate()
- except IntegrityError:
- DBSession.rollback()
from xonstat.models import DBSession
-from xonstat.models import MyModel
-
-def my_view(request):
- dbsession = DBSession()
- root = dbsession.query(MyModel).filter(MyModel.name==u'root').first()
- return {'root':root, 'project':'XonStat'}