[storm] RFC: Storm with Pylons and Repoze.tm2
James Henstridge
james at jamesh.id.au
Mon Jul 28 05:30:35 BST 2008
On Mon, Jul 28, 2008 at 12:29 AM, Olaf Conradi <olaf at conradi.org> wrote:
> 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 ;)
This sounds like a useful feature, although I do worry about the code
duplication. I've been looking at using Storm with Django recently,
and am reusing the Zope transaction manager there too (since Django
doesn't offer any kind of global TM).
Repeating all the synchronizer/data manager code for each of these
systems would mean that problems would need to be fixed multiple times
instead of once (and there are things I'd like to see changed in the
way we hook up stores to the TM).
Was there anything preventing you from using the ZStorm class directly
for your purposes? You could probably still use the Paste registry as
a way of accessing the singleton rather than the Zope's utility
system.
As for the actual code in your branch, it needs tests. This will be
less of a problem if you can reuse the existing tested code more
though :)
James.
More information about the storm
mailing list