__init__.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. """
  3. flask_session
  4. ~~~~~~~~~~~~~
  5. Adds server session support to your application.
  6. :copyright: (c) 2014 by Shipeng Feng.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. __version__ = '0.4.0'
  10. import os
  11. from .sessions import NullSessionInterface, RedisSessionInterface, \
  12. MemcachedSessionInterface, FileSystemSessionInterface, \
  13. MongoDBSessionInterface, SqlAlchemySessionInterface
  14. class Session(object):
  15. """This class is used to add Server-side Session to one or more Flask
  16. applications.
  17. There are two usage modes. One is initialize the instance with a very
  18. specific Flask application::
  19. app = Flask(__name__)
  20. Session(app)
  21. The second possibility is to create the object once and configure the
  22. application later::
  23. sess = Session()
  24. def create_app():
  25. app = Flask(__name__)
  26. sess.init_app(app)
  27. return app
  28. By default Flask-Session will use :class:`NullSessionInterface`, you
  29. really should configurate your app to use a different SessionInterface.
  30. .. note::
  31. You can not use ``Session`` instance directly, what ``Session`` does
  32. is just change the :attr:`~flask.Flask.session_interface` attribute on
  33. your Flask applications.
  34. """
  35. def __init__(self, app=None):
  36. self.app = app
  37. if app is not None:
  38. self.init_app(app)
  39. def init_app(self, app):
  40. """This is used to set up session for your app object.
  41. :param app: the Flask app object with proper configuration.
  42. """
  43. app.session_interface = self._get_interface(app)
  44. def _get_interface(self, app):
  45. config = app.config.copy()
  46. config.setdefault('SESSION_TYPE', 'null')
  47. config.setdefault('SESSION_PERMANENT', True)
  48. config.setdefault('SESSION_USE_SIGNER', False)
  49. config.setdefault('SESSION_KEY_PREFIX', 'session:')
  50. config.setdefault('SESSION_REDIS', None)
  51. config.setdefault('SESSION_MEMCACHED', None)
  52. config.setdefault('SESSION_FILE_DIR',
  53. os.path.join(os.getcwd(), 'flask_session'))
  54. config.setdefault('SESSION_FILE_THRESHOLD', 500)
  55. config.setdefault('SESSION_FILE_MODE', 384)
  56. config.setdefault('SESSION_MONGODB', None)
  57. config.setdefault('SESSION_MONGODB_DB', 'flask_session')
  58. config.setdefault('SESSION_MONGODB_COLLECT', 'sessions')
  59. config.setdefault('SESSION_SQLALCHEMY', None)
  60. config.setdefault('SESSION_SQLALCHEMY_TABLE', 'sessions')
  61. if config['SESSION_TYPE'] == 'redis':
  62. session_interface = RedisSessionInterface(
  63. config['SESSION_REDIS'], config['SESSION_KEY_PREFIX'],
  64. config['SESSION_USE_SIGNER'], config['SESSION_PERMANENT'])
  65. elif config['SESSION_TYPE'] == 'memcached':
  66. session_interface = MemcachedSessionInterface(
  67. config['SESSION_MEMCACHED'], config['SESSION_KEY_PREFIX'],
  68. config['SESSION_USE_SIGNER'], config['SESSION_PERMANENT'])
  69. elif config['SESSION_TYPE'] == 'filesystem':
  70. session_interface = FileSystemSessionInterface(
  71. config['SESSION_FILE_DIR'], config['SESSION_FILE_THRESHOLD'],
  72. config['SESSION_FILE_MODE'], config['SESSION_KEY_PREFIX'],
  73. config['SESSION_USE_SIGNER'], config['SESSION_PERMANENT'])
  74. elif config['SESSION_TYPE'] == 'mongodb':
  75. session_interface = MongoDBSessionInterface(
  76. config['SESSION_MONGODB'], config['SESSION_MONGODB_DB'],
  77. config['SESSION_MONGODB_COLLECT'],
  78. config['SESSION_KEY_PREFIX'], config['SESSION_USE_SIGNER'],
  79. config['SESSION_PERMANENT'])
  80. elif config['SESSION_TYPE'] == 'sqlalchemy':
  81. session_interface = SqlAlchemySessionInterface(
  82. app, config['SESSION_SQLALCHEMY'],
  83. config['SESSION_SQLALCHEMY_TABLE'],
  84. config['SESSION_KEY_PREFIX'], config['SESSION_USE_SIGNER'],
  85. config['SESSION_PERMANENT'])
  86. else:
  87. session_interface = NullSessionInterface()
  88. return session_interface