Added uuid support for new session serialization and documented it

This commit is contained in:
Armin Ronacher 2013-05-14 11:33:36 +01:00
parent 38eed272f9
commit 18673ba370
3 changed files with 21 additions and 0 deletions

View file

@ -224,6 +224,18 @@ implementation that Flask is using.
.. autoclass:: SessionMixin
:members:
.. autodata:: session_json_serializer
This object provides dumping and loading methods similar to simplejson
but it also tags certain builtin Python objects that commonly appear in
sessions. Currently the following extended values are supported in
the JSON it dumps:
- :class:`~markupsafe.Markup` objects
- :class:`~uuid.UUID` objects
- :class:`~datetime.datetime` objects
- :class:`tuple`\s
.. admonition:: Notice
The ``PERMANENT_SESSION_LIFETIME`` config key can also be an integer

View file

@ -9,6 +9,7 @@
:license: BSD, see LICENSE for more details.
"""
import uuid
import hashlib
from datetime import datetime
from werkzeug.http import http_date, parse_date
@ -58,6 +59,8 @@ class TaggedJSONSerializer(object):
def _tag(value):
if isinstance(value, tuple):
return {' t': [_tag(x) for x in value]}
elif isinstance(value, uuid.UUID):
return {' u': value.hex}
elif callable(getattr(value, '__html__', None)):
return {' m': unicode(value.__html__())}
elif isinstance(value, list):
@ -84,6 +87,8 @@ class TaggedJSONSerializer(object):
the_key, the_value = obj.iteritems().next()
if the_key == ' t':
return tuple(the_value)
elif the_key == ' u':
return uuid.UUID(the_value)
elif the_key == ' m':
return Markup(the_value)
elif the_key == ' d':

View file

@ -12,6 +12,7 @@
from __future__ import with_statement
import re
import uuid
import flask
import pickle
import unittest
@ -319,10 +320,12 @@ class BasicFunctionalityTestCase(FlaskTestCase):
app.secret_key = 'development-key'
app.testing = True
now = datetime.utcnow().replace(microsecond=0)
the_uuid = uuid.uuid4()
@app.after_request
def modify_session(response):
flask.session['m'] = flask.Markup('Hello!')
flask.session['u'] = the_uuid
flask.session['dt'] = now
flask.session['t'] = (1, 2, 3)
return response
@ -337,6 +340,7 @@ class BasicFunctionalityTestCase(FlaskTestCase):
self.assert_equal(rv['m'], flask.Markup('Hello!'))
self.assert_equal(type(rv['m']), flask.Markup)
self.assert_equal(rv['dt'], now)
self.assert_equal(rv['u'], the_uuid)
self.assert_equal(rv['t'], (1, 2, 3))
def test_flashes(self):