Permalink
Browse files

make sure session_factory_from_settings properly coerces cookie_from_…

…exception to a boolean, and test; add docs and a changelog entry
  • Loading branch information...
1 parent ffa20f6 commit 2a075609f705060d5ecd39afaf33ebd2775ed7ed @mcdonc mcdonc committed Jan 4, 2011
Showing with 28 additions and 8 deletions.
  1. +5 −0 CHANGES.txt
  2. +6 −0 docs/index.rst
  3. +6 −7 pyramid_beaker/__init__.py
  4. +11 −1 pyramid_beaker/tests.py
View
@@ -3,6 +3,11 @@ Next release
- Allow cache regions to inherit URL configuration from main cache options.
+- Respect a ``cookie_on_exception`` configuration value passed to the
+ ``BeakerSessionFactoryConfig`` constructor. If this value is passed and
+ it's not false, session cookies will be set even on responses resulting
+ from an exception.
+
0.3 (2010-12-29)
----------------
View
@@ -33,6 +33,7 @@ parameters prefixed with ``session.`` in your Pyramid application's
session.lock_dir = %(here)s/data/sessions/lock
session.key = mykey
session.secret = mysecret
+ session.cookie_on_exception = true
If your ``.ini`` file has such settings, you can use
:func:`pyramid_beaker.session_factory_from_settings` in your
@@ -61,6 +62,11 @@ in the ``__init__.py`` of your Pyramid application that uses an
config.end()
return config.make_wsgi_app()
+The ``cookie_on_exception`` option is specific to ``pyramid_beaker`` (as
+opposed to all other options, which are specific to Beaker itself). It
+indicates that the session cookie should be set even when a Pyramid exception
+view is being rendered. The default is ``False``, meaning that session
+cookies will not be sent back in responses generated by exception views.
Beaker cache region support
```````````````````````````
@@ -5,26 +5,23 @@
from beaker.util import coerce_session_params
from pyramid.interfaces import ISession
+from pyramid.settings import asbool
from zope.interface import implements
def BeakerSessionFactoryConfig(**options):
""" Return a Pyramid session factory using Beaker session settings
supplied directly as ``**options``"""
- cookie_on_exception = options.pop('cookie_on_exception', False)
-
class PyramidBeakerSessionObject(SessionObject):
implements(ISession)
_options = options
- _cookie_on_exception = cookie_on_exception
-
+ _cookie_on_exception = _options.pop('cookie_on_exception', False)
def __init__(self, request):
SessionObject.__init__(self, request.environ, **self._options)
-
def session_callback(request, response):
exception = getattr(request, 'exception', None)
- if (exception is None or self._cookie_on_exception) \
- and self.accessed():
+ if (exception is None or self._cookie_on_exception
+ and self.accessed()):
self.persist()
headers = self.__dict__['_headers']
if headers['set_cookie'] and headers['cookie_out']:
@@ -115,6 +112,8 @@ def session_factory_from_settings(settings):
for prefix in prefixes:
if k.startswith(prefix):
option_name = k[len(prefix):]
+ if option_name == 'cookie_on_exception':
+ v = asbool(v)
options[option_name] = v
options = coerce_session_params(options)
@@ -166,7 +166,17 @@ def test_it(self):
settings = {'session.auto':'true', 'session.key':'foo'}
factory = self._callFUT(settings)
self.assertEqual(factory._options, {'auto':True, 'key':'foo'})
-
+
+ def test_cookie_on_exception_true(self):
+ settings = {'session.cookie_on_exception':'true'}
+ factory = self._callFUT(settings)
+ self.assertEqual(factory._cookie_on_exception, True)
+
+ def test_cookie_on_exception_false(self):
+ settings = {'session.cookie_on_exception':'false'}
+ factory = self._callFUT(settings)
+ self.assertEqual(factory._cookie_on_exception, False)
+
class DummyRequest:
def __init__(self):
self.callbacks = []

0 comments on commit 2a07560

Please sign in to comment.