def downvote_post(name, title): """ Route that handles downvoting a post as the current user. """ post = post_service.get_post(title, name) if post: post_service.downvote_post(post.id, current_user.id) return redirect(request.referrer) else: abort(404)
def delete_post(name, title): """ Route that handles deleting a post. """ post = post_service.get_post(title, name) if post: if post.user_id != current_user.id: return redirect(url_for("post.post", name=name, title=title)) post_service.delete_post(post) flash("Successfully deleted post.", "primary") return redirect(url_for("community.community", name=name)) else: abort(404)
def reply(name, title): """ Route for creating a reply. On a GET request, it returns the reply creation form. On a POST request, it handles creating a reply. """ post = post_service.get_post(title, name) if post: form = ReplyForm() if form.validate_on_submit(): reply_service.create_reply(form.reply.data, post, current_user) flash("Successfully created reply.", "primary") return redirect(url_for("post.post", name=name, title=title)) return render_template("create_reply.jinja2", name=name, title=title, form=form) else: abort(404)
def update_post(name, title): """ Route for updating a post. On a GET request, it returns the post update form. On a POST request, it handles updating a post. """ post = post_service.get_post(title, name) if post: if post.user_id != current_user.id: return redirect(url_for("post.post", name=name, title=title)) form = UpdatePostForm() if form.validate_on_submit(): post_service.update_post(post, form.post.data) flash("Successfully updated post.", "primary") return redirect(url_for("post.post", name=name, title=title)) form.post.data = post.post return render_template("update_post.html", name=name, title=title, form=form) else: abort(404)