Skip to content
Myr

Added uuid support for new session serialization and documented it

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 38eed27 · Browse files · 3 files+21 added−0 removed
This commit

State

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

Diff stats

3 files+21 added−0 removed

  • other:+21 added−0 removed

3 files+21 added−0 removed

modified:docs/api.rst+12 added
222 unmodified lines
223224.. autoclass:: SessionMixin225 :members:226227.. autodata:: session_json_serializer228229 This object provides dumping and loading methods similar to simplejson230 but it also tags certain builtin Python objects that commonly appear in231 sessions. Currently the following extended values are supported in232 the JSON it dumps:233234 - :class:`~markupsafe.Markup` objects235 - :class:`~uuid.UUID` objects236 - :class:`~datetime.datetime` objects237 - :class:`tuple`\s238239.. admonition:: Notice240241 The ``PERMANENT_SESSION_LIFETIME`` config key can also be an integer242 starting with Flask 0.8. Either catch this down yourself or use
478 unmodified lines
modified:flask/testsuite/basic.py+4 added
10 unmodified lines
1112from __future__ import with_statement1314import re15import uuid16import flask17import pickle18import unittest19from datetime import datetime
299 unmodified lines
319 app = flask.Flask(__name__)320 app.secret_key = 'development-key'321 app.testing = True322 now = datetime.utcnow().replace(microsecond=0)323 the_uuid = uuid.uuid4()324325 @app.after_request326 def modify_session(response):327 flask.session['m'] = flask.Markup('Hello!')328 flask.session['u'] = the_uuid329 flask.session['dt'] = now330 flask.session['t'] = (1, 2, 3)331 return response332
6 unmodified lines
339 rv = pickle.loads(c.get('/').data)340 self.assert_equal(rv['m'], flask.Markup('Hello!'))341 self.assert_equal(type(rv['m']), flask.Markup)342 self.assert_equal(rv['dt'], now)343 self.assert_equal(rv['u'], the_uuid)344 self.assert_equal(rv['t'], (1, 2, 3))345346 def test_flashes(self):347 app = flask.Flask(__name__)
805 unmodified lines
modified:flask/sessions.py+5 added
7 unmodified lines
8 :copyright: (c) 2012 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112import uuid13import hashlib14from datetime import datetime15from werkzeug.http import http_date, parse_date16from werkzeug.datastructures import CallbackDict
41 unmodified lines
58 def dumps(self, value):59 def _tag(value):60 if isinstance(value, tuple):61 return {' t': [_tag(x) for x in value]}62 elif isinstance(value, uuid.UUID):63 return {' u': value.hex}64 elif callable(getattr(value, '__html__', None)):65 return {' m': unicode(value.__html__())}66 elif isinstance(value, list):67 return [_tag(x) for x in value]
18 unmodified lines
86 return obj87 the_key, the_value = obj.iteritems().next()88 if the_key == ' t':89 return tuple(the_value)90 elif the_key == ' u':91 return uuid.UUID(the_value)92 elif the_key == ' m':93 return Markup(the_value)94 elif the_key == ' d':95 return parse_date(the_value)
231 unmodified lines