def post(post_id): """View function for post page""" # Form object:'Comment' form = CommentForm() # form.validate_on_submit() will be true and return the # data object to form instance from user enter # when the HTTP request is POST # form.validata_on_submit() 方法会隐式的判断该 HTTP 请求是不是 POST, # 若是, 则将请求中提交的表单数据对象传入上述的 form 对象并进行数据检验. if form.validate_on_submit(): new_comment = Comment(name=form.name.data) new_comment.text = form.text.data new_comment.date = datetime.datetime.now() new_comment.post_id = post_id db.session.add(new_comment) db.session.commit() post = db.session.query(Post).get_or_404(post_id) tags = post.tags comments = post.comments.order_by(Comment.date.desc()).all() recent, top_tags = sidebar_data() return render_template('post.html', post=post, tags=tags, comments=comments, recent=recent, top_tags=top_tags, form=form)
def view_post(post_id): """VIEW-POST page displays post information alongwith comments.""" post = Post.query.get(post_id) if post: form = CommentForm() if form.validate_on_submit(): comment = Comment(user=current_user, post=post, body=form.data.get("body")) db.session.add(comment) db.session.commit() flash("Comment sucessfully posted.") return render_template( "view-post.html", user=current_user, post=post, form=form, page_title="VIEW-POST", page_color="purple", ) else: next_page = request.headers.get("Referer") flash("Blog post not found.") return redirect(next_page or url_for("app.home"))
def post_comment(post_id): search = SearchForm() post = Post.query.get_or_404(post_id) form = CommentForm() if form.validate_on_submit(): db.session.add(Comment(content=form.comment.data, post_id=post.id, author_id=current_user.id)) db.session.commit() flash("Your comment has been added to the post") return redirect(f'/post/{post.id}') comments = Comment.query.filter(Comment.post_id == post.id) return render_template('post.html', post=post, comments=comments, form=form, search=search)
def post_comment(post_id): post = Post.query.get_or_404(post_id) form = CommentForm() # like = LikeForm() if form.validate_on_submit(): db.session.add(Comment(content=form.comment.data, post_id=post.id, author_id=current_user.id)) db.session.commit() flash("Congratulations! You left a comment!") return redirect(f'/post/{post.id}') comments = Comment.query.filter(Comment.post_id == post.id) return render_template('post.html', post=post, comments=comments, form=form)
def post(post_id): post = Post.query.get_or_404(post_id) form = CommentForm() comments=Comment.query.all() if form.validate_on_submit(): comment = Comment(content=form.content.data) db.session.add(comment) db.session.commit() flash('Your comment has been posted!', 'primary') return redirect(url_for('home')) return render_template('blog.html', title=post.title, post=post,form=form,comments=comments)
def individual_blog(id): form = CommentForm() blog = Post.query.filter_by(id=id).first() comments = Comment.query.filter_by(owner_post=id).all() if form.validate_on_submit(): new_comment = Comment(comment=form.comment.data) db.session.add(new_comment) db.session.commit() new_comment.set_comment_owner(id) return redirect(f"/blogs/{id}") return render_template('individual_blog.html', blog=blog, comments=comments, form=form)
def new_comment(post_id): comments = Comment.query.filter_by(post_id=post_id) form = CommentForm() if form.validate_on_submit(): comment = Comment(comment=form.comment.data, post_id=post_id) db.session.add(comment) db.session.commit() flash('Your comment has been added', 'success') return redirect(url_for('home')) return render_template('new_comment.html', title="New Comment", form=form, comments=comments)
def post(post_id): form = CommentForm() post = Post.query.filter_by(id=post_id).first() comments = Comment.query.filter_by(post_id=post.id).all() if form.validate_on_submit(): comment = Comment(content=form.comment.data, post_id=post.id, user_id=current_user.id) db.session.add(comment) db.session.commit() flash("Comment Added", "success") return redirect(url_for('post', post_id=post.id)) return render_template("post.html", title="Post", post=post, form=form, comments=comments)
def post_comment(post_id): post = Post.query.get_or_404(post_id) form = CommentForm() if form.validate_on_submit(): db.session.add( Comment(content=form.comment.data, post_id=post.id, author_id=current_user.id)) db.session.commit() flash("Your comment has been added to the post", "success") return redirect(f"/post/{post.id}") else: flash("Log in to comment") comments = Comment.query.filter(Comment.post_id == post.id) return render_template("post.html", post=post, comments=comments, form=form)
def post_comment(post_id): if current_user.is_authenticated is False: flash('you need to log in firstly') return redirect(url_for('login')) post = Post.query.get_or_404(post_id) form = CommentForm() if form.validate_on_submit(): db.session.add( Comment(content=form.comment.data, post_id=post.id, author_id=current_user.id)) db.session.commit() flash("Your comment has been added to the post", "success") return redirect(f'/post/{post.id}') comments = Comment.query.filter(Comment.post_id == post.id) return render_template('post.html', post=post, comments=comments, form=form)
def article(id): form = CommentForm() if form.validate_on_submit(): comment = Comment(content=form.content.data, authorEmail=form.authorEmail.data, article_id=id) db.session.add(comment) db.session.commit() flash('Your comment has published successfully!', 'success') return redirect(url_for('article', id=id)) article = Article.query.get_or_404(id) ip_add = request.remote_addr visitor = Visitor(article_id=article.id, visitor_ip=ip_add) result = Visitor.query.filter( and_(Visitor.article_id == article.id, Visitor.visitor_ip == ip_add)).first() if not result: db.session.add(visitor) db.session.commit() return render_template('article.html', article=article, form=form, Comment=Comment, len=len)
def edit_comment(comment_id): """EDIT-COMMENT page helps in editing of comment.""" comment = Comment.query.get(comment_id) if comment: form = CommentForm(obj=comment) if form.validate_on_submit(): comment.body = form.data.get("body") db.session.add(comment) db.session.commit() flash("Comment sucessfully edited.") return redirect(url_for("app.view_post", post_id=comment.post_id)) return render_template( "comment.html", user=current_user, form=form, page_title="EDIT-COMMENT", page_color="purple", ) else: next_page = request.headers.get("Referer") flash("Comment not found.") return redirect(next_page or url_for("app.home"))