Skip to content

Commit

Permalink
bugfix: support application factory pattern (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwag956 committed May 6, 2019
1 parent c3ceca7 commit 113d16b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
59 changes: 36 additions & 23 deletions flask_security/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,30 +462,52 @@ class Security(object):
:param app: The application.
:param datastore: An instance of a user datastore.
:param register_blueprint: to register the Security blueprint or not.
:param login_form: set form for the login view
:param register_form: set form for the register view
:param confirm_register_form: set form for the confirm register view
:param forgot_password_form: set form for the forgot password view
:param reset_password_form: set form for the reset password view
:param change_password_form: set form for the change password view
:param send_confirmation_form: set form for the send confirmation view
:param passwordless_login_form: set form for the passwordless login view
:param anonymous_user: class to use for anonymous user
"""

def __init__(self, app=None, datastore=None, **kwargs):
def __init__(self, app=None, datastore=None, register_blueprint=True,
**kwargs):
self.app = app
self.datastore = datastore

self._datastore = datastore
self._register_blueprint = register_blueprint
self._kwargs = dict(login_form=None,
register_form=None,
confirm_register_form=None,
forgot_password_form=None,
reset_password_form=None,
change_password_form=None,
send_confirmation_form=None,
passwordless_login_form=None,
anonymous_user=None)
self._kwargs.update(kwargs)

self._state = None # set by init_app
if app is not None and datastore is not None:
self._state = self.init_app(app, datastore, **kwargs)

def init_app(self, app, datastore=None, register_blueprint=True,
login_form=None, confirm_register_form=None,
register_form=None, forgot_password_form=None,
reset_password_form=None, change_password_form=None,
send_confirmation_form=None, passwordless_login_form=None,
anonymous_user=None):
self._state = self.init_app(app, datastore, **self._kwargs)

def init_app(self, app, datastore=None, register_blueprint=None, **kwargs):
"""Initializes the Flask-Security extension for the specified
application and datastore implentation.
application and datastore implementation.
:param app: The application.
:param datastore: An instance of a user datastore.
:param register_blueprint: to register the Security blueprint or not.
"""
self.app = app
self.datastore = datastore
if datastore is None:
datastore = self._datastore
if register_blueprint is None:
register_blueprint = self._register_blueprint
self._kwargs.update(kwargs)

for key, value in _default_config.items():
app.config.setdefault('SECURITY_' + key, value)
Expand All @@ -495,16 +517,7 @@ def init_app(self, app, datastore=None, register_blueprint=True,

identity_loaded.connect_via(app)(_on_identity_loaded)

state = _get_state(app, self.datastore,
login_form=login_form,
confirm_register_form=confirm_register_form,
register_form=register_form,
forgot_password_form=forgot_password_form,
reset_password_form=reset_password_form,
change_password_form=change_password_form,
send_confirmation_form=send_confirmation_form,
passwordless_login_form=passwordless_login_form,
anonymous_user=anonymous_user)
self._state = state = _get_state(app, datastore, **self._kwargs)

if register_blueprint:
app.register_blueprint(create_blueprint(state, __name__))
Expand Down
17 changes: 17 additions & 0 deletions tests/test_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,20 @@ def test_access_datastore_from_factory(app, datastore):

assert security.datastore is not None
assert security.app is not None


def test_access_datastore_from_app_factory_pattern(app, datastore):
security = Security(datastore=datastore)
security.init_app(app)

assert security.datastore is not None
assert security.app is not None


def test_init_app_kwargs_override_constructor_kwargs(app, datastore):
security = Security(datastore=datastore, login_form='__init__login_form',
register_form='__init__register_form')
security.init_app(app, login_form='init_app_login_form')

assert security.login_form == 'init_app_login_form'
assert security.register_form == '__init__register_form'

0 comments on commit 113d16b

Please sign in to comment.