Commits

Anonymous committed 5986a12

eliminate non-pub posts from tags

Comments (0)

Files changed (1)

newsmeme/models.py

         return url_for("frontend.tag", slug=self.slug)
 
     num_posts = db.column_property(
-        db.select([db.func.count(post_tags.c.post_id)]).where(
-            post_tags.c.tag_id==id).as_scalar())
+        db.select([db.func.count(post_tags.c.post_id)]).\
+            where(db.and_(post_tags.c.tag_id==id,
+                          Post.id==post_tags.c.post_id,
+                          Post.access==Post.PUBLIC)).as_scalar())
 
 class CommentQuery(BaseQuery):
 
 
         if user and user.is_moderator:
             return self
+       
+        q = self.join(Post)
+        criteria = [Post.access==Post.PUBLIC]
 
-        posts = Post.__table__.alias()
+        if user:
+            criteria.append(Post.author_id==user.id)
+            if user.friends:
+                criteria.append(db.and_(Post.access==Post.FRIENDS,
+                                        Post.author_id.in_(user.friends)))
         
-        q = self.filter(posts.c.id==Comment.post_id)
+        return self.filter(reduce(db.or_, criteria))
 
-        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):
 
     __tablename__ = "comments"