Anonymous avatar Anonymous committed 43c4760

common permissions class

Comments (0)

Files changed (3)

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)
 

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)
 

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
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.