Browse Source

Merge pull request #2711 from pope1ni/session-serializer-fixes

Minor session serializer fixes.
pull/2717/head
David Lord 7 years ago committed by GitHub
parent
commit
324b4ea255
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      flask/json/tag.py
  2. 16
      tests/test_json_tag.py

10
flask/json/tag.py

@ -36,7 +36,7 @@ processes dicts first, so insert the new tag at the front of the order since
def to_python(self, value): def to_python(self, value):
return OrderedDict(value) return OrderedDict(value)
app.session_interface.serializer.register(TagOrderedDict, 0) app.session_interface.serializer.register(TagOrderedDict, index=0)
:copyright: © 2010 by the Pallets team. :copyright: © 2010 by the Pallets team.
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
@ -243,7 +243,7 @@ class TaggedJSONSerializer(object):
for cls in self.default_tags: for cls in self.default_tags:
self.register(cls) self.register(cls)
def register(self, tag_class, force=False, index=-1): def register(self, tag_class, force=False, index=None):
"""Register a new tag with this serializer. """Register a new tag with this serializer.
:param tag_class: tag class to register. Will be instantiated with this :param tag_class: tag class to register. Will be instantiated with this
@ -251,8 +251,8 @@ class TaggedJSONSerializer(object):
:param force: overwrite an existing tag. If false (default), a :param force: overwrite an existing tag. If false (default), a
:exc:`KeyError` is raised. :exc:`KeyError` is raised.
:param index: index to insert the new tag in the tag order. Useful when :param index: index to insert the new tag in the tag order. Useful when
the new tag is a special case of an existing tag. If -1 (default), the new tag is a special case of an existing tag. If ``None``
the tag is appended to the end of the order. (default), the tag is appended to the end of the order.
:raise KeyError: if the tag key is already registered and ``force`` is :raise KeyError: if the tag key is already registered and ``force`` is
not true. not true.
@ -266,7 +266,7 @@ class TaggedJSONSerializer(object):
self.tags[key] = tag self.tags[key] = tag
if index == -1: if index is None:
self.order.append(tag) self.order.append(tag)
else: else:
self.order.insert(index, tag) self.order.insert(index, tag)

16
tests/test_json_tag.py

@ -72,3 +72,19 @@ def test_tag_interface():
pytest.raises(NotImplementedError, t.check, None) pytest.raises(NotImplementedError, t.check, None)
pytest.raises(NotImplementedError, t.to_json, None) pytest.raises(NotImplementedError, t.to_json, None)
pytest.raises(NotImplementedError, t.to_python, None) pytest.raises(NotImplementedError, t.to_python, None)
def test_tag_order():
class Tag1(JSONTag):
key = ' 1'
class Tag2(JSONTag):
key = ' 2'
s = TaggedJSONSerializer()
s.register(Tag1, index=-1)
assert isinstance(s.order[-2], Tag1)
s.register(Tag2, index=None)
assert isinstance(s.order[-1], Tag2)

Loading…
Cancel
Save