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.

88 lines
2.9 KiB

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