Source

CherryPy Recipes / testing / load / myapp / __init__.py

Full commit
# -*- coding: utf-8 -*-

if __name__ == '__main__':
    import os, os.path
    base_dir = os.path.abspath(os.getcwd())
    
    import sys
    sys.path.append(os.path.normpath(os.path.join(base_dir, "../../../web/database/sql_alchemy")))
    sys.path.append(os.path.normpath(os.path.join(base_dir, "../../../web/templating/mako_templating")))
    
    import cherrypy
    cherrypy.config.update({'server.socket_port': 8090,
                            'server.thread_pool': 80})
    
    from makoplugin import MakoTemplatePlugin
    MakoTemplatePlugin(cherrypy.engine, base_dir=os.path.join(base_dir, 'templates')).subscribe()

    from makotool import MakoTool
    cherrypy.tools.template = MakoTool()
    
    from saplugin import SAEnginePlugin
    saplugin = SAEnginePlugin(cherrypy.engine, 'sqlite:///%s/myapp.db' % base_dir)
    saplugin.subscribe()

    from satool import SATool
    cherrypy.tools.db = SATool()

    def create_database():
        import random
        cherrypy.log('Creating database')
        from model import Post, Comment, Base
        Base.metadata.create_all(saplugin.sa_engine)

        session = saplugin.bind()
        for i in range(12):
            post = Post(title=u"Title %d" % i,
                        author=u"Author %d" % i,
                        text=u"some text" * i)
            for j in range(random.randint(1, 6)):
                comment = Comment(author=u"Commenter %d" % j,
                                  text=u"blah blah" * j)
                post.comments.append(comment)
            session.add(post)
        session.commit()
        session.remove()
        
    def destroy_database():
        cherrypy.log('Destroying database')
        from model import Base
        Base.metadata.drop_all(saplugin.sa_engine)

    cherrypy.engine.subscribe('start', create_database, priority=60)
    cherrypy.engine.subscribe('stop', destroy_database, priority=40)
    
    from blog import Blog
    cherrypy.quickstart(
        Blog(), '/', {'/':
                      {
                          'tools.encode.on': False,
                          'tools.template.on': True,
                          'tools.db.on': True
                          },
                      '/post': {
                          'tools.template.on': False
                          },
                      '/favicon.ico': {
                          'tools.template.on': False
                          }
                      }
        )