Refactored session interface
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 1a61b12 · Browse files · 6 files·+59 added−49 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+59 added−49 removed
- other:+59 added−49 removed
Provenance
- Commit
0fccfe7- Tree
792ee4b- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
1a61b12
6 files changed+59 added−49 removed
modified:docs/api.rst+25 added
193 unmodified lines194 default is 31 days. If set to `False` (which is the default) the195 session will be deleted when the user closes the browser.196 197 198+Session Interface199+-----------------200+201+.. versionadded:: 0.7202+203+The session interface provides a simple way to replace the session204+implementation that Flask is using.205+206+.. currentmodule:: flask.sessions207+208+.. autoclass:: SessionInterface209+ :members:210+211+.. autoclass:: SecureCookieSessionInterface212+ :members:213+214+.. autoclass:: NullSession215+ :members:216+217+.. autoclass:: SessionMixin218+ :members:219+220+221 Application Globals222 -------------------223 224+.. currentmodule:: flask225+226 To share data that is valid for one request only from one function to227 another, a global variable is not good enough because it would break in228 threaded environments. Flask provides you with a special object that229 ensures it is only valid for the active request and that will return224 unmodified linesmodified:flask/__init__.py-1 removed
25 unmodified lines26 from .ctx import has_request_context27 from .module import Module28 from .blueprints import Blueprint29 from .templating import render_template, render_template_string30-from .session import Session30 31 # the signals32 from .signals import signals_available, template_rendered, request_started, \33 request_finished, got_request_exception, request_tearing_down4 unmodified linesmodified:flask/app.py+22 added-16 removed
25 unmodified lines26 from .wrappers import Request, Response27 from .config import ConfigAttribute, Config28 from .ctx import RequestContext29 from .globals import _request_ctx_stack, request30-from .session import Session, _NullSession30+from .sessions import SecureCookieSessionInterface31 from .module import blueprint_is_module32 from .templating import DispatchingJinjaLoader, Environment, \33 _default_template_ctx_processor34 from .signals import request_started, request_finished, got_request_exception, \175 unmodified lines210 #:211 #: .. versionadded:: 0.7212 test_client_class = None213 214+ #: the session interface to use. By default an instance of215+ #: :class:`~flask.sessions.SecureCookieSessionInterface` is used here.216+ #:217+ #: .. versionadded:: 0.7218+ session_interface = SecureCookieSessionInterface()219+220 def __init__(self, import_name, static_path=None, static_url_path=None,221 static_folder='static', template_folder='templates'):222 _PackageBoundObject.__init__(self, import_name,223 template_folder=template_folder)361 unmodified lines585 586 def open_session(self, request):587 """Creates or opens a new session. Default implementation stores all588 session data in a signed cookie. This requires that the583- :attr:`secret_key` is set.589+ :attr:`secret_key` is set. Instead of overriding this method590+ we recommend replacing the :class:`session_interface`.591 592 :param request: an instance of :attr:`request_class`.593 """587- key = self.secret_key588- if key is not None:589- return Session.load_cookie(request, self.session_cookie_name,590- secret_key=key)594+ return self.session_interface.open_session(self, request)595 596 def save_session(self, session, response):597 """Saves the session if it needs updates. For the default594- implementation, check :meth:`open_session`.598+ implementation, check :meth:`open_session`. Instead of overriding this599+ method we recommend replacing the :class:`session_interface`.600 601 :param session: the session to be saved (a602 :class:`~werkzeug.contrib.securecookie.SecureCookie`603 object)604 :param response: an instance of :attr:`response_class`605 """601- expires = domain = None602- if session.permanent:603- expires = datetime.utcnow() + self.permanent_session_lifetime604- if self.config['SERVER_NAME'] is not None:605- # chop of the port which is usually not supported by browsers606- domain = '.' + self.config['SERVER_NAME'].rsplit(':', 1)[0]607- session.save_cookie(response, self.session_cookie_name,608- expires=expires, httponly=True, domain=domain)606+ return self.session_interface.save_session(self, session, response)607 608+ def make_null_session(self):609+ """Creates a new instance of a missing session. Instead of overriding610+ this method we recommend replacing the :class:`session_interface`.611+612+ .. versionadded:: 0.7613+ """614+ return self.session_interface.make_null_session(self)615+616 def register_module(self, module, **options):617 """Registers a module with this application. The keyword argument618 of this function are the same as the ones for the constructor of the619 :class:`Module` class and will override the values of the module if569 unmodified lines1189 instance of :attr:`response_class`.1190 """1191 ctx = _request_ctx_stack.top1192 bp = ctx.request.blueprint1187- if not isinstance(ctx.session, _NullSession):1193+ if not self.session_interface.is_null_session(ctx.session):1194 self.save_session(ctx.session, response)1195 funcs = ()1196 if bp is not None and bp in self.after_request_funcs:1197 funcs = reversed(self.after_request_funcs[bp])115 unmodified linesmodified:flask/ctx.py+1 added-1 removed
120 unmodified lines121 # available. This allows a custom open_session method to use the122 # request context (e.g. flask-sqlalchemy).123 self.session = self.app.open_session(self.request)124 if self.session is None:125- self.session = _NullSession()125+ self.session = self.app.make_null_session()126 127 def pop(self):128 """Pops the request context and unbinds it by doing that. This will129 also trigger the execution of functions registered by the18 unmodified linesmodified:flask/session.py+7 added-31 removed
1 unmodified line2 """3 flask.session4 ~~~~~~~~~~~~~5 6- Implements cookie based sessions based on Werkzeug's secure cookie7- system.6+ This module used to flask with the session global so we moved it7+ over to flask.sessions8 9 :copyright: (c) 2010 by Armin Ronacher.10 :license: BSD, see LICENSE for more details.11 """12 13-from werkzeug.contrib.securecookie import SecureCookie13+from warnings import warn14+warn(DeprecationWarning('please use flask.sessions instead'))15 16+from .sessions import *17 16-class Session(SecureCookie):17- """Expands the session with support for switching between permanent18- and non-permanent sessions.19- """20-21- def _get_permanent(self):22- return self.get('_permanent', False)23-24- def _set_permanent(self, value):25- self['_permanent'] = bool(value)26-27- permanent = property(_get_permanent, _set_permanent)28- del _get_permanent, _set_permanent29-30-31-class _NullSession(Session):32- """Class used to generate nicer error messages if sessions are not33- available. Will still allow read-only access to the empty session34- but fail on setting.35- """36-37- def _fail(self, *args, **kwargs):38- raise RuntimeError('the session is unavailable because no secret '39- 'key was set. Set the secret_key on the '40- 'application to something unique and secret.')41- __setitem__ = __delitem__ = clear = pop = popitem = \42- update = setdefault = _fail43- del _fail18+Session = SecureCookieSession19+_NullSession = NullSessionmodified:CHANGES+4 added
6 unmodified lines7 -----------8 9 Relase date to be decided, codename to be chosen.10 11+- Refactored session support into a session interface so that12+ the implementation of the sessions can be changed without13+ having to override the Flask class.14+15 Version 0.7.216 -------------17 18 Bugfix release, released on July 6th 2011229 unmodified lines