使用wsgistate为cromlech处理会话
项目描述
cromlech.beaker是beaker与cromlech集成以进行会话管理。
上下文管理器
让我们导入一些辅助工具
>>> import pytest >>> from webtest import TestApp >>> SK = 'session.key'
然后使用wsgistate中间件运行它
>>> 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)]
如果应用程序抛出异常,上下文管理器不会保存会话
>>> 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'
测试事务感知
>>> result = wsgi_app.get('/fail') Traceback (most recent call last): ... ValueError >>> result.status '200 OK' >>> result.body '/foo, /bar'
变更日志
>>> 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'
0.3b2 (2017-03-18)
添加了python3兼容性
0.3b1 (2016-11-29)
添加了Timeout异常,并通过environ向上冒泡,以便知道会话何时过期。
将local_conf选项转发到装饰器,以进行进一步配置
0.2 (2014-08-06)
初始版本
0.1 (2013-03-13)
项目详情
此版本
cromlech.wsgistate-0.3b2.tar.gz的哈希
哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 6819119ca58b8f0ff8a81bc1cda21aa9d7a39787c27b57ba008c2a4a06bdbd7d |
|
MD5 | 47d38f4364748abbccff5f916d1630de |
|
BLAKE2b-256 | 13cbb5081ecc696dba826265ef9dfcba5a7eb9fb3714dbb4092acefee79383ae |