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).order_by( Comment.timestamp.desc()).paginate(page, per_page=per_page) comments = pagination.items form = CommentForm() if form.validate_on_submit(): author = form.name.data body = form.comment.data # 必须加入post=post参数,否则无法显示出对应评论,因为comment不知道自己属于哪一篇文章 comment = Comment(author=author, body=body, post=post) 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() flash('评论成功!', 'success') 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['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() reviewed = False if current_user == post.author: reviewed = True if form.validate_on_submit(): if current_user.confirmed: author = current_user._get_current_object() author_name = author.username body = form.body.data if post.author == current_user: comment = Comment(author=author, author_name=author_name, body=body, post=post, reviewed=reviewed) else: comment = Comment(author=author, author_name=author_name, body=body, post_id_r=post.id, 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 post.author == current_user: flash('评论发布成功.', 'success') else: flash('你的评论将在作者审阅后发布.', 'info') return redirect(url_for('.show_post', post_id=post_id)) else: message = Markup('请先在邮箱中确认您的账号.' '没收到邮件?' '<a class="alert-link" href="%s">重新发送确认邮件</a>' % url_for('auth.resend_confirm_email')) flash(message, 'warning') 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 fake_comment(count=150): for i in range(count): comment = Comment( author=fake.name(), body=fake.sentence(), timestamp=fake.date_time_this_year(), post=Post.query.get(random.randint(1, Post.query.count())), reviewed=True ) db.session.add(comment) db.session.commit() salt = int(0.1*count) for i in range(salt): comment = Comment( author='一头特立独行的猪', from_admin=True, reviewed=True, body=fake.sentence(), timestamp=fake.date_time_this_year(), post=Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) db.session.commit() for i in range(salt): comment = Comment( author=fake.name(), body=fake.sentence(), timestamp=fake.date_time_this_year(), replied=Comment.query.get(random.randint(1, Comment.query.count())), post=Post.query.get(random.randint(1, Post.query.count())), reviewed=False ) db.session.add(comment) db.session.commit() for i in range(salt): comment = Comment( author=fake.name(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, replied=Comment.query.get(random.randint(1, Comment.query.count())), post=Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) db.session.commit()
def fake_comments(count=500): for i in range(count): user = Admin.query.get(random.randint(1, Admin.query.count())) comment = Comment( author_name=user.username, body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, post=Post.query.get(random.randint(1, Post.query.count())), author=user ) db.session.add(comment) salt = int(count * 0.1) for i in range(salt): user = Admin.query.get(random.randint(1, Admin.query.count())) post = Post.query.get(random.randint(1, Post.query.count())) comment = Comment( author_name=user.username, body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=False, author=user, post_id_r=post.id ) db.session.add(comment) db.session.commit() for i in range(salt): user = Admin.query.get(random.randint(1, Admin.query.count())) comment = Comment( author_name=user.username, body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, replied=Comment.query.get(random.randint(1, Comment.query.count())), post=Post.query.get(random.randint(1, Post.query.count())), author=user ) db.session.add(comment) db.session.commit()
def fake_comment(count=150): for i in range(count): comment = Comment( admin=Admin.query.get(random.randint(1, Admin.query.count())), body=fake.sentence(), timestamp=fake.date_time_this_year(), post=Post.query.get(random.randint(1, Post.query.count())), reviewed=True ) db.session.add(comment) db.session.commit() for i in range(count): comment = Comment( admin=Admin.query.get(random.randint(1, Admin.query.count())), body=fake.sentence(), timestamp=fake.date_time_this_year(), post=Post.query.get(random.randint(1, Post.query.count())), replied=Comment.query.get(random.randint(1, Comment.query.count())), reviewed=True ) db.session.add(comment)
def post(post_id): form = CommentForm() if form.validate_on_submit(): new_comment = Comment(id=str(uuid4()), 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 = Post.query.get_or_404(post_id) tags = post.tags comments = post.comments.order_by(Comment.date.desc()).all() recent, top_tags = siderbar_data() return render_template('post.html', post=post, tags=tags, comments=comments, form=form, recent=recent, top_tags=top_tags)
def comment(request): if request.method == "POST": comm = request.POST.get('comment') if 'username' in request.session: username = request.session['username'] user = User.objects.get(userName=username) comment = Comment() comment.user = user comment.comm = comm comment.save() return HttpResponseRedirect('/gbook/1/') else: return render(request, 'temp/login.html')