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
Provenance
- Commit
18673ba- Tree
098f35f- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
38eed27
3 files changed+21 added−0 removed
modified:docs/api.rst+12 added
222 unmodified lines223 224 .. autoclass:: SessionMixin225 :members:226 227+.. autodata:: session_json_serializer228+229+ 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:233+234+ - :class:`~markupsafe.Markup` objects235+ - :class:`~uuid.UUID` objects236+ - :class:`~datetime.datetime` objects237+ - :class:`tuple`\s238+239 .. admonition:: Notice240 241 The ``PERMANENT_SESSION_LIFETIME`` config key can also be an integer242 starting with Flask 0.8. Either catch this down yourself or use478 unmodified linesmodified:flask/testsuite/basic.py+4 added
10 unmodified lines11 12 from __future__ import with_statement13 14 import re15+import uuid16 import flask17 import pickle18 import unittest19 from datetime import datetime299 unmodified lines319 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()324 325 @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 lines339 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))345 346 def test_flashes(self):347 app = flask.Flask(__name__)805 unmodified linesmodified:flask/sessions.py+5 added
7 unmodified lines8 :copyright: (c) 2012 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11 12+import uuid13 import hashlib14 from datetime import datetime15 from werkzeug.http import http_date, parse_date16 from werkzeug.datastructures import CallbackDict41 unmodified lines58 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 lines86 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