Simplified MessageEncoder instantiation with default separators.
This commit is contained in:
parent
fe9c7ded29
commit
d10425f9c7
|
@ -14,6 +14,10 @@ class MessageEncoder(json.JSONEncoder):
|
||||||
"""
|
"""
|
||||||
message_key = '__json_message'
|
message_key = '__json_message'
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
kwargs.setdefault('separators', (',', ':'))
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def default(self, obj):
|
def default(self, obj):
|
||||||
if isinstance(obj, Message):
|
if isinstance(obj, Message):
|
||||||
# Using 0/1 here instead of False/True to produce more compact json
|
# Using 0/1 here instead of False/True to produce more compact json
|
||||||
|
@ -148,7 +152,7 @@ class CookieStorage(BaseStorage):
|
||||||
also contains a hash to ensure that the data was not tampered with.
|
also contains a hash to ensure that the data was not tampered with.
|
||||||
"""
|
"""
|
||||||
if messages or encode_empty:
|
if messages or encode_empty:
|
||||||
encoder = MessageEncoder(separators=(',', ':'))
|
encoder = MessageEncoder()
|
||||||
value = encoder.encode(messages)
|
value = encoder.encode(messages)
|
||||||
return self.signer.sign(value)
|
return self.signer.sign(value)
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ class SessionStorage(BaseStorage):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def serialize_messages(self, messages):
|
def serialize_messages(self, messages):
|
||||||
encoder = MessageEncoder(separators=(',', ':'))
|
encoder = MessageEncoder()
|
||||||
return encoder.encode(messages)
|
return encoder.encode(messages)
|
||||||
|
|
||||||
def deserialize_messages(self, data):
|
def deserialize_messages(self, data):
|
||||||
|
|
|
@ -141,7 +141,7 @@ class CookieTests(BaseTests, SimpleTestCase):
|
||||||
},
|
},
|
||||||
Message(constants.INFO, 'message %s'),
|
Message(constants.INFO, 'message %s'),
|
||||||
]
|
]
|
||||||
encoder = MessageEncoder(separators=(',', ':'))
|
encoder = MessageEncoder()
|
||||||
value = encoder.encode(messages)
|
value = encoder.encode(messages)
|
||||||
decoded_messages = json.loads(value, cls=MessageDecoder)
|
decoded_messages = json.loads(value, cls=MessageDecoder)
|
||||||
self.assertEqual(messages, decoded_messages)
|
self.assertEqual(messages, decoded_messages)
|
||||||
|
@ -166,7 +166,7 @@ class CookieTests(BaseTests, SimpleTestCase):
|
||||||
storage = self.storage_class(self.get_request())
|
storage = self.storage_class(self.get_request())
|
||||||
messages = ['this', 'that']
|
messages = ['this', 'that']
|
||||||
# Encode/decode a message using the pre-Django 3.1 hash.
|
# Encode/decode a message using the pre-Django 3.1 hash.
|
||||||
encoder = MessageEncoder(separators=(',', ':'))
|
encoder = MessageEncoder()
|
||||||
value = encoder.encode(messages)
|
value = encoder.encode(messages)
|
||||||
encoded_messages = '%s$%s' % (storage._legacy_hash(value), value)
|
encoded_messages = '%s$%s' % (storage._legacy_hash(value), value)
|
||||||
decoded_messages = storage._decode(encoded_messages)
|
decoded_messages = storage._decode(encoded_messages)
|
||||||
|
|
Loading…
Reference in New Issue