CherryPy Project Download

Storm is the ORM from Canonical (makers of Ubuntu.) Here is the simple implementation I have that utilizes CP 3.1 (Currently Beta) and Storms current trunk release (.11 i believe it is).

For my connection setup, here is the code.

def connect_db(thread_index):
	database = create_database(cherrypy.config.get('storm.default.url'))
	cherrypy.thread_data.store = Store(database)
	
cherrypy.engine.subscribe('start_thread', connect_db)

In your config file you will want:

storm.default.url = "postgres://postgres:pass@server:5432/database"

Here is a sample method that uses genshi, storm etc:

@cherrypy.expose
@template.output('index.html')
def index(self, site_id=1):
    site = cherrypy.thread_data.store.find(Site, Site.site_id == 1).one()
    return template.render(site=site)

** NOTE: This last part is un-tested as of yet. I am still debugging it.

Instead of doing a commit on every query in every action you can allow the request handler to do the commit for you. This is very similar to the Dejavu example shown in the tools section as well. This basically attempts to do a commit and if it doesn't commit it does a rollback and spits out an error.

def do_commit():
	try:
		cherrypy.thread_data.store.commit()
	except:
		cherrypy.thread_data.store.rollback()
		cherrypy.log(format_exc(), "STORM")
		

def try_commit():
	
	if cherrypy.response.stream:
		cherrypy.request.hooks.attach('on_end_request', do_commit)
	else:
		if isinstance(cherrypy.response.body, types.GeneratorType):
			cherrypy.response.collapse_body()
		do_commit()
	
cherrypy.tools.storm = cherrypy.Tool('on_end_resource', try_commit)

Simply create the tool and then in your config you need to have

tools.storm.on = True

Hosted by WebFaction

Log in as guest/cherrypy to create/edit wiki pages