Browse Source

Added uuid support for new session serialization and documented it

pull/696/merge
Armin Ronacher 12 years ago
parent
commit
18673ba370
  1. 12
      docs/api.rst
  2. 5
      flask/sessions.py
  3. 4
      flask/testsuite/basic.py

12
docs/api.rst

@ -224,6 +224,18 @@ implementation that Flask is using.
.. autoclass:: SessionMixin .. autoclass:: SessionMixin
:members: :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 .. admonition:: Notice
The ``PERMANENT_SESSION_LIFETIME`` config key can also be an integer The ``PERMANENT_SESSION_LIFETIME`` config key can also be an integer

5
flask/sessions.py

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

4
flask/testsuite/basic.py

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

Loading…
Cancel
Save