diff --git a/muk_session_store/README.rst b/muk_session_store/README.rst index 51c2b7a..3398289 100644 --- a/muk_session_store/README.rst +++ b/muk_session_store/README.rst @@ -110,6 +110,7 @@ The following fields can be modified in the config file: **Postgres:** * session_store_dbname +* session_store_dbtable **Redis:** diff --git a/muk_session_store/__manifest__.py b/muk_session_store/__manifest__.py index d6b0f1c..362153d 100644 --- a/muk_session_store/__manifest__.py +++ b/muk_session_store/__manifest__.py @@ -23,7 +23,7 @@ { "name": "MuK Session Store", "summary": """Session Store Options""", - "version": "12.0.2.0.1", + "version": "12.0.2.1.0", "category": "Extra Tools", "license": "LGPL-3", "website": "http://www.mukit.at", diff --git a/muk_session_store/doc/index.rst b/muk_session_store/doc/index.rst index 51c2b7a..3398289 100644 --- a/muk_session_store/doc/index.rst +++ b/muk_session_store/doc/index.rst @@ -110,6 +110,7 @@ The following fields can be modified in the config file: **Postgres:** * session_store_dbname +* session_store_dbtable **Redis:** diff --git a/muk_session_store/store/postgres.py b/muk_session_store/store/postgres.py index a37d3a5..94b174d 100644 --- a/muk_session_store/store/postgres.py +++ b/muk_session_store/store/postgres.py @@ -20,7 +20,7 @@ # ################################################################################### -import json +import pickle import logging import psycopg2 import functools @@ -53,6 +53,7 @@ class PostgresSessionStore(SessionStore): def __init__(self, *args, **kwargs): super(PostgresSessionStore, self).__init__(*args, **kwargs) self.dbname = config.get('session_store_dbname', 'session_store') + self.dbtable = config.get('session_store_dbtable', 'odoo_sessions') self._setup_database(raise_exception=False) def _setup_database(self, raise_exception=True): @@ -75,12 +76,12 @@ class PostgresSessionStore(SessionStore): def _create_table(self, cursor): cursor.execute(""" - CREATE TABLE IF NOT EXISTS sessions ( + CREATE TABLE IF NOT EXISTS {dbtable} ( sid varchar PRIMARY KEY, write_date timestamp without time zone NOT NULL, - payload text NOT NULL + payload bytea NOT NULL ); - """) + """.format(dbtable=self.dbtable)) @contextmanager def open_cursor(self): @@ -94,16 +95,21 @@ class PostgresSessionStore(SessionStore): def save(self, session): with self.open_cursor() as cursor: cursor.execute(""" - INSERT INTO sessions (sid, write_date, payload) + INSERT INTO {dbtable} (sid, write_date, payload) VALUES (%(sid)s, now() at time zone 'UTC', %(payload)s) ON CONFLICT (sid) DO UPDATE SET payload = %(payload)s, write_date = now() at time zone 'UTC'; - """, dict(sid=session.sid, payload=json.dumps(dict(session)))) + """.format(dbtable=self.dbtable), dict( + sid=session.sid, + payload=psycopg2.Binary( + pickle.dumps(dict(session), pickle.HIGHEST_PROTOCOL) + ) + )) @retry_database def delete(self, session): with self.open_cursor() as cursor: - cursor.execute("DELETE FROM sessions WHERE sid=%s;", [session.sid]) + cursor.execute("DELETE FROM {dbtable} WHERE sid=%s;".format(dbtable=self.dbtable), [session.sid]) @retry_database def get(self, sid): @@ -112,30 +118,30 @@ class PostgresSessionStore(SessionStore): with self.open_cursor() as cursor: cursor.execute(""" SELECT payload, write_date - FROM sessions WHERE sid=%s; - """, [sid]) + FROM {dbtable} WHERE sid=%s; + """.format(dbtable=self.dbtable), [sid]) try: payload, write_date = cursor.fetchone() if write_date.date() != datetime.today().date(): cursor.execute(""" - UPDATE sessions + UPDATE {dbtable} SET write_date = now() at time zone 'UTC' WHERE sid=%s; - """, [sid]) - return self.session_class(json.loads(payload), sid, False) + """.format(dbtable=self.dbtable), [sid]) + return self.session_class(pickle.loads(payload), sid, False) except Exception: return self.session_class({}, sid, False) @retry_database def list(self): with self.open_cursor() as cursor: - cursor.execute("SELECT sid FROM sessions;") + cursor.execute("SELECT sid FROM {dbtable};".format(dbtable=self.dbtable)) return [record[0] for record in cursor.fetchall()] @retry_database def clean(self): with self.open_cursor() as cursor: cursor.execute(""" - DELETE FROM sessions + DELETE FROM {dbtable} WHERE now() at time zone 'UTC' - write_date > '7 days'; - """) \ No newline at end of file + """.format(dbtable=self.dbtable)) \ No newline at end of file