[storm] RFC: Storm with Pylons and Repoze.tm2

Olaf Conradi olaf at conradi.org
Sun Jul 27 17:29:19 BST 2008


Hello,

I modified the Zope module for Storm to use repoze.tm2 as transactions
and add a middleware layer for use with Pylons.

Let's describe it with a Pylons example:

In development.ini:

# Add repoze.tm2 in the pipeline
[pipeline:main]
pipeline = egg:Paste#cgitb
           egg:Paste#httpexceptions
           egg:repoze.tm2#tm
           your-app

In middleware.py:

# add import
from storm.tm.middleware import StormMiddleware

# inside make_app(global_conf, full_stack=True, **app_conf):

    # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
    app = StormMiddleware(app)

In the controller of your app:

# add import
from storm.store import Store
from storm.tm.session import storm_session

from yourapp.model import Person

class MyController(BaseController):
    def index(self):
        store = storm_session.get('persondb',
config['storm.database.persons.uri'])
        c.persons = list(store.find(Person))
        return render('list.mako')

Just like the zope module had global_zstorm, this module uses
storm_session for use in your Pylons application. It's now a
StackedObjectProxy, and I think I can therefore remove the thread
local stuff from TMStorm.

A lot of code is the same between the Zope module and this module. It
would be nice to share that, and not duplicate it.

Because of repoze.tm2 each request will be a transaction, it will
abort on 401 or raised exceptions, or otherwise commit.

An example of a controller which adds a records:

    def new(self):
        store = storm_session.get('persondb',
config['storm.database.persons.uri'])
        p = Person()
        p.firstname = u"Boo"
        p.lastname = u"Foo"
        p.gender = "Male"
        p.date_of_birth = date(1975, 2, 8)
        store.add(p)
        return 'Done'

So no need to use store.commit, it will be handled by repoze.tm2.
If you add a second store both will either commit or rollback.

The code is hosted in a launchpad branch, use:
bzr branch lp:~olaf-conradi/storm/tmstorm

Comments are welcome ;)

Cheers,
 -Olaf



More information about the storm mailing list