Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues/232: Allow custom session instantiation #233

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ 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

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
Expand Down Expand Up @@ -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

Expand All @@ -62,13 +62,13 @@ Using rye
~~~~~~~~~~~

.. code-block:: bash

$ rye pin 3.11
$ rye sync


.. code-block:: bash

$ rye run python examples/hello.py


Expand All @@ -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.
Please ensure your pull requests are to the `development` branch.
11 changes: 6 additions & 5 deletions src/flask_session/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@
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"""
with pytest.raises(ValueError):
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"
Loading