def show_post(post_id): post = Post.query.get_or_404(post_id) if post.private and not current_user.is_authenticated: flash("你没有权限访问该文章!", "warning") return redirect(url_for(".index")) page = request.args.get("page", 1, type=int) per_page = current_app.config.get("BLOG_COMMENT_PER_PAGE", 15) pagination = ( Comment.query.with_parent(post) .filter_by(reviewed=True) .order_by(Comment.timestamp.asc()) .paginate(page, per_page=per_page) ) comments = pagination.items if current_user.is_authenticated: form = AdminCommentForm() form.author.data = current_user.name form.email.data = current_app.config["BLOG_EMAIL"] form.site.data = url_for(".index") from_admin = True reviewed = True else: form = CommentForm() from_admin = False reviewed = False if form.validate_on_submit(): author = form.author.data email = form.email.data site = form.site.data body = form.body.data comment = Comment( author=author, email=email, site=site, body=body, from_admin=from_admin, post=post, reviewed=reviewed, ) replied_id = request.args.get("reply") if replied_id: replied_comment = Comment.query.get_or_404(replied_id) comment.replied = replied_comment send_new_reply_email(replied_comment) db.session.add(comment) db.session.commit() if current_user.is_authenticated: flash("Comment published.", "success") else: flash("Thanks, your comment will be published after reviewed.", "info") send_new_comment_email(post) return redirect(url_for(".show_post", post_id=post_id)) return render_template( "blog/post.html", post=post, pagination=pagination, comments=comments, form=form )
def show_post(post_id): post = Post.query.get_or_404(post_id) page = request.args.get('page', 1, type=int) per_page = current_app.config['BLUELOG_COMMENT_PER_PAGE'] pagination = Comment.query.with_parent(post).filter_by( reviewed=True).order_by(Comment.timestamp.asc()).paginate( page, per_page) comments = pagination.items if current_user.is_authenticated: form = AdminCommentForm() form.author.data = current_user.name form.email.data = current_app.config['BLUELOG_EMAIL'] form.site.data = url_for('.index') from_admin = True reviewed = True else: form = CommentForm() from_admin = False reviewed = False if form.validate_on_submit(): author = form.author.data email = form.email.data site = form.site.data body = form.body.data comment = Comment(author=author, email=email, site=site, body=body, from_admin=from_admin, post=post, reviewed=reviewed) replied_id = request.args.get('reply') if replied_id: replied_comment = Comment.query.get_or_404(replied_id) comment.replied = replied_comment send_new_reply_email(replied_comment) db.session.add(comment) db.session.commit() if current_user.is_authenticated: flash('Comment pulished', 'success') else: flash('thanks,you comment will be published after reviewed', 'info') send_new_comment_email(post) return redirect(url_for('.show_post', post_id=post_id)) return render_template('blog/post.html', post=post, pagination=pagination, form=form, comments=comments)
def show_post(post_id): post = Post.query.get_or_404(post_id) page = request.args.get('page', 1, type=int) per_page = current_app.config['MYBLOG_COMMENT_PER_PAGE'] pagination = Comment.query.with_parent(post).filter_by( reviewed=True).order_by(Comment.timestamp.asc()).paginate( page, per_page) comments = pagination.items if current_user.is_authenticated: form = AdminCommentForm() form.author.data = current_user.username form.email.data = current_app.config['MYBLOG_EMAIL'] from_admin = True reviewed = True else: form = CommentForm() from_admin = False reviewed = False if form.validate_on_submit(): author = form.author.data email = form.email.data body = form.body.data comment = Comment(author=author, email=email, body=body, from_admin=from_admin, post=post, reviewed=reviewed) replied_id = request.args.get('reply') if replied_id: replied_comment = Comment.query.get_or_404(replied_id) comment.replied = replied_comment db.session.add(comment) db.session.commit() if current_user.is_authenticated: flash('评论发布成功', 'success') else: flash('您的评论将在管理员审核后发布', 'info') return redirect(url_for('main.show_post', post_id=post_id)) if request.args.get('from_index') == 'True': post.read_count += 1 db.session.add(post) db.session.commit() return render_template('posts/post.html', post=post, pagination=pagination, form=form, comments=comments)
def show_post(post_id): post = Post.query.get_or_404(post_id) page = request.args.get('page', 1, type=int) per_page = current_app.config['MYBLOG_MANAGE_POST_PER_PAGE'] # with_parent(post)表示当前文章下的所有评论,filter_by(reviewed=True)表示通过审核的评论 pagination = Comment.query.with_parent(post).filter_by(reviewed=True).order_by(Comment.timestamp.desc()).paginate( page, per_page) comments = pagination.items if current_user.is_authenticated: # 如果当前用户已登陆,使用管理员表单 form = AdminCommentForm() form.author.data = current_user.name form.email.data = current_app.config['MYBLOG_EMAIL'] form.site.data = url_for('.index') from_admin = True reviewed = True else: # 否则使用普通表单 form = CommentForm() from_admin = False reviewed = False if form.validate_on_submit(): author = form.author.data email = form.email.data site = form.site.data body = form.body.data comment = Comment( author=author, email=email, site=site, body=body, from_admin=from_admin, post=post, reviewed=reviewed) replied_id = request.args.get('reply') if replied_id: # 若果存在参数reply,表示是回复 replied_comment = Comment.query.get_or_404(replied_id) comment.replied = replied_comment send_new_reply_email(replied_comment) db.session.add(comment) db.session.commit() if current_user.is_authenticated: # send message based on authentication status flash('回复成功', 'success') else: flash('谢谢,您的评论将在审核后发表', 'info') send_new_comment_email(post) # send notification email to admin return redirect(url_for('.show_post', post_id=post_id)) return render_template('blog/post.html', post=post, pagination=pagination, form=form, comments=comments)
def show_post(post_id): post = Post.query.get_or_404(post_id) page = request.args.get('page', 1, type=int) per_page = current_app.config['MYBLOG_COMMENT_PER_PAGE'] pagination = Comment.query.with_parent(post).filter_by(reviewed=True).order_by(Comment.timestamp.desc()).paginate( page, per_page) comments = pagination.items if current_user.is_authenticated: form = AdminCommentForm() form.author.data = current_user.name form.email.data = current_app.config['MAIL_DEFAULT_SENDER'] from_admin = True reviewed = True else: form = CommentForm() from_admin = False reviewed = False if form.validate_on_submit(): author = form.author.data email = form.email.data # site = form.site.data body = form.body.data comment = Comment(author=author,body=body,post=post,email=email, reviewed=reviewed) replied_id = request.args.get('reply') if replied_id: replied_comment = Comment.query.get_or_404(replied_id) comment.replied = replied_comment send_new_reply_email(replied_comment) db.session.add(comment) db.session.commit() if current_user.is_authenticated: # send message based on authentication status flash('Comment published.', 'success') else: flash('Thanks, your comment will be published after reviewed.', 'info') send_new_comment_email(post) # send notification email to admin return redirect(url_for('.show_post', post_id=post_id) + '#comment-form') return render_template('blog/post.html', post=post, pagination=pagination, form=form, comments=comments)
def show_post(post_id): """ 文章详情页:文章内容+评论+评论表单 :param post_id: :return: """ # 获取文章详情 post = Post.query.get_or_404(post_id) # 评论信息 page = request.args.get('page', 1, type=int) per_page = current_app.config['BLOG_COMMENT_PER_PAGE'] pagination = Comment.query.with_parent(post).filter_by( reviewed=True).order_by(Comment.timestamp.desc()).paginate( page, per_page) comments = pagination.items # 验证用户是否登录, # 如果登录则将相关数据填入表单 if current_user.is_authenticated: form = AdminCommentForm() form.author.data = current_user.name form.email.data = current_app.config['BLOG_EMAIL'] form.site.data = url_for('.index') from_admin = True reviewed = True # 直接通过审核 else: form = CommentForm() from_admin = False reviewed = False # 表单验证通过后写入数据库 if form.validate_on_submit(): author = form.author.data email = form.email.data site = form.site.data body = form.body.data comment = Comment(author=author, email=email, site=site, body=body, from_admin=from_admin, post=post, reviewed=reviewed) # 对于评论的回复 # 试图获取是否为回复:如果是回复,则添加对应的字段 replied_id = request.args.get('reply') if replied_id: replied_comment = Comment.query.get_or_404(replied_id) comment.replied = replied_comment # 为回复建立与评论的关系 # todo 邮件发送功能 # send_new_reply_email(replied_comment) db.session.add(comment) db.session.commit() # 如果管理员登录则显示发布成功,如果不是则发送邮件通知管理员 if current_user.is_authenticated: flash('Comment published.', 'success') else: flash('Thanks, your comment will be published after reviewed.', 'info') # todo 邮件发送功能 # send_new_comment_email(post) return redirect(url_for('.show_post', post_id=post_id)) return render_template('blog/post.html', post=post, pagination=pagination, form=form, comments=comments)