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.

87 lines
3.0 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.tools.func import lazy_property
  23. from odoo.addons.muk_utils.tools.patch import monkey_patch
  24. from odoo.addons.muk_session_store.store import postgres
  25. from odoo.addons.muk_session_store.store import redis
  26. _logger = logging.getLogger(__name__)
  27. try:
  28. import redis
  29. except ImportError:
  30. if tools.config.get('session_store_redis'):
  31. _logger.warning("The Python library redis is not installed.")
  32. redis = False
  33. def get_session_store_database():
  34. return tools.config.get('session_store_dbname', 'session_store')
  35. @monkey_patch(http)
  36. def db_monodb(httprequest=None):
  37. if tools.config.get('session_store_database'):
  38. httprequest = httprequest or request.httprequest
  39. dbs = http.db_list(True, httprequest)
  40. store = get_session_store_database()
  41. db_session = httprequest.session.db
  42. if db_session in dbs:
  43. return db_session
  44. if store in dbs:
  45. dbs.remove(store)
  46. if len(dbs) == 1:
  47. return dbs[0]
  48. return None
  49. else:
  50. return db_monodb.super(httprequest)
  51. @monkey_patch(http)
  52. def db_filter(dbs, httprequest=None):
  53. dbs = db_filter.super(dbs, httprequest=httprequest)
  54. store = get_session_store_database()
  55. if store in dbs:
  56. dbs.remove(store)
  57. return dbs
  58. @monkey_patch(http)
  59. def session_gc(session_store):
  60. if tools.config.get('session_store_database'):
  61. if random.random() < 0.001:
  62. session_store.clean()
  63. elif tools.config.get('session_store_redis'):
  64. pass
  65. else:
  66. session_gc.super(session_store)
  67. class Root(http.Root):
  68. @lazy_property
  69. def session_store(self):
  70. if tools.config.get('session_store_database'):
  71. return postgres.PostgresSessionStore(session_class=http.OpenERPSession)
  72. elif tools.config.get('session_store_redis') and redis:
  73. return redis.RedisSessionStore(session_class=http.OpenERPSession)
  74. return super(Root, self).session_store
  75. http.root = Root()