improve documentation for session attributes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
add test for session attributes
authored by David Lord · committed by David Lord · parent 2bec58f · 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
Provenance
- Commit
0a33954- Tree
6962ea7- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
2bec58f
2 files changed+47 added−27 removed
modified:flask/sessions.py+37 added-26 removed
8 unmodified lines9 :license: BSD, see LICENSE for more details.10 """11 import hashlib12 import warnings13+from collections import MutableMapping14 from datetime import datetime15 16 from itsdangerous import BadSignature, URLSafeTimedSerializer17 from werkzeug.datastructures import CallbackDict1 unmodified line19 from flask.helpers import is_ip, total_seconds20 from flask.json.tag import TaggedJSONSerializer21 22 22-class SessionMixin(object):23- """Expands a basic dictionary with an accessors that are expected24- by Flask extensions and users for the session.25- """23+class SessionMixin(MutableMapping):24+ """Expands a basic dictionary with session attributes."""25 26+ @property27- def _get_permanent(self):27+ def permanent(self):28+ """This reflects the ``'_permanent'`` key in the dict."""29 return self.get('_permanent', False)30 31+ @permanent.setter30- def _set_permanent(self, value):32+ def permanent(self, value):33 self['_permanent'] = bool(value)34 33- #: this reflects the ``'_permanent'`` key in the dict.34- permanent = property(_get_permanent, _set_permanent)35- del _get_permanent, _set_permanent36-37- #: 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 = False39 42- #: 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 = True44 48- #: 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 = True49 50 51 class SecureCookieSession(CallbackDict, SessionMixin):58- """Base class for sessions based on signed cookies."""52+ """Base class for sessions based on signed cookies.53 54+ 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+ """59+60+ #: 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 = False66+67+ #: 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 = False72+73 def __init__(self, initial=None):74 def on_update(self):75 self.modified = True76 self.accessed = True77 78 super(SecureCookieSession, self).__init__(initial, on_update)66- self.modified = False67- self.accessed = False79 80 def __getitem__(self, key):81 self.accessed = True82 return super(SecureCookieSession, self).__getitem__(key)293 unmodified linesmodified:tests/test_basic.py+10 added-1 removed
219 unmodified lines220 221 def 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'230 231 @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 v239 240 assert client.post('/set', data={'value': '42'}).data == b'value set'241 assert client.get('/get').data == b'42'242 1621 unmodified lines