diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1d48ab6f..6af04403 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,7 +7,6 @@ exclude: | ^monitoring_prometheus/| ^monitoring_statsd/| ^monitoring_status/| - ^session_redis/| ^test_base_fileurl_field/| # END NOT INSTALLABLE ADDONS # Files and folders generated by bots, to avoid loops diff --git a/requirements.txt b/requirements.txt index e4686695..1997842f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ # generated from manifests external_dependencies python-json-logger +redis diff --git a/session_redis/__manifest__.py b/session_redis/__manifest__.py index de134274..d7207578 100644 --- a/session_redis/__manifest__.py +++ b/session_redis/__manifest__.py @@ -18,5 +18,5 @@ "python": ["redis"], }, "website": "https://github.com/camptocamp/odoo-cloud-platform", - "installable": False, + "installable": True, } diff --git a/session_redis/http.py b/session_redis/http.py index efd6c827..becfbd58 100644 --- a/session_redis/http.py +++ b/session_redis/http.py @@ -93,15 +93,14 @@ def purge_fs_sessions(path): if is_true(os.getenv("ODOO_SESSION_REDIS")): if sentinel_host: _logger.debug( - "HTTP sessions stored in Redis with prefix '%s'. " - "Using Sentinel on %s:%s", + "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", + "HTTP sessions stored in Redis with prefix '%s' on %s:%s", prefix or "", host, port, diff --git a/session_redis/session.py b/session_redis/session.py index e995ce69..da356479 100644 --- a/session_redis/session.py +++ b/session_redis/session.py @@ -1,9 +1,10 @@ # Copyright 2016-2024 Camptocamp SA # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) +import builtins import json import logging -from typing import TypeAlias, List +from typing import TypeAlias import odoo.http from odoo.http import SESSION_LIFETIME @@ -96,9 +97,8 @@ def save(self, session): "utf-8" ) if self.redis.set(key, data): - if type(expiration) != int: + if not (expiration and isinstance(expiration, int)): expiration = DEFAULT_SESSION_TIMEOUT_ANONYMOUS - if expiration == 0: expiration = DEFAULT_SESSION_TIMEOUT_ANONYMOUS return self.redis.expire(key, expiration) @@ -110,8 +110,7 @@ def delete(self, session): def get(self, sid): if not self.is_valid_key(sid): _logger.debug( - f"session with invalid sid '{sid}' has been asked, " - "returning a new one" + f"session with invalid sid '{sid}' has been asked, returning a new one" ) return self.new() @@ -134,7 +133,7 @@ def get(self, sid): return self.session_class(data, sid, False) def list(self): - keys = self.redis.keys("%s*" % self.prefix) + keys = self.redis.keys(f"{self.prefix}*") _logger.debug("a listing redis keys has been called") return [key[len(self.prefix) :] for key in keys] @@ -162,7 +161,9 @@ def delete_old_sessions(self, session): """ return - def get_missing_session_identifiers(self, identifiers: List[PartialSid]) -> set[PartialSid]: + def get_missing_session_identifiers( + self, identifiers: builtins.list[PartialSid] + ) -> set[PartialSid]: """ Given a list of partial session ids, return a set of those session ids which no longer exist in the keystore. @@ -188,7 +189,7 @@ def get_missing_session_identifiers(self, identifiers: List[PartialSid]) -> set[ return not_found - def delete_from_identifiers(self, identifiers: List[PartialSid]): + def delete_from_identifiers(self, identifiers: builtins.list[PartialSid]): """ Given a list of partial session ids, remove any that are in the session store. @@ -198,10 +199,13 @@ def delete_from_identifiers(self, identifiers: List[PartialSid]): """ patterns_to_unlink = [] for identifier in identifiers: - # Avoid removing a session if it does not match an identifier. - # See this same comment in odoo.http.FileSessionStore.delete_from_identifiers. + # Avoid removing a session if it does not match an identifier. See this same + # comment in odoo.http.FileSessionStore.delete_from_identifiers. if not odoo.http._session_identifier_re.match(identifier): - raise ValueError("Identifier format incorrect, did you pass in a string instead of a list?") + raise ValueError( + "Identifier format incorrect, did you pass in a string instead ", + "of a list?", + ) patterns_to_unlink.append(f"{self.prefix}{identifier}*") keys_to_unlink = [] for pattern in patterns_to_unlink: