示例#1
0
    def post(self, post_id, comment_id=None):
        """
        Adds a new comment to the post
        """
        content = self.request.get('content')

        key = db.Key.from_path('Post', int(post_id), parent=blog_key())
        post = db.get(key)

        if not post:
            self.error(404)
            return

        if not self.user:
            user_name = "Anon"
            # self.redirect('/blog')
        else:
            user_name = self.user.name

        if not content:
            self.render("permalink.html", post=post, comments=post.comments,
                        user=self.user,
                        error_comment="No text provided for comment")
            return
        # Create comment
        if not comment_id:
            comment = Comment(parent=blog_key(), post=post,
                              user_name=user_name, content=content)
        # Edit comment
        else:
            key = db.Key.from_path('Comment', int(comment_id),
                                   parent=blog_key())
            comment = db.get(key)
            if not comment:
                self.error(404)
                return
            # Make sure comment author is same as current user before
            # making the edit. If there is a mismatch, redirect to post page
            if not (comment.user_name == self.user.name):
                self.redirect('/blog/%s' % post_id)
                return
            comment.content = content
        comment.put()
        sleep(0.1)
        self.redirect('/blog/%s' % post_id)
示例#2
0
    def get(self, post_id):
        key = db.Key.from_path('Post', int(post_id), parent=blog_key())
        self.post_obj = db.get(key)

        if not self.post_obj:
            self.error(404)
            return

        self.render("permalink.html", post=self.post_obj,
                    comments=self.post_obj.comments)
示例#3
0
    def get(self, post_id, comment_id):
        if post_id and comment_id and self.user:
            key = db.Key.from_path('Comment', int(
                comment_id), parent=blog_key())
            c = db.get(key)

            if not c:
                self.error(404)
                return

            self.render('editcomment.html', content=c.content)
示例#4
0
    def post(self, post_id=None):
        """ Handles post request (clicking the Submit button)"""

        # If no user is logged in, redirect to login page
        if not self.user:
            self.redirect('/login')
            return

        subject = self.request.get('subject')
        content = self.request.get('content')

        if subject and content:
            # Edit post
            if post_id:
                key = db.Key.from_path('Post', int(post_id), parent=blog_key())
                p = db.get(key)
                if not p:
                    self.error(404)
                    return

                # If current user is not the post author, then do nothing and 
                # redirect to the post page
                if not (p.user.name == self.user.name):
                    self.redirect('/blog/%s' % post_id)
                    return
                p.subject = subject
                p.content = content
                title = "edit post"
            # Create post
            else:
                title = "new post"
                p = Post(parent=blog_key(), title=title, subject=subject,
                         content=content, user=self.user)
            p.put()
            self.redirect('/blog/%s' % str(p.key().id()))
        else:
            error = "subject and content, please!"
            self.render("createoreditpost.html", subject=subject,
                        content=content, error=error)
示例#5
0
    def get(self, user_name=None, post_id=None):
        if post_id and user_name:
            key = db.Key.from_path('Post', int(post_id), parent=blog_key())
            post = db.get(key)
            if not post:
                self.error(404)
                return
            # Check if current user is author of the post, and if so,
            # do nothing and return to the blog page
            if user_name == post.user.name:
                self.redirect('/blog/%s' % post_id)

            # Check if user has already liked the post before, and if so,
            # do nothing and return to the blog page
            if post.has_user_liked(user_name):
                self.redirect('/blog/%s' % post_id)
                return
            l = Like(parent=blog_key(), post_id=post_id,
                     user_name=user_name)
            l.put()
            sleep(0.1)
            self.redirect('/blog/%s' % post_id)
示例#6
0
 def get(self, post_id):
     if post_id and self.user:
         key = db.Key.from_path('Post', int(post_id), parent=blog_key())
         p = db.get(key)
         if not p:
             self.error(404)
             return
         if p.user.name == self.user.name:
             p.delete()
             sleep(0.1)
             self.redirect('/blog')
     else:
         self.error(404)
         return
示例#7
0
 def get(self, post_id=None):
     subject = ""
     content = ""
     title = "new post"
     if post_id:
         key = db.Key.from_path('Post', int(post_id), parent=blog_key())
         post = db.get(key)
         subject = post.subject
         content = post.content
         title = "edit post"
     if self.user:
         self.render("createoreditpost.html", title=title,
                     subject=subject, content=content)
     else:
         self.redirect("/login")
示例#8
0
    def get(self, post_id, comment_id):
        if post_id and comment_id and self.user:
            key = db.Key.from_path('Comment', int(
                comment_id), parent=blog_key())
            c = db.get(key)

            if c and c.user_name == self.user.name:

                c.delete()
                sleep(0.1)
                self.redirect('/blog/%s' % post_id)
            else:
                self.error(404)
                return
        else:
            self.error(404)
            return