Show More
@@ -3,11 +3,10 b'' | |||||
3 | # License: BSD-derived (http://www.repoze.org/LICENSE.txt) |
|
3 | # License: BSD-derived (http://www.repoze.org/LICENSE.txt) | |
4 | # With Patches from RhodeCode GmBH |
|
4 | # With Patches from RhodeCode GmBH | |
5 |
|
5 | |||
6 |
|
||||
7 | import os |
|
6 | import os | |
8 |
|
7 | |||
9 | from beaker import cache |
|
8 | from beaker import cache | |
10 | from beaker.session import SessionObject |
|
9 | from beaker.session import SessionObject, Session | |
11 | from beaker.util import coerce_cache_params |
|
10 | from beaker.util import coerce_cache_params | |
12 | from beaker.util import coerce_session_params |
|
11 | from beaker.util import coerce_session_params | |
13 |
|
12 | |||
@@ -18,6 +17,11 b' from zope.interface import implementer' | |||||
18 | from binascii import hexlify |
|
17 | from binascii import hexlify | |
19 |
|
18 | |||
20 |
|
19 | |||
|
20 | ||||
|
21 | class CustomSession(Session): | |||
|
22 | pass | |||
|
23 | ||||
|
24 | ||||
21 | def BeakerSessionFactoryConfig(**options): |
|
25 | def BeakerSessionFactoryConfig(**options): | |
22 | """ Return a Pyramid session factory using Beaker session settings |
|
26 | """ Return a Pyramid session factory using Beaker session settings | |
23 | supplied directly as ``**options``""" |
|
27 | supplied directly as ``**options``""" | |
@@ -26,8 +30,11 b' def BeakerSessionFactoryConfig(**options' | |||||
26 | _options = options |
|
30 | _options = options | |
27 | _cookie_on_exception = _options.pop('cookie_on_exception', True) |
|
31 | _cookie_on_exception = _options.pop('cookie_on_exception', True) | |
28 | _constant_csrf_token = _options.pop('constant_csrf_token', False) |
|
32 | _constant_csrf_token = _options.pop('constant_csrf_token', False) | |
|
33 | _sa_opts = _options.pop('sa_opts', {}) | |||
29 |
|
34 | |||
30 | def __init__(self, request): |
|
35 | def __init__(self, request): | |
|
36 | self._options['session_class'] = CustomSession | |||
|
37 | self._options['sa_opts'] = self._sa_opts | |||
31 | SessionObject.__init__(self, request.environ, **self._options) |
|
38 | SessionObject.__init__(self, request.environ, **self._options) | |
32 |
|
39 | |||
33 | def session_callback(_request, _response): |
|
40 | def session_callback(_request, _response): | |
@@ -93,12 +100,12 b' def BeakerSessionFactoryConfig(**options' | |||||
93 |
|
100 | |||
94 | # Flash API methods |
|
101 | # Flash API methods | |
95 | def flash(self, msg, queue='', allow_duplicate=True): |
|
102 | def flash(self, msg, queue='', allow_duplicate=True): | |
96 |
storage = self.setdefault('_f_' |
|
103 | storage = self.setdefault(f'_f_{queue}', []) | |
97 | if allow_duplicate or (msg not in storage): |
|
104 | if allow_duplicate or (msg not in storage): | |
98 | storage.append(msg) |
|
105 | storage.append(msg) | |
99 |
|
106 | |||
100 | def pop_flash(self, queue=''): |
|
107 | def pop_flash(self, queue=''): | |
101 |
storage = self.pop('_f_' |
|
108 | storage = self.pop(f'_f_{queue}', []) | |
102 | return storage |
|
109 | return storage | |
103 |
|
110 | |||
104 | def peek_flash(self, queue=''): |
|
111 | def peek_flash(self, queue=''): | |
@@ -140,6 +147,10 b' def session_factory_from_settings(settin' | |||||
140 | prefixes = ('session.', 'beaker.session.') |
|
147 | prefixes = ('session.', 'beaker.session.') | |
141 | options = {} |
|
148 | options = {} | |
142 |
|
149 | |||
|
150 | # custom gather of our specific sqlalchemy session db configuration we need to translate this into a single entry | |||
|
151 | # dict because this is how beaker expects that. | |||
|
152 | sa_opts = {} | |||
|
153 | ||||
143 | # Pull out any config args meant for beaker session. if there are any |
|
154 | # Pull out any config args meant for beaker session. if there are any | |
144 | for k, v in settings.items(): |
|
155 | for k, v in settings.items(): | |
145 | for prefix in prefixes: |
|
156 | for prefix in prefixes: | |
@@ -147,9 +158,12 b' def session_factory_from_settings(settin' | |||||
147 | option_name = k[len(prefix):] |
|
158 | option_name = k[len(prefix):] | |
148 | if option_name == 'cookie_on_exception': |
|
159 | if option_name == 'cookie_on_exception': | |
149 | v = asbool(v) |
|
160 | v = asbool(v) | |
|
161 | if option_name.startswith('sa.'): | |||
|
162 | sa_opts[option_name] = v | |||
150 | options[option_name] = v |
|
163 | options[option_name] = v | |
151 |
|
164 | |||
152 | options = coerce_session_params(options) |
|
165 | options = coerce_session_params(options) | |
|
166 | options['sa_opts'] = sa_opts | |||
153 | return BeakerSessionFactoryConfig(**options) |
|
167 | return BeakerSessionFactoryConfig(**options) | |
154 |
|
168 | |||
155 |
|
169 |
General Comments 0
You need to be logged in to leave comments.
Login now