Encode/decode date and datetime in redis sessions

In several places, odoo sets a datetime object directly in the
session. It works with the default session handler of odoo which
uses pickle.

But datetime and date are not json serializable by default.

Add custom encoder / decoder to handle them (from
https://github.com/OCA/queue/blob/dc12a6a20ecfd15c5b90f9b089c9a64cf9d8bbe4/queue_job/fields.py#L57-L99)

See the discussion raised by @PCatinean on https://github.com/camptocamp/odoo-cloud-platform/pull/176
This commit is contained in:
Guewen Baconnier
2020-05-26 11:39:30 +02:00
parent a6f4e7990f
commit 2a022ba537
2 changed files with 47 additions and 2 deletions
+8 -2
View File
@@ -6,6 +6,8 @@ import logging
from werkzeug.contrib.sessions import SessionStore
from . import json_encoding
# this is equal to the duration of the session garbage collector in
# odoo.http.session_gc()
DEFAULT_SESSION_TIMEOUT = 60 * 60 * 24 * 7 # 7 days in seconds
@@ -57,7 +59,9 @@ class RedisSessionStore(SessionStore):
"expiration of %s seconds for %s",
key, expiration, user_msg)
data = json.dumps(dict(session)).encode('utf-8')
data = json.dumps(
dict(session), cls=json_encoding.SessionEncoder
).encode('utf-8')
if self.redis.set(key, data):
return self.redis.expire(key, expiration)
@@ -79,7 +83,9 @@ class RedisSessionStore(SessionStore):
"returning a new one", key)
return self.new()
try:
data = json.loads(saved.decode('utf-8'))
data = json.loads(
saved.decode('utf-8'), cls=json_encoding.SessionDecoder
)
except ValueError:
_logger.debug("session for key '%s' has been asked but its json "
"content could not be read, it has been reset", key)