Skip to content

Commit

Permalink
Add ignore static feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Wright committed Jun 8, 2012
1 parent de7fc57 commit 8d7f5b0
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions flask_principal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
except ImportError:
from namedtuple import namedtuple

from flask import g, session, current_app, abort
from flask import g, session, current_app, abort, request
from flask.signals import Namespace


Expand Down Expand Up @@ -377,11 +377,13 @@ class Principal(object):
:param use_sessions: Whether to use sessions to extract and store
identification.
"""
def __init__(self, app=None, use_sessions=True):
def __init__(self, app=None, use_sessions=True, skip_static=False):
self.identity_loaders = deque()
self.identity_savers = deque()
# XXX This will probably vanish for a better API
self.use_sessions = use_sessions
self.skip_static = skip_static

if app is not None:
self._init_app(app)

Expand All @@ -398,6 +400,9 @@ def set_identity(self, identity):
:param identity: The identity to set
"""
if self._is_static_route():
return

self._set_thread_identity(identity)
for saver in self.identity_savers:
saver(identity)
Expand Down Expand Up @@ -449,9 +454,16 @@ def _on_identity_changed(self, app, identity):
self.set_identity(identity)

def _on_before_request(self):
if self._is_static_route():
return

g.identity = AnonymousIdentity()
for loader in self.identity_loaders:
identity = loader()
if identity is not None:
self.set_identity(identity)
return

def _is_static_route(self):
return (self.skip_static and \
request.path.startswith(current_app.static_path))

0 comments on commit 8d7f5b0

Please sign in to comment.