def post(self, post_id): """ POST method for /blog/post/delete/<post_id> action. Validates the current user and if they can delete the post and remove it from database. """ if not self.user: return self.redirect("/login") if post_id: post = Post.by_id(int(post_id)) if not post: return self.render("error_page.html", error="Post does not exists") if post.user.key().id() != self.user.key().id(): return self.render("error_page.html", error="You are not the owner of this post") post.delete() time.sleep(0.5) return self.redirect("/blog/posts") else: return self.render("error_page.html", error="Post does not exists")
def post(self, post_id): """ POST method for /blog/post/like/<post_id> action. Validates the current user and if they can like the post and insert a new like row on database. """ if not self.user: return self.redirect("/login") post = Post.by_id(int(post_id)) user = self.user if not post: error_message = "You can't like posts that does not exists" return self.render("error_page.html", error=error_message) else: like = Like.get_like(user=user, post=post).get() if post and post.user.key().id() == user.key().id(): error_message = "You can't like your own posts" return self.render("error_page.html", error=error_message) if like and like.do_like: like.do_like = False elif like and not like.do_like: like.do_like = True else: like = Like(post=post, user=self.user, do_like=True) like.put() time.sleep(0.5) self.redirect("/blog/post/%s" % post_id)
def post(self, post_id): """ POST method for /blog/post/<post_id> action. Validates the inputs and redirects the user to the post page if validations are ok. If not, render the post's page, keeping the field values, and show error messages. """ post = Post.by_id(int(post_id)) input_comment = self.request.get("comment") comment_error = False comments = Comment.get_all(post) if not input_comment: comment_error = True return self.render("show_post.html", user=self.user, post=post, comments=comments, comment_error=comment_error) comment = Comment(comment=input_comment, user=self.user, post=post) comment.put() time.sleep(0.5) self.redirect("/blog/post/%s" % post_id)
def post(self, post_id): """ POST method for /blog/post/edit/<post_id> action. Validates the inputs and redirects the user to the post page if validations are ok. If not, render the edit post's page, keeping the field values, and show error messages. """ if not self.user: return self.redirect("/login") input_subject = self.request.get("subject") input_content = self.request.get("content") post = Post.by_id(int(post_id)) if post.user.key().id() != self.user.key().id(): return self.render("error_page.html", error="You are not the owner of this post") if not (input_subject and input_content): error_message = "Please inform subject and content" self.render("edit_post.html", error_message=error_message, subject=input_subject, content=input_content, post=post) else: post.subject = input_subject post.content = input_content post.put() post_id = post.key().id() self.redirect("/blog/post/%s" % post_id)
def get(self, posts=""): """ GET method for /blog/posts action. Retrieve all the posts from database and render the main blog view. """ posts = list(Post.get_all()) self.render("blog.html", user=self.user, posts=posts)
def get(self, post_id): """ GET method for /blog/post/edit/<post_id> action. Render the edit post's page. """ if not self.user: return self.redirect("/login") if post_id: post = Post.by_id(int(post_id)) if not post: return self.render("error_page.html", error="Post does notexists") if post.user.key().id() != self.user.key().id(): return self.render("error_page.html", error="You are not the owner of this post") return self.render("edit_post.html", user=self.user, subject=post.subject, content=post.content, post=post) else: return self.render("error_page.html", error="Post does not exists")
def get(self, post_id): """ GET method for /blog/post/<post_id> action. Render the post's page, plus comments and comment area. """ if post_id and post_id.isdigit(): post = Post.by_id(int(post_id)) if not post: return self.render("error_page.html", error="Post does notexists") comments = list(Comment.get_all(post)) like = Like.get_like(user=self.user, post=post).get() count_likes = Like.count_likes(post=post) self.render("show_post.html", user=self.user, post=post, like=like, count_likes=count_likes, comments=comments) else: return self.render("error_page.html", error="Post does not exists")
def get_all_posts(user): teams = user.teams posts = Post.objects(team__in=teams) return jsonify({'posts': [post.to_json() for post in posts]})
def create_a_post(post_json): content = post_json['content'] team = post_json['team'] post = Post(content=content, team=team) post.save() return jsonify(post.to_json())