Commits

Anonymous committed 9992869

from_identity method added

Comments (0)

Files changed (2)

newsmeme/application.py

         self.configure_logging()
         self.configure_errorhandlers()
         self.configure_extensions()
+        self.configure_before_handlers()
         self.configure_filters()
         self.configure_context_processors()
+        # self.configure_after_handlers()
         self.configure_modules()
 
         return self.app
         def timesince(value):
             return helpers.timesince(value)
 
+    def configure_before_handlers(self):
+
         @self.app.before_request
         def authenticate():
             g.user = getattr(g.identity, 'user', None)
 
         @identity_loaded.connect_via(self.app)
         def on_identity_loaded(sender, identity):
-            if identity.name and identity.name != 'anon':
-                user = User.query.get(identity.name)
-            else:
-                user = None
-            if user:
-                identity.user = user
-                identity.provides.update(user.provides)
+            g.user = User.query.from_identity(identity)
 
     def configure_i18n(self):
 

newsmeme/models/users.py

 
 class UserQuery(BaseQuery):
 
+    def from_identity(self, identity):
+        """
+        Loads user from flaskext.principal.Identity instance and
+        assigns permissions from user.
+
+        A "user" instance is monkeypatched to the identity instance.
+
+        If no user found then None is returned.
+        """
+
+        try:
+            user = self.get(int(identity.name))
+        except ValueError:
+            user = None
+
+        if user:
+            identity.provides.update(user.provides)
+
+        identity.user = user
+
+        return user
+ 
     def authenticate(self, login, password):
         
         user = self.filter(db.or_(User.username==login,