diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 8793b138..1e670329 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -8,7 +8,7 @@ Navigate to the project directory and run the following commands: Create and activate a virtual environment .. code-block:: bash - + $ python -m venv .venv $ source .venv/bin/activate @@ -16,7 +16,7 @@ Install dependencies .. code-block:: bash - $ pip install -r requirements/dev.in + $ pip install -r requirements/dev.txt $ pip install -r requirements/docs.in Install the package in editable mode @@ -47,11 +47,11 @@ or Run the tests together or individually .. code-block:: bash - + $ pytest tests $ pytest tests/test_basic.py -For easier startup and teardown of storage for testing you may use +For easier startup and teardown of storage for testing you may use .. code-block:: bash @@ -62,13 +62,13 @@ Using rye ~~~~~~~~~~~ .. code-block:: bash - + $ rye pin 3.11 $ rye sync .. code-block:: bash - + $ rye run python examples/hello.py @@ -78,4 +78,4 @@ Pull requests -------------- Please check previous pull requests before submitting a new one. -Please ensure your pull requests are to the `development` branch. \ No newline at end of file +Please ensure your pull requests are to the `development` branch. diff --git a/src/flask_session/__init__.py b/src/flask_session/__init__.py index b95a0b05..5dae02aa 100644 --- a/src/flask_session/__init__.py +++ b/src/flask_session/__init__.py @@ -21,13 +21,13 @@ class Session: your Flask applications. You should always use :class:`flask.session`. """ - def __init__(self, app=None): + def __init__(self, app=None, custom_interface=None): self.app = app if app is not None: - self.init_app(app) + self.init_app(app, custom_interface) - def init_app(self, app): - """This the the alternate setup method, typically used in an application factory pattern:: + def init_app(self, app, custom_interface=None): + """This the alternate setup method, typically used in an application factory pattern:: sess = Session() @@ -37,8 +37,9 @@ def create_app(): return app :param app: the Flask app object with proper configuration. + :param custom_interface: a custom session interface to use. """ - app.session_interface = self._get_interface(app) + app.session_interface = custom_interface or self._get_interface(app) def _get_interface(self, app): config = app.config diff --git a/tests/test_basic.py b/tests/test_basic.py index 1a904266..43b15bb8 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -2,6 +2,8 @@ import flask_session import pytest +from flask_session.mongodb import MongoDBSessionInterface + def test_null_session(): """Invalid session should fail to get/set the flask session""" @@ -9,3 +11,16 @@ def test_null_session(): app = flask.Flask(__name__) app.secret_key = "alsdkfjaldkjsf" flask_session.Session(app) + + +class CustomInterface(MongoDBSessionInterface): + def hello(self): + return "world" + + +def test_init_app_with_custom_interface(): + """Test init_app with a custom session interface""" + app = flask.Flask(__name__) + flask_session.Session(app, custom_interface=CustomInterface(app=app)) + assert isinstance(app.session_interface, CustomInterface) + assert app.session_interface.hello() == "world"