mirror of
https://github.com/camptocamp/odoo-cloud-platform.git
synced 2026-06-24 08:47:40 +00:00
Change CI to GitHub actions
Use copier template from oca/oca-addons-repo-template Target Python3.8 Apply linting Fix a missing call to super
This commit is contained in:
@@ -1,3 +1,2 @@
|
||||
|
||||
from . import http
|
||||
from . import session
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
# Copyright 2016-2019 Camptocamp SA
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
|
||||
|
||||
|
||||
{'name': 'Sessions in Redis',
|
||||
'summary': 'Store web sessions in Redis',
|
||||
'version': "14.0.1.0.0",
|
||||
'author': 'Camptocamp,Odoo Community Association (OCA)',
|
||||
'license': 'AGPL-3',
|
||||
'category': 'Extra Tools',
|
||||
'depends': ['base'],
|
||||
'external_dependencies': {
|
||||
'python': ['redis'],
|
||||
},
|
||||
'website': 'http://www.camptocamp.com',
|
||||
'data': [],
|
||||
'installable': True,
|
||||
}
|
||||
{
|
||||
"name": "Sessions in Redis",
|
||||
"summary": "Store web sessions in Redis",
|
||||
"version": "14.0.1.0.0",
|
||||
"author": "Camptocamp,Odoo Community Association (OCA)",
|
||||
"license": "AGPL-3",
|
||||
"category": "Extra Tools",
|
||||
"depends": ["base"],
|
||||
"external_dependencies": {"python": ["redis"]},
|
||||
"website": "https://github.com/camptocamp/odoo-cloud-platform",
|
||||
"data": [],
|
||||
"installable": True,
|
||||
}
|
||||
|
||||
+34
-27
@@ -3,7 +3,6 @@
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from distutils.util import strtobool
|
||||
|
||||
import odoo
|
||||
@@ -23,46 +22,46 @@ except ImportError:
|
||||
|
||||
|
||||
def is_true(strval):
|
||||
return bool(strtobool(strval or '0'.lower()))
|
||||
return bool(strtobool(strval or "0".lower()))
|
||||
|
||||
|
||||
sentinel_host = os.environ.get('ODOO_SESSION_REDIS_SENTINEL_HOST')
|
||||
sentinel_master_name = os.environ.get(
|
||||
'ODOO_SESSION_REDIS_SENTINEL_MASTER_NAME'
|
||||
)
|
||||
sentinel_host = os.environ.get("ODOO_SESSION_REDIS_SENTINEL_HOST")
|
||||
sentinel_master_name = os.environ.get("ODOO_SESSION_REDIS_SENTINEL_MASTER_NAME")
|
||||
if sentinel_host and not sentinel_master_name:
|
||||
raise Exception(
|
||||
"ODOO_SESSION_REDIS_SENTINEL_MASTER_NAME must be defined "
|
||||
"when using session_redis"
|
||||
)
|
||||
sentinel_port = int(os.environ.get('ODOO_SESSION_REDIS_SENTINEL_PORT', 26379))
|
||||
host = os.environ.get('ODOO_SESSION_REDIS_HOST', 'localhost')
|
||||
port = int(os.environ.get('ODOO_SESSION_REDIS_PORT', 6379))
|
||||
prefix = os.environ.get('ODOO_SESSION_REDIS_PREFIX')
|
||||
url = os.environ.get('ODOO_SESSION_REDIS_URL')
|
||||
password = os.environ.get('ODOO_SESSION_REDIS_PASSWORD')
|
||||
expiration = os.environ.get('ODOO_SESSION_REDIS_EXPIRATION')
|
||||
anon_expiration = os.environ.get('ODOO_SESSION_REDIS_EXPIRATION_ANONYMOUS')
|
||||
sentinel_port = int(os.environ.get("ODOO_SESSION_REDIS_SENTINEL_PORT", 26379))
|
||||
host = os.environ.get("ODOO_SESSION_REDIS_HOST", "localhost")
|
||||
port = int(os.environ.get("ODOO_SESSION_REDIS_PORT", 6379))
|
||||
prefix = os.environ.get("ODOO_SESSION_REDIS_PREFIX")
|
||||
url = os.environ.get("ODOO_SESSION_REDIS_URL")
|
||||
password = os.environ.get("ODOO_SESSION_REDIS_PASSWORD")
|
||||
expiration = os.environ.get("ODOO_SESSION_REDIS_EXPIRATION")
|
||||
anon_expiration = os.environ.get("ODOO_SESSION_REDIS_EXPIRATION_ANONYMOUS")
|
||||
|
||||
|
||||
@lazy_property
|
||||
def session_store(self):
|
||||
if sentinel_host:
|
||||
sentinel = Sentinel([(sentinel_host, sentinel_port)],
|
||||
password=password)
|
||||
sentinel = Sentinel([(sentinel_host, sentinel_port)], password=password)
|
||||
redis_client = sentinel.master_for(sentinel_master_name)
|
||||
elif url:
|
||||
redis_client = redis.from_url(url)
|
||||
else:
|
||||
redis_client = redis.Redis(host=host, port=port, password=password)
|
||||
return RedisSessionStore(redis=redis_client, prefix=prefix,
|
||||
expiration=expiration,
|
||||
anon_expiration=anon_expiration,
|
||||
session_class=http.OpenERPSession)
|
||||
return RedisSessionStore(
|
||||
redis=redis_client,
|
||||
prefix=prefix,
|
||||
expiration=expiration,
|
||||
anon_expiration=anon_expiration,
|
||||
session_class=http.OpenERPSession,
|
||||
)
|
||||
|
||||
|
||||
def session_gc(session_store):
|
||||
""" Do not garbage collect the sessions
|
||||
"""Do not garbage collect the sessions
|
||||
|
||||
Redis keys are automatically cleaned at the end of their
|
||||
expiration.
|
||||
@@ -79,14 +78,22 @@ def purge_fs_sessions(path):
|
||||
pass
|
||||
|
||||
|
||||
if is_true(os.environ.get('ODOO_SESSION_REDIS')):
|
||||
if is_true(os.environ.get("ODOO_SESSION_REDIS")):
|
||||
if sentinel_host:
|
||||
_logger.debug("HTTP sessions stored in Redis with prefix '%s'. "
|
||||
"Using Sentinel on %s:%s",
|
||||
prefix or '', sentinel_host, sentinel_port)
|
||||
_logger.debug(
|
||||
"HTTP sessions stored in Redis with prefix '%s'. "
|
||||
"Using Sentinel on %s:%s",
|
||||
prefix or "",
|
||||
sentinel_host,
|
||||
sentinel_port,
|
||||
)
|
||||
else:
|
||||
_logger.debug("HTTP sessions stored in Redis with prefix '%s' on "
|
||||
"%s:%s", prefix or '', host, port)
|
||||
_logger.debug(
|
||||
"HTTP sessions stored in Redis with prefix '%s' on " "%s:%s",
|
||||
prefix or "",
|
||||
host,
|
||||
port,
|
||||
)
|
||||
|
||||
http.Root.session_store = session_store
|
||||
http.session_gc = session_gc
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
|
||||
|
||||
import json
|
||||
|
||||
from datetime import date, datetime
|
||||
|
||||
import dateutil
|
||||
|
||||
+40
-28
@@ -17,10 +17,16 @@ _logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RedisSessionStore(SessionStore):
|
||||
""" SessionStore that saves session to redis """
|
||||
"""SessionStore that saves session to redis"""
|
||||
|
||||
def __init__(self, redis, session_class=None,
|
||||
prefix='', expiration=None, anon_expiration=None):
|
||||
def __init__(
|
||||
self,
|
||||
redis,
|
||||
session_class=None,
|
||||
prefix="",
|
||||
expiration=None,
|
||||
anon_expiration=None,
|
||||
):
|
||||
super().__init__(session_class=session_class)
|
||||
self.redis = redis
|
||||
if expiration is None:
|
||||
@@ -31,14 +37,12 @@ class RedisSessionStore(SessionStore):
|
||||
self.anon_expiration = DEFAULT_SESSION_TIMEOUT_ANONYMOUS
|
||||
else:
|
||||
self.anon_expiration = anon_expiration
|
||||
self.prefix = 'session:'
|
||||
self.prefix = "session:"
|
||||
if prefix:
|
||||
self.prefix = '%s:%s:' % (
|
||||
self.prefix, prefix
|
||||
)
|
||||
self.prefix = "%s:%s:" % (self.prefix, prefix)
|
||||
|
||||
def build_key(self, sid):
|
||||
return '%s%s' % (self.prefix, sid)
|
||||
return "%s%s" % (self.prefix, sid)
|
||||
|
||||
def save(self, session):
|
||||
key = self.build_key(session.sid)
|
||||
@@ -51,48 +55,56 @@ class RedisSessionStore(SessionStore):
|
||||
expiration = session.expiration or self.anon_expiration
|
||||
if _logger.isEnabledFor(logging.DEBUG):
|
||||
if session.uid:
|
||||
user_msg = "user '%s' (id: %s)" % (
|
||||
session.login, session.uid)
|
||||
user_msg = "user '%s' (id: %s)" % (session.login, session.uid)
|
||||
else:
|
||||
user_msg = "anonymous user"
|
||||
_logger.debug("saving session with key '%s' and "
|
||||
"expiration of %s seconds for %s",
|
||||
key, expiration, user_msg)
|
||||
_logger.debug(
|
||||
"saving session with key '%s' and " "expiration of %s seconds for %s",
|
||||
key,
|
||||
expiration,
|
||||
user_msg,
|
||||
)
|
||||
|
||||
data = json.dumps(
|
||||
dict(session), cls=json_encoding.SessionEncoder
|
||||
).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)
|
||||
|
||||
def delete(self, session):
|
||||
key = self.build_key(session.sid)
|
||||
_logger.debug('deleting session with key %s', key)
|
||||
_logger.debug("deleting session with key %s", key)
|
||||
return self.redis.delete(key)
|
||||
|
||||
def get(self, sid):
|
||||
if not self.is_valid_key(sid):
|
||||
_logger.debug("session with invalid sid '%s' has been asked, "
|
||||
"returning a new one", sid)
|
||||
_logger.debug(
|
||||
"session with invalid sid '%s' has been asked, " "returning a new one",
|
||||
sid,
|
||||
)
|
||||
return self.new()
|
||||
|
||||
key = self.build_key(sid)
|
||||
saved = self.redis.get(key)
|
||||
if not saved:
|
||||
_logger.debug("session with non-existent key '%s' has been asked, "
|
||||
"returning a new one", key)
|
||||
_logger.debug(
|
||||
"session with non-existent key '%s' has been asked, "
|
||||
"returning a new one",
|
||||
key,
|
||||
)
|
||||
return self.new()
|
||||
try:
|
||||
data = json.loads(
|
||||
saved.decode('utf-8'), cls=json_encoding.SessionDecoder
|
||||
)
|
||||
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)
|
||||
_logger.debug(
|
||||
"session for key '%s' has been asked but its json "
|
||||
"content could not be read, it has been reset",
|
||||
key,
|
||||
)
|
||||
data = {}
|
||||
return self.session_class(data, sid, False)
|
||||
|
||||
def list(self):
|
||||
keys = self.redis.keys('%s*' % self.prefix)
|
||||
keys = self.redis.keys("%s*" % self.prefix)
|
||||
_logger.debug("a listing redis keys has been called")
|
||||
return [key[len(self.prefix):] for key in keys]
|
||||
return [key[len(self.prefix) :] for key in keys]
|
||||
|
||||
Reference in New Issue
Block a user