【问题标题】:How do I logout inactive users session in odoo14?如何注销 odoo14 中的非活动用户会话?
【发布时间】:2022-06-10 19:03:34
【问题描述】:

'''嘟嘟''' 这是我尝试修改会话注销代码的 odoo http.py 文件 def session_gc(session_store): 如果 random.random() 1 ###!!! 6060247 对于 os.listdir(session_store.path) 中的 fname: 路径 = os.path.join(session_store.path, fname) 尝试: 如果 os.path.getmtime(path)

【问题讨论】:

    标签: session


    【解决方案1】:

    创建一个py文件例如session.py并添加以下代码:

    import psycopg2
    import os
    import json
    import random
    import werkzeug.contrib.sessions
    import time
    
    import odoo
    from odoo import http
    from odoo.tools.func import lazy_property
    
    
    def with_cursor(func):
        def wrapper(self, *args, **kwargs):
            tries = 0
            while True:
                tries += 1
                try:
                    return func(self, *args, **kwargs)
                except psycopg2.InterfaceError as e:
                    if tries>4:
                        raise e
                    self._open_connection()
        return wrapper
    
    class PGSessionStore(werkzeug.contrib.sessions.SessionStore):
        # FIXME This class is NOT thread-safe. Only use in worker mode
        def __init__(self, uri, session_class=None):
            super(PGSessionStore, self).__init__(session_class)
            self._uri = uri
            self._open_connection()
            self._setup_db()
    
        def __del__(self):
            self._cr.close()
    
        def _open_connection(self):
            cnx = odoo.sql_db.db_connect(self._uri, allow_uri=True)
            self._cr = cnx.cursor()
            self._cr.autocommit(True)
    
        @with_cursor
        def _setup_db(self):
            self._cr.execute("""
                CREATE TABLE IF NOT EXISTS http_sessions (
                    sid varchar PRIMARY KEY,
                    write_date timestamp without time zone NOT NULL,
                    payload text NOT NULL
                )
            """)
    
        @with_cursor
        def save(self, session):
            payload = json.dumps(dict(session))
            self._cr.execute("""
                INSERT INTO http_sessions(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=payload))
    
        @with_cursor
        def delete(self, session):
            self._cr.execute("DELETE FROM http_sessions WHERE sid=%s", [session.sid])
    
        @with_cursor
        def get(self, sid):
            self._cr.execute("UPDATE http_sessions SET write_date = now() at time zone 'UTC' WHERE sid=%s", [sid])
            self._cr.execute("SELECT payload FROM http_sessions WHERE sid=%s", [sid])
            try:
                data = json.loads(self._cr.fetchone()[0])
            except Exception:
                return self.new()
    
            return self.session_class(data, sid, False)
    
        @with_cursor
        def gc(self):
            self._cr.execute(
                "DELETE FROM http_sessions WHERE now() at time zone 'UTC' - write_date > '2 hours'"
            )
    
    
    def session_gc(session_store):
        """
        Global cleaning of sessions using either the standard way (delete session files),
        Or the DB way.
        """
        if random.random() < 0.001:
            # we keep session two hours
            if hasattr(session_store, 'gc'):
                session_store.gc()
                return
            two_hours = time.time() - 60*60*2
            for fname in os.listdir(session_store.path):
                path = os.path.join(session_store.path, fname)
                try:
                    if os.path.getmtime(path) < two_hours:
                        os.unlink(path)
                except OSError:
                    pass
    
    class Root(http.Root):
        @lazy_property
        def session_store(self):
            """
            Store sessions in DB rather than on FS if parameter permit so
            """
            # Setup http sessions
            session_db = odoo.tools.config.get('session_db')
            if session_db:
                return PGSessionStore(session_db, session_class=http.OpenERPSession)
            path = odoo.tools.config.session_dir
            return werkzeug.contrib.sessions.FilesystemSessionStore(path, session_class=http.OpenERPSession)
                            
    http.session_gc = session_gc
    http.root = Root()
    

    以下代码将继承自 odoo 模块中的 http 文件。配置的超时时间为 2 小时,可以在 session_gc 函数中更改。

    【讨论】:

      猜你喜欢
      • 2014-10-21
      • 2014-08-04
      • 2017-12-19
      • 1970-01-01
      • 2019-12-17
      • 2020-05-19
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      相关资源
      最近更新 更多