Commits

danjac  committed 43c4760

common permissions class

  • Participants
  • Parent commits a47decd

Comments (0)

Files changed (3)

File newsmeme/models/comments.py

 from newsmeme.helpers import markdown
 from newsmeme.models.posts import Post
 from newsmeme.models.users import User
+from newsmeme.models.permissions import Permissions
 from newsmeme.models.types import DenormalizedText
 
 class CommentQuery(BaseQuery):
 
     __mapper_args__ = {'order_by' : id.asc()}
     
-    class Permissions(object):
+    class Permissions(Permissions):
 
-        def __init__(self, obj):
-            self.obj = obj
 
         @cached_property
         def default(self):
-            return Permission(UserNeed(self.obj.author_id)) & moderator
+            return Permission(UserNeed(self.author_id)) & moderator
 
         @cached_property
         def edit(self):
         @cached_property
         def vote(self):
 
-            needs = [UserNeed(user_id) for user_id in self.obj.votes]
-            needs.append(UserNeed(self.obj.author_id))
+            needs = [UserNeed(user_id) for user_id in self.votes]
+            needs.append(UserNeed(self.author_id))
 
             return auth & Denial(*needs)
 

File newsmeme/models/posts.py

     
     __mapper_args__ = {'order_by' : id.desc()}
 
-    class Permissions(object):
-
-        def __init__(self, obj):
-            self.obj = obj
+    class Permissions(Permissions):
 
         @cached_property
         def default(self):
-            return Permission(UserNeed(self.obj.author_id)) & moderator
+            return Permission(UserNeed(self.author_id)) & moderator
 
         @cached_property
         def view(self):
-            if self.obj.access == Post.PUBLIC:
+            if self.access == Post.PUBLIC:
                 return Permission()
 
-            if self.obj.access == Post.FRIENDS:
+            if self.access == Post.FRIENDS:
                 needs = [UserNeed(user_id) for user_id in \
-                            self.obj.author.friends]
+                            self.author.friends]
 
                 return self.default & Permission(*needs)
 
         @cached_property
         def vote(self):
 
-            needs = [UserNeed(user_id) for user_id in self.obj.votes]
-            needs.append(UserNeed(self.obj.author_id))
+            needs = [UserNeed(user_id) for user_id in self.votes]
+            needs.append(UserNeed(self.author_id))
 
             return auth & Denial(*needs)
 

File newsmeme/models/users.py

 
 from newsmeme.extensions import db
 from newsmeme.permissions import null
+from newsmeme.models.permissions import Permissions
 from newsmeme.models.types import DenormalizedText
 
 class UserQuery(BaseQuery):
     _password = db.Column("password", db.String(80))
     _openid = db.Column("openid", db.String(80), unique=True)
 
-    class Permissions(object):
-
-        def __init__(self, obj):
-            self.obj = obj
+    class Permissions(Permissions):
 
         @cached_property
         def send_message(self):
-            if not self.obj.receive_email:
+            if not self.receive_email:
                 return null
 
-            needs = [UserNeed(user_id) for user_id in self.obj.friends]
+            needs = [UserNeed(user_id) for user_id in self.friends]
             if not needs:
                 return null