Commits

Anonymous committed 653fa62

add restrict to views

Comments (0)

Files changed (1)

newsmeme/models.py

 
     def restricted(self, user):
 
-        if user is None:
-                return self.filter(Post.access==Post.PUBLIC)
-
-        if user.is_moderator:
+        if user and user.is_moderator:
             return self
 
-        return self.filter(
-            db.or_(Post.access==Post.PUBLIC,
-                   Post.author_id==user.id,
-                   db.and_(Post.access==Post.FRIENDS, 
-                           Post.author_id.in_(user.friends)))).distinct()
+        posts = Post.__table__.alias()
+        
+        q = self.filter(posts.c.id==Comment.post_id)
+
+        if user is None:
+            return q.filter(posts.c.access==Post.PUBLIC)
+
+
+        return q.filter(
+            db.or_(posts.c.access==Post.PUBLIC,
+                   posts.c.author_id==user.id,
+                   db.and_(posts.c.access==Post.FRIENDS, 
+                           posts.c.author_id.in_(user.friends)))).distinct()
 
 
 class Comment(db.Model):