You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.0 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Session Store
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import logging
  23. import random
  24. from odoo import http, tools
  25. from odoo.addons.muk_session_store.store.postgres import PostgresSessionStore
  26. from odoo.addons.muk_session_store.store.redis import RedisSessionStore
  27. from odoo.addons.muk_utils.tools.patch import monkey_patch
  28. from odoo.http import request
  29. from odoo.tools.func import lazy_property
  30. _logger = logging.getLogger(__name__)
  31. try:
  32. import redis
  33. except ImportError:
  34. if tools.config.get("session_store_redis"):
  35. _logger.warning("The Python library redis is not installed.")
  36. redis = False
  37. def get_session_store_database():
  38. return tools.config.get("session_store_dbname", "session_store")
  39. @monkey_patch(http)
  40. def db_monodb(httprequest=None):
  41. if tools.config.get("session_store_database"):
  42. httprequest = httprequest or request.httprequest
  43. dbs = http.db_list(True, httprequest)
  44. store = get_session_store_database()
  45. db_session = httprequest.session.db
  46. if db_session in dbs:
  47. return db_session
  48. if store in dbs:
  49. dbs.remove(store)
  50. if len(dbs) == 1:
  51. return dbs[0]
  52. return None
  53. else:
  54. return db_monodb.super(httprequest)
  55. @monkey_patch(http)
  56. def db_filter(dbs, httprequest=None):
  57. dbs = db_filter.super(dbs, httprequest=httprequest)
  58. store = get_session_store_database()
  59. if store in dbs:
  60. dbs.remove(store)
  61. return dbs
  62. @monkey_patch(http)
  63. def session_gc(session_store):
  64. if tools.config.get("session_store_database"):
  65. if random.random() < 0.001:
  66. session_store.clean()
  67. elif tools.config.get("session_store_redis"):
  68. pass
  69. else:
  70. session_gc.super(session_store)
  71. class Root(http.Root):
  72. @lazy_property
  73. def session_store(self):
  74. if tools.config.get("session_store_database"):
  75. return PostgresSessionStore(session_class=http.OpenERPSession)
  76. elif tools.config.get("session_store_redis") and redis:
  77. return RedisSessionStore(session_class=http.OpenERPSession)
  78. return super(Root, self).session_store
  79. http.root = Root()