Skip to content
Myr

Merge pull request #2580 from pallets/session-attrs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

improve documentation for session attributes

authored by David Lord · committed by GitHub · parents 54f3d21, 0a33954 · Browse files · 2 files+47 added−27 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+47 added−27 removed

  • tests:+10 added−1 removed
  • other:+37 added−26 removed

2 files+47 added−27 removed

modified:flask/sessions.py+37 added-26 removed
8 unmodified lines
9 :license: BSD, see LICENSE for more details.10"""11import hashlib12import warnings13from collections import MutableMapping14from datetime import datetime1516from itsdangerous import BadSignature, URLSafeTimedSerializer17from werkzeug.datastructures import CallbackDict
1 unmodified line
19from flask.helpers import is_ip, total_seconds20from flask.json.tag import TaggedJSONSerializer212222class SessionMixin(object):23 """Expands a basic dictionary with an accessors that are expected24 by Flask extensions and users for the session.25 """23class SessionMixin(MutableMapping):24 """Expands a basic dictionary with session attributes."""2526 @property27 def _get_permanent(self):27 def permanent(self):28 """This reflects the ``'_permanent'`` key in the dict."""29 return self.get('_permanent', False)3031 @permanent.setter30 def _set_permanent(self, value):32 def permanent(self, value):33 self['_permanent'] = bool(value)3433 #: this reflects the ``'_permanent'`` key in the dict.34 permanent = property(_get_permanent, _set_permanent)35 del _get_permanent, _set_permanent3637 #: some session backends can tell you if a session is new, but that is38 #: not necessarily guaranteed. Use with caution. The default mixin39 #: implementation just hardcodes ``False`` in.35 #: Some implementations can detect whether a session is newly36 #: created, but that is not guaranteed. Use with caution. The mixin37 # default is hard-coded ``False``.38 new = False3942 #: for some backends this will always be ``True``, but some backends will43 #: default this to false and detect changes in the dictionary for as44 #: long as changes do not happen on mutable structures in the session.45 #: The default mixin implementation just hardcodes ``True`` in.40 #: Some implementations can detect changes to the session and set41 #: this when that happens. The mixin default is hard coded to42 #: ``True``.43 modified = True4448 #: the accessed variable indicates whether or not the session object has49 #: been accessed in that request. This allows flask to append a `Vary:50 #: Cookie` header to the response if the session is being accessed. This51 #: allows caching proxy servers, like Varnish, to use both the URL and the52 #: session cookie as keys when caching pages, preventing multiple users53 #: from being served the same cache.45 #: Some implementations can detect when session data is read or46 #: written and set this when that happens. The mixin default is hard47 #: coded to ``True``.48 accessed = True495051class SecureCookieSession(CallbackDict, SessionMixin):58 """Base class for sessions based on signed cookies."""52 """Base class for sessions based on signed cookies.5354 This session backend will set the :attr:`modified` and55 :attr:`accessed` attributes. It cannot reliably track whether a56 session is new (vs. empty), so :attr:`new` remains hard coded to57 ``False``.58 """5960 #: When data is changed, this is set to ``True``. Only the session61 #: dictionary itself is tracked; if the session contains mutable62 #: data (for example a nested dict) then this must be set to63 #: ``True`` manually when modifying that data. The session cookie64 #: will only be written to the response if this is ``True``.65 modified = False6667 #: When data is read or written, this is set to ``True``. Used by68 # :class:`.SecureCookieSessionInterface` to add a ``Vary: Cookie``69 #: header, which allows caching proxies to cache different pages for70 #: different users.71 accessed = False7273 def __init__(self, initial=None):74 def on_update(self):75 self.modified = True76 self.accessed = True7778 super(SecureCookieSession, self).__init__(initial, on_update)66 self.modified = False67 self.accessed = False7980 def __getitem__(self, key):81 self.accessed = True82 return super(SecureCookieSession, self).__getitem__(key)
293 unmodified lines
modified:tests/test_basic.py+10 added-1 removed
219 unmodified lines
220221def test_session(app, client):222 @app.route('/set', methods=['POST'])223 def set():224 assert not flask.session.accessed225 assert not flask.session.modified226 flask.session['value'] = flask.request.form['value']227 assert flask.session.accessed228 assert flask.session.modified229 return 'value set'230231 @app.route('/get')232 def get():229 return flask.session['value']233 assert not flask.session.accessed234 assert not flask.session.modified235 v = flask.session.get('value', 'None')236 assert flask.session.accessed237 assert not flask.session.modified238 return v239240 assert client.post('/set', data={'value': '42'}).data == b'value set'241 assert client.get('/get').data == b'42'242
1621 unmodified lines