Exemple #1
0
    def post(self, post_id):
        if not self.user:
            msg = 'Login to Like Post'
            self.render('login-form.html', error=msg)
            return

        post = self.get_post_by_id(post_id)
        if not post:
            self.error(404)
            return

        logged_name = self.get_user_name()

        numoflikes = Like.gql("WHERE post_id = '%s' " % (str(post_id))).count()
        if logged_name == post.author:
            comments = Comment.gql("WHERE postid = '%s' " % (str(post_id)))
            msg = "Oops, don't like your own post"

            self.render("permalink.html",
                        post=post,
                        comments=comments,
                        error=msg,
                        numoflikes=numoflikes)
        else:
            like = Like.gql("WHERE post_id = '%s' AND author = '%s'" %
                            (str(post_id), logged_name)).count()
            if like == 0:
                new_like = Like(post_id=str(post_id), author=str(logged_name))
                new_like.put()
                numoflikes = Like.gql("WHERE post_id = '%s' " %
                                      (str(post_id))).count()
                msg = "You liked the post"

            else:
                msg = "You couldn't like a post twice"
            comments = Comment.gql("WHERE postid = '%s' " % (str(post_id)))

            self.render("permalink.html",
                        post=post,
                        comments=comments,
                        error=msg,
                        numoflikes=numoflikes)
            return
Exemple #2
0
    def get(self, post_id):
        post = self.get_post_by_id(post_id)
        if not post:
            self.error(404)
            return

        comments = Comment.gql("WHERE postid = '%s' " % (str(post_id)))
        numoflikes = Like.gql("WHERE post_id = '%s' " % (str(post_id))).count()
        self.render("permalink.html",
                    post=post,
                    comments=comments,
                    numoflikes=numoflikes)
        return
Exemple #3
0
 def post(self, post_id):
     # retrive post from key
     key = ndb.Key('BlogPost', int(post_id), parent=blog_key())
     post = key.get()
     # case: user liking the post
     if self.request.get('like'):
         if post and self.user:
             post.likes += 1
             like = Like(post_id=int(post_id), author=self.user)
             # save like and post
             like.put()
             post.put()
             time.sleep(0.1)
         self.redirect("/blog/%s" % post_id)
     # case: user unliking the post
     elif self.request.get('unlike'):
         if post and self.user:
             post.likes -= 1
             # delete like from db
             like = Like.gql(
                 "WHERE post_id = :1 AND author.username = :2",
                 int(post_id),
                 self.user.username).get()
             key = like.key
             key.delete()
             post.put()
             time.sleep(0.1)
         self.redirect("/blog/%s" % post_id)
     # user posted comment on the post
     else:
         content = self.request.get('content')
         if content and self.user:
             content = content.encode('ascii', 'ignore')
             comment = Comment(
                 content=str(content),
                 author=self.user,
                 post_id=int(post_id))
             comment.put()
             time.sleep(0.1)
             self.redirect("/blog/%s" % post_id)
         else:
             comments = Comment.gql(
                 "WHERE post_id = %s ORDER BY created DESC" %
                 int(post_id))
             self.render("blogpost.html", post=post, comments=comments)
Exemple #4
0
 def get(self, post_id):
     post_key = ndb.Key('BlogPost', int(post_id), parent=blog_key())
     post = post_key.get()
     # retrieve comments
     comments = Comment.gql(
         "WHERE post_id = %s ORDER BY created DESC" %
         int(post_id))
     liked_by_user = None
     if self.user:
         liked_by_user = Like.gql(
             "WHERE post_id = :1 AND author.username = :2",
             int(post_id),
             self.user.username).get()
     if not post:
         self.error(404)
         return
     self.render(
         "blogpost.html",
         post=post,
         comments=comments,
         liked_by_user=liked_by_user)
Exemple #5
0
    def post(self, post_id):
        if not self.user:
            msg = 'Login to Make Comment to Post'
            self.render('login-form.html', error=msg)
            return

        post_id = self.request.get("post_id")
        comment_content = self.request.get("Comment")
        numoflikes = Like.gql("WHERE post_id = '%s' " % (str(post_id))).count()

        post = self.get_post_by_id(post_id)
        if not post:
            self.error(404)
            return

        if not comment_content:
            comments = Comment.gql("WHERE postid = '%s' " % (str(post_id)))
            msg = "Comment Cannot be Empty"
            self.render("permalink.html",
                        post=post,
                        comments=comments,
                        error=msg,
                        numoflikes=numoflikes)
            return
        author_name = self.get_user_name()
        uid = self.get_uid()
        comment_ins = Comment(content=comment_content,
                              postid=post_id,
                              author=author_name,
                              user_id=uid)
        comment_ins.put()
        comments = Comment.gql("WHERE postid = '%s' " % (str(post_id)))

        self.render("permalink.html",
                    post=post,
                    comment_ins=None,
                    comments=comments,
                    numoflikes=numoflikes)
        return