skip to navigation
skip to content

cromlech.beaker 0.2

Session handling for cromlech using beaker

Downloads ↓

cromlech.beaker is an integration of beaker to cromlech for session management.

Context manager

Let's import some helpers:

>>> from beaker.middleware import SessionMiddleware
>>> from cromlech.browser.testing import TestRequest
>>> from webtest import TestApp

And our session controller:

>>> from cromlech.beaker import BeakerSession

Use the BeakerSession context manager inside your application using a with statement:

>>> def simple_app(environ, start_response):
...     """retained visited path, raise exception if path contain 'fail'
...     """
...     with BeakerSession(environ, 'beaker.session') as session:
...         visited = session.get('visited', [])
...         visited.append(environ['PATH_INFO'])
...         session['visited'] = visited
...         if 'fail' in environ['PATH_INFO']:
...             raise ValueError
...     start_response('200 OK', [('Content-type', 'text/plain')])
...     return [", ".join(visited),]

Note that the call to start_response nust be outside the context.

Then run it with beaker middelware:

# Configure the SessionMiddleware
>>> opts = {
...     'session.type': 'memory',
...     'session.validate_key': 'cromlech.beaker',
... }

>>> wsgi_app = TestApp(SessionMiddleware(simple_app, **opts))
>>> result = wsgi_app.get('/foo')
>>> result.status
'200 OK'
>>> result.body
'/foo'

>>> result = wsgi_app.get('/bar')
>>> result.status
'200 OK'
>>> result.body
'/foo, /bar'

If application raise an exception, the context manager wont save the session:

>>> result = wsgi_app.get('/fail')
Traceback (most recent call last):
...
ValueError
>>> result.status
'200 OK'
>>> result.body
'/foo, /bar'

Integration with cromlech.browser

The context manager hooks the session in cromlech.browser, so you can use cromlech.browser.session.getSession:

>>> from cromlech.browser.session import getSession

>>> def cromlech_app(environ, start_response):
...     """retained visited path, raise exception if path contain 'fail'
...     """
...     with BeakerSession(environ, 'beaker.session'):
...         session = getSession()
...         if not session.has_key('foo'):
...             session['foo'] = 0
...         session['foo'] += 1
...     start_response('200 OK', [('Content-type', 'text/plain')])
...     return [str(session['foo']),]

>>> wsgi_app = TestApp(SessionMiddleware(cromlech_app, **opts))
>>> result = wsgi_app.get('/foo')
>>> result.status
'200 OK'
>>> result.body
'1'

>>> result = wsgi_app.get('/bar')
>>> result.status
'200 OK'
>>> result.body
'2'

Changelog

0.1b2 (2011-08-25)

  • fixed middleware which was not working as beaker check session states at start_response call time

0.1b1 (2011-08-25)

  • Corrected a bug in the middleware where the return was outside the controled execution.

0.1a2 (2011-05-13)

  • Added WSGI app filter, to wrap a response into a session controler.

0.1a1 (2011-05-12)

  • Initial release
 
File Type Py Version Uploaded on Size # downloads
cromlech.beaker-0.2.tar.gz (md5) Source 2012-11-16 5KB 0