From 27879b5f7967c9a8ad3360ab95f664b0613ea83d Mon Sep 17 00:00:00 2001 From: Patrick Tombez Date: Tue, 4 Feb 2025 14:24:51 +0100 Subject: [PATCH] [MIG] session_redis: Migration to 18.0 --- session_redis/__manifest__.py | 3 +-- session_redis/http.py | 26 +++++++++++++++----------- session_redis/session.py | 22 +++++++++------------- session_redis/strtobool.py | 10 ++++++---- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/session_redis/__manifest__.py b/session_redis/__manifest__.py index a0deae69..45d32e26 100644 --- a/session_redis/__manifest__.py +++ b/session_redis/__manifest__.py @@ -14,6 +14,5 @@ "python": ["redis"], }, "website": "https://github.com/camptocamp/odoo-cloud-platform", - "data": [], - "installable": False, + "installable": True, } diff --git a/session_redis/http.py b/session_redis/http.py index f4e86d06..e2e10698 100644 --- a/session_redis/http.py +++ b/session_redis/http.py @@ -25,21 +25,21 @@ def is_true(strval): 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.getenv("ODOO_SESSION_REDIS_SENTINEL_HOST") +sentinel_master_name = os.getenv("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.getenv("ODOO_SESSION_REDIS_SENTINEL_PORT", 26379)) +host = os.getenv("ODOO_SESSION_REDIS_HOST", "localhost") +port = int(os.getenv("ODOO_SESSION_REDIS_PORT", 6379)) +prefix = os.getenv("ODOO_SESSION_REDIS_PREFIX") +url = os.getenv("ODOO_SESSION_REDIS_URL") +password = os.getenv("ODOO_SESSION_REDIS_PASSWORD") +expiration = os.getenv("ODOO_SESSION_REDIS_EXPIRATION") +anon_expiration = os.getenv("ODOO_SESSION_REDIS_EXPIRATION_ANONYMOUS") @lazy_property @@ -61,6 +61,10 @@ def session_store(self): def purge_fs_sessions(path): + if not os.path.isdir(path): + _logger.warning(f"Session directory '{path}' does not exist.") + return + for fname in os.listdir(path): path = os.path.join(path, fname) try: @@ -69,7 +73,7 @@ def purge_fs_sessions(path): _logger.warning("OS Error during purge of redis sessions.") -if is_true(os.environ.get("ODOO_SESSION_REDIS")): +if is_true(os.getenv("ODOO_SESSION_REDIS")): if sentinel_host: _logger.debug( "HTTP sessions stored in Redis with prefix '%s'. " diff --git a/session_redis/session.py b/session_redis/session.py index ec19b7cc..fbc0c34a 100644 --- a/session_redis/session.py +++ b/session_redis/session.py @@ -60,10 +60,8 @@ def save(self, session): else: user_msg = "anonymous user" _logger.debug( - "saving session with key '%s' and " "expiration of %s seconds for %s", - key, - expiration, - user_msg, + f"saving session with key '{key}' and " + f"expiration of {expiration} seconds for {user_msg}" ) data = json.dumps(dict(session), cls=json_encoding.SessionEncoder).encode( @@ -74,14 +72,14 @@ def save(self, session): def delete(self, session): key = self.build_key(session.sid) - _logger.debug("deleting session with key %s", key) + _logger.debug(f"deleting session with key {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, + f"session with invalid sid '{sid}' has been asked, " + "returning a new one" ) return self.new() @@ -89,18 +87,16 @@ def get(self, 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, + f"session with non-existent key '{key}' has been asked, " + "returning a new one" ) return self.new() try: 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, + f"session for key '{key}' has been asked but its json " + "content could not be read, it has been reset" ) data = {} return self.session_class(data, sid, False) diff --git a/session_redis/strtobool.py b/session_redis/strtobool.py index 1a7ad558..10ae6239 100644 --- a/session_redis/strtobool.py +++ b/session_redis/strtobool.py @@ -15,7 +15,9 @@ def strtobool(value): - try: - return _MAP[str(value).lower()] - except KeyError as error: - raise ValueError(f'"{value}" is not a valid bool value') from error + result = _MAP.get(str(value).strip().lower()) + + if result is None: + raise ValueError(f"Invalid boolean value: {repr(value)}") + + return result