Commits

Anonymous committed 8e3845d

polymorphic relations

Comments (0)

Files changed (2)

newsmeme/models/comments.py

 from newsmeme.extensions import db
 from newsmeme.permissions import auth, moderator
 from newsmeme.helpers import markdown
-from newsmeme.models.posts import Post
+from newsmeme.models.posts import BasePost
 from newsmeme.models.users import User
 from newsmeme.models.types import DenormalizedText
 
         if user and user.is_moderator:
             return self
        
-        q = self.join(Post)
-        criteria = [Post.access==Post.PUBLIC]
+        q = self.join(BasePost)
+        criteria = [BasePost.access==BasePost.PUBLIC]
 
         if user:
-            criteria.append(Post.author_id==user.id)
+            criteria.append(BasePost.author_id==user.id)
             if user.friends:
-                criteria.append(db.and_(Post.access==Post.FRIENDS,
-                                        Post.author_id.in_(user.friends)))
+                criteria.append(db.and_(BasePost.access==BasePost.FRIENDS,
+                                        BasePost.author_id.in_(user.friends)))
         
         return q.filter(reduce(db.or_, criteria))
 
                           nullable=False)
 
     post_id = db.Column(db.Integer, 
-                        db.ForeignKey(Post.id, ondelete='CASCADE'), 
+                        db.ForeignKey(BasePost.id, ondelete='CASCADE'), 
                         nullable=False)
 
     parent_id = db.Column(db.Integer, 
 
     author = db.relation(User, innerjoin=True, lazy="joined")
 
-    post = db.relation(Post, innerjoin=True, lazy="joined")
+    post = db.relation(BasePost, innerjoin=True, lazy="joined")
 
     parent = db.relation('Comment', remote_side=[id])
 

newsmeme/views/post.py

 from flaskext.babel import gettext as _
 
 from newsmeme import signals
-from newsmeme.models import Post, Comment
+from newsmeme.models import Post, BasePost, Comment
 from newsmeme.forms import CommentForm, PostForm
 from newsmeme.helpers import render_template
 from newsmeme.decorators import keep_login_url
 @post.route("/<int:post_id>/<int:parent_id>/reply/", methods=("GET", "POST"))
 @auth.require(401)
 def add_comment(post_id, parent_id=None):
-    post = Post.query.get_or_404(post_id)
+    post = BasePost.query.get_or_404(post_id)
     post.permissions.view.test(403)
 
     parent = Comment.query.get_or_404(parent_id) if parent_id else None
 
 def _vote(post_id, score):
 
-    post = Post.query.get_or_404(post_id)
+    post = BasePost.query.get_or_404(post_id)
     post.permissions.vote.test(403)
     
     post.score += score