Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

cromlech.beaker

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

Context manager

Let’s import some helpers:

>>> import pytest
>>> from webtest import TestApp
>>> SK = 'session.key'

And our session controller:

>>> from cromlech.wsgistate import WsgistateSession
>>> from cromlech.wsgistate.controlled import WsgistateSession

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

Then run it with wsgistate middelware:

>>> from cromlech.wsgistate import session_wrapper
>>> wsgi_app = TestApp(session_wrapper(simple_app, session_key=SK))
>>> 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'

Testing the transaction awareness

>>> import transaction
>>> from cromlech.wsgistate import SessionStateException
>>> def transactional_app(environ, start_response):
...
...     with transaction.manager as tm:
...         with WsgistateSession(environ, SK, tm) as session:
...             session['crom'] = 'Pyramid'
...             tm.abort()
...
...     assert not session
...
...     with transaction.manager as tm:
...         with WsgistateSession(environ, SK, tm) as session:
...             session['crom'] = 'Crom'
...
...     with transaction.manager as tm:
...         with WsgistateSession(environ, SK, tm) as session:
...             session['lech'] = 'Lech'
...             sp1 = tm.savepoint()
...
...             session['crom'] = 'Zope'
...             session['lech'] = 'Quack'
...             sp2 = tm.savepoint()
...
...             session['crom'] = 'PHP'
...             sp3 = tm.savepoint()
...
...             sp2.rollback()
...             assert session['crom'] == 'Zope'
...             assert session['lech'] == 'Quack'
...
...             session['crom'] = 'Zorglub'
...             session['lech'] = 'Zimbabwe'
...
...             sp1.rollback()
...
...     # outside of a transaction, the writing is prohibited
...     with pytest.raises(SessionStateException) as e:
...         session['fail'] = True
...
...     assert e.value.message == (
...         "Session's current state disallows writing")
...
...     start_response('200 OK', [('Content-type', 'text/plain')])
...     return [session.get('crom', ''), session.get('lech', '')]
>>> wsgi_app = TestApp(session_wrapper(transactional_app, session_key=SK))
>>> result = wsgi_app.get('/bar')
>>> result.body
'CromLech'

Changelog

0.3b2 (2017-03-18)

  • Added python3 compatibility

0.3b1 (2016-11-29)

  • Added Timeout exception and bubbling up through the environ, to know when a session expired.

0.2 (2014-08-06)

  • Forwarding the local_conf options to the decorator, for further configuration

0.1 (2013-03-13)

  • Initial release

Release files for cromlech.wsgistate 0.3b2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cromlech.wsgistate 0.3b2
File Size Uploaded
cromlech.wsgistate-0.3b2.tar.gz 6.2 kB Details

Release files / cromlech.wsgistate-0.3b2.tar.gz

Download URL cromlech.wsgistate-0.3b2.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
6819119ca58b8f0ff8a81bc1cda21aa9d7a39787c27b57ba008c2a4a06bdbd7d
BLAKE2b-256 checksum
How to use checksums
13cbb5081ecc696dba826265ef9dfcba5a7eb9fb3714dbb4092acefee79383ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3b2 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page