Skip to content

Commit

Permalink
Configurable extension options
Browse files Browse the repository at this point in the history
Makes extension options configurable via application config.
(closes #48)
  • Loading branch information
jirikuncar committed May 7, 2017
1 parent 0b5cd06 commit 8cd5df9
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions flask_principal.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,12 @@ class Principal(object):
:param app: The flask application to extend
:param use_sessions: Whether to use sessions to extract and store
identification.
:param skip_static: Whether to ignore static endpoints.
identification. If it is not provided it will check
``PRINCIPAL_USE_SESSIONS`` configuration.
:param skip_static: Whether to ignore static endpoints. If it is not
provided it will check ``PRINCIPAL_SKIP_STATIC``.
"""
def __init__(self, app=None, use_sessions=True, skip_static=False):
def __init__(self, app=None, use_sessions=None, skip_static=None):
self.identity_loaders = deque()
self.identity_savers = deque()
# XXX This will probably vanish for a better API
Expand All @@ -405,7 +407,7 @@ def init_app(self, app):
app.before_request(self._on_before_request)
identity_changed.connect(self._on_identity_changed, app)

if self.use_sessions:
if _resolve_configuration('use_sessions', self, app, default=True):
self.identity_loader(session_identity_loader)
self.identity_saver(session_identity_saver)

Expand Down Expand Up @@ -480,7 +482,16 @@ def _on_before_request(self):
return

def _is_static_route(self):
return (
self.skip_static and
(self._static_path and request.path.startswith(self._static_path))
return _resolve_configuration(
'skip_static', self, current_app, default=False
) and (
self._static_path and request.path.startswith(self._static_path)
)


def _resolve_configuration(key, ext, app, default=None):
"""Resolve configuration value."""
value = getattr(ext, key.lower(), None)
if value is not None:
return value
return app.config.get('PRINCIPAL_{0}'.format(key.upper()), default)

0 comments on commit 8cd5df9

Please sign in to comment.