Beispiel #1
0
    def delete(self, post_id):
        blog = Blog.get_by_id(int(post_id))
        comment = Comment.get_by_id(int(self.request.get("id")))
        cookie_val = self.request.cookies.get("user_id")

        if not self.blog_exists(blog):
            message = "Invalid. The blog page does not exist."
            self.send_response(404, message)
            return
        if not self.comment_exists(comment):
            message = "Invalid. The comment does not exist."
            self.send_response(400, message)
            return
        if not self.is_signed_in(cookie_val):
            message = "Invalid. Must be signed in to edit comment."
            self.send_response(401, message)
            return
        if not self.is_author(cookie_val, comment):
            message = "Invalid. Must be its author to edit this."
            self.send_response(403, message)
            return

        comment.delete()

        message = json.dumps(
            {"success": "The comment has been deleted successfully."})
        self.send_response(200, message)
Beispiel #2
0
    def post(self, post_id):
        title = self.request.get("title")
        content = self.request.get("content")
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))

        if not blog:
            self.response.set_status(404)
            self.redirect("/blog/not_found")
            return
        if not self.is_signed_in(cookie_val):
            self.response.set_status(401)
            self.redirect("/blog/login")
            return
        if not self.is_author(cookie_val, blog):
            self.response.set_status(403)
            self.redirect("/blog/not_authorized")
            return
        if not title and content:
            error = ("Either title or texts are empty. Please fill both in "
                     "before trying again.")
            self.render("updateBlog.html",
                        title=title,
                        content=content,
                        error=error,
                        signed_in=True)
            return

        blog.title = title
        blog.content = content
        blog_id = blog.put().id()

        self.response.set_status(200)
        self.redirect("/blog/%s" % blog_id)
Beispiel #3
0
    def post(self, post_id):
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))

        if not self.is_signed_in(cookie_val):
            message = "Not signed in."
            self.send_response(401, message)
            return
        if not self.blog_exists(blog):
            message = "Post doesn't exist."
            self.send_response(404, message)
            return
        if self.is_author(cookie_val, blog):
            message = "Post cannot be liked by creator."
            self.send_response(400, message)
            return

        user_id = cookie_val.split("|")[0]

        # Check if user already liked the post.
        # Remove if id already in a list.
        if (user_id in blog.liked_by):
            self.remove_like(blog, user_id)
        else:
            self.add_like(blog, user_id)
Beispiel #4
0
    def get(self, post_id):
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))
        comments = (Comment.all().filter("blog =",
                                         blog.key()).order("-date_created"))

        # Determine whether to insert 'Login' or 'Logout' button.
        if (self.is_signed_in(cookie_val)):
            self.render("readBlog.html",
                        blog=blog,
                        signed_in=True,
                        comments=comments)
        # Insert login button
        else:
            self.render("readBlog.html", blog=blog, comments=comments)
Beispiel #5
0
    def get(self, post_id):
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))

        if not blog:
            self.response.set_status(404)
            self.redirect("/blog/not_found")
            return
        if not self.is_signed_in(cookie_val):
            self.response.set_status(401)
            self.redirect("/blog/login")
            return
        if not self.is_author(cookie_val, blog):
            self.response.set_status(403)
            self.redirect("/blog/not_authorized")
            return

        self.render("deleteBlog.html", blog=blog, signed_in=True)
Beispiel #6
0
    def post(self, post_id):
        blog = Blog.get_by_id(int(post_id))
        cookie_val = self.request.cookies.get("user_id")

        if not blog:
            self.response.set_status(404)
            self.redirect("/blog/not_found")
            return
        if not self.is_signed_in(cookie_val):
            self.response.set_status(401)
            self.redirect("/blog/login")
            return
        if not self.is_author(cookie_val, blog):
            self.response.set_status(403)
            self.redirect("/blog/not_authorized")
            return

        blog.delete()

        self.response.set_status(200)
        self.redirect("/blog")
Beispiel #7
0
    def get(self, post_id):
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))

        # Check if user is authorized to modify the blog post.
        #
        # Also, determine whether to insert login or logout button.
        if not blog:
            self.response.set_status(404)
            self.redirect("/blog/not_found")
            return
        if not self.is_signed_in(cookie_val):
            self.response.set_status(401)
            self.redirect("/blog/login")
            return
        if not self.is_author(cookie_val, blog):
            self.response.set_status(403)
            self.redirect("/blog/not_authorized")
            return

        self.render("updateBlog.html", blog=blog, signed_in=True)
Beispiel #8
0
    def post(self, post_id):
        data = json.loads(self.request.body)
        cookie_val = self.request.cookies.get("user_id")
        blog = Blog.get_by_id(int(post_id))
        title = data["title"]
        content = data["content"]
        user = User.get_by_id(int(cookie_val.split("|")[0]))

        if not self.blog_exists(blog):
            message = "Invalid. The requested page doesn't exist."
            self.send_response(404, message)
            return
        if not self.is_signed_in(cookie_val):
            message = "Invalid. Only signed in User can post comments"
            self.send_response(401, message)
            return
        if not (title and content):
            message = "Invalid. Title and texts must not be empty."
            self.send_response(400, message)
            return

        comment = Comment(title=title, content=content, blog=blog, author=user)
        comment_id = comment.put().id()

        message = json.dumps({
            "success":
            "Comment successfully added to database.",
            "id":
            comment_id,
            "title":
            title,
            "content":
            content,
            "author":
            user.username,
            "date_created":
            comment.date_created.strftime("%B %d %Y %I:%M%p")
        })
        self.send_response(200, message)
Beispiel #9
0
    def put(self, post_id):
        data = json.loads(self.request.body)
        comment = Comment.get_by_id(int(data["id"]))
        blog = Blog.get_by_id(int(post_id))
        cookie_val = self.request.cookies.get("user_id")
        new_title = data["title"]
        new_content = data["content"]

        if not (new_content and new_title):
            message = "Invalid. Both title and comment must not be empty."
            self.send_response(400, message)
            return
        if not self.blog_exists(blog):
            self.send_response(404)
            return
        if not self.comment_exists(comment):
            message = "Invalid. The comment does not exist."
            self.send_response(400, message)
            return
        if not self.is_signed_in(cookie_val):
            message = "Invalid. Must be signed in to edit comments."
            self.send_response(400, message)
            return
        if not self.is_author(cookie_val, comment):
            message = "Invalid. Only its author is allowed to edit."
            self.send_response(400, message)
            return

        comment.title = new_title
        comment.content = new_content
        comment.put()

        message = json.dumps(
            {"success": "The comment has been updated successfully."})
        self.send_response(200, message)
        self.response.set_status(200)