Skip to content
Myr

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

6 files+59 added−49 removed

modified:docs/api.rst+25 added
193 unmodified lines
194 default is 31 days. If set to `False` (which is the default) the195 session will be deleted when the user closes the browser.196197198Session Interface199-----------------200201.. versionadded:: 0.7202203The session interface provides a simple way to replace the session204implementation that Flask is using.205206.. currentmodule:: flask.sessions207208.. autoclass:: SessionInterface209 :members:210211.. autoclass:: SecureCookieSessionInterface212 :members:213214.. autoclass:: NullSession215 :members:216217.. autoclass:: SessionMixin218 :members:219220221Application Globals222-------------------223224.. currentmodule:: flask225226To share data that is valid for one request only from one function to227another, a global variable is not good enough because it would break in228threaded environments. Flask provides you with a special object that229ensures it is only valid for the active request and that will return
224 unmodified lines
modified:flask/__init__.py-1 removed
25 unmodified lines
26from .ctx import has_request_context27from .module import Module28from .blueprints import Blueprint29from .templating import render_template, render_template_string30from .session import Session3031# the signals32from .signals import signals_available, template_rendered, request_started, \33 request_finished, got_request_exception, request_tearing_down
4 unmodified lines
modified:flask/app.py+22 added-16 removed
25 unmodified lines
26from .wrappers import Request, Response27from .config import ConfigAttribute, Config28from .ctx import RequestContext29from .globals import _request_ctx_stack, request30from .session import Session, _NullSession30from .sessions import SecureCookieSessionInterface31from .module import blueprint_is_module32from .templating import DispatchingJinjaLoader, Environment, \33 _default_template_ctx_processor34from .signals import request_started, request_finished, got_request_exception, \
175 unmodified lines
210 #:211 #: .. versionadded:: 0.7212 test_client_class = None213214 #: 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()219220 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 lines
585586 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`.591592 :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)595596 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`.600601 :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)607608 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`.611612 .. versionadded:: 0.7613 """614 return self.session_interface.make_null_session(self)615616 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 if
569 unmodified lines
1189 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 lines
modified:flask/ctx.py+1 added-1 removed
120 unmodified lines
121 # 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()126127 def pop(self):128 """Pops the request context and unbinds it by doing that. This will129 also trigger the execution of functions registered by the
18 unmodified lines
modified:flask/session.py+7 added-31 removed
1 unmodified line
2"""3 flask.session4 ~~~~~~~~~~~~~56 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.sessions89 :copyright: (c) 2010 by Armin Ronacher.10 :license: BSD, see LICENSE for more details.11"""1213from werkzeug.contrib.securecookie import SecureCookie13from warnings import warn14warn(DeprecationWarning('please use flask.sessions instead'))1516from .sessions import *1716class Session(SecureCookie):17 """Expands the session with support for switching between permanent18 and non-permanent sessions.19 """2021 def _get_permanent(self):22 return self.get('_permanent', False)2324 def _set_permanent(self, value):25 self['_permanent'] = bool(value)2627 permanent = property(_get_permanent, _set_permanent)28 del _get_permanent, _set_permanent293031class _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 """3637 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 _fail18Session = SecureCookieSession19_NullSession = NullSession
modified:CHANGES+4 added
6 unmodified lines
7-----------89Relase date to be decided, codename to be chosen.1011- Refactored session support into a session interface so that12 the implementation of the sessions can be changed without13 having to override the Flask class.1415Version 0.7.216-------------1718Bugfix release, released on July 6th 2011
229 unmodified lines