def fake_comments(count=500): for _ in range(int(count * 0.8)): comment = Comment(author=fake.name(), email=fake.email(), body=fake.sentence(), timestamp=fake.date_time_this_year(), read=True, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) salt = int(count * 0.1) for _ in range(salt): # 未读评论 comment = Comment(author=fake.name(), email=fake.email(), body=fake.sentence(), timestamp=fake.date_time_this_year(), read=False, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) # 管理员评论 comment = Comment(author='admin', email='', body=fake.sentence(), timestamp=fake.date_time_this_year(), from_admin=True, read=True, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) db.session.commit()
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 form = CommentForm() from_admin = False reviewed = False #注意,如果为False则不会在页面中显示,因为32行规定只有为True才会显示,所以提交评论后应该在评论批准里批准为True 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, reviewed=reviewed, post=post) replied_comment_id = request.args.get('reply_to') if replied_comment_id: replied_comment = Comment.query.get_or_404(replied_comment_id) comment.replied = replied_comment db.session.add(comment) db.session.commit() send_comment_mail(post) #发邮件提醒评论 flash('Comment success', 'info') return redirect(url_for('blog.show_post', post_id=post_id)) return render_template('blog/post.html', post=post, pagination=pagination, comments=comments, form=form)
def show_post(slug): post = Post.query.filter_by(slug=slug).first_or_404() # 显示评论 page = request.args.get('page', 1, type=int) per_page = current_app.config['BLUELOG_POST_PER_PAGE'] pagination = Comment.query.with_parent(post).filter_by( reviewed=True).order_by(Comment.timestamp.desc()).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['BLUELOG_EMAIL'] form.site.data = url_for('blog.index') from_admin = True reviewed = True else: form = CommentForm() from_admin = False # TODO: 已经检查 reviewed = True 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 # 存入数据库 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') # 发送提示邮件给管理员 return redirect( url_for('blog.show_post', slug=post.slug) + '#comments') 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=per_page) comments = pagination.items # 发表评论 # 如果当前用户通过验证 if current_user.is_authenticated: # 实例化AdminCommentForm类,并对三个隐藏字段赋予相应的值 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') # 如果请求URL的查询字符串中是否包含replied_id的值,如果包含,则表示提交的评论时一条回复 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, form=form, comments=comments)
def fake_comments(count=500): ''' 生成虚拟评论 ''' for i in range(count): comment = Comment( author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, post=Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) salt = int(count * 0.1) for i in range(salt): # 未审核评论 comment = Comment( author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=False, post=Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) # 管理员发表评论 comment = Comment( author='雨轩恋i', email='*****@*****.**', site='tyutltf.com', body=fake.sentence(), timestamp=fake.date_time_this_year(), from_admin=True, reviewed=True, 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(), email=fake.email(), site=fake.url(), 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 show_post(post_id): '''显示博客条目详情''' post = Post.query.get_or_404(post_id) page = request.args.get('page', 1, int) per_page = current_app.config['BLUELOG_COMMENT_PER_PAGE'] # 知道post,得到post对应所有的comment,这里有好几种方式,可以试一下 pagination = Comment.query.with_parent(post).filter_by(reviewed=True).order_by(Comment.timestamp.desc()).paginate( page, per_page) # 这种写法无法排序 # pagination = post.comments.filter_by(reviewed=True).order_by(Comment.timestamp.asc()).paginate(page, per_page) comments = pagination.items if current_user.is_authenticated: # 如果当前用户是管理员admin # 隐藏字段赋值 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) # 根据reply_comment 这个endpoint的参数 # replied_id就是对Comment进行reply的Comment的id # 这个字段是Comment表中的外键,关联的是主键id replied_id = request.args.get('reply') if replied_id: # 如果这个不为空,则是对comment评论的reply 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('.show_post', post_id=post_id)) # else: # flash('form validate not passed!') # return render_template('blog/post.html', post=post, pagination=pagination, comments=comments, form=form) return render_template('blog/post.html', post=post, pagination=pagination, comments=comments, form=form)
def fake_comments(count=500): """生成虚拟评论""" for i in range(count): comment = Comment( author=fake.name(), email=fake.ascii_email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, post=Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) salt = int(count * 0.1) for i in range(salt): # 未审核评论 comment = Comment( author=fake.name(), email=fake.ascii_email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=False, post=Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) # 管理员发表的评论 comment = Comment( author='meizhaohui', email=fake.ascii_email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), from_admin=True, reviewed=True, 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(), email=fake.ascii_email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, post=Post.query.get(random.randint(1, Post.query.count())), replied=Comment.query.get(random.randint(1, Comment.query.count())) ) db.session.add(comment) db.session.commit()
def fake_comments(count=500): for i in range(count): comment = Comment( author=fake.name(), email=fake.email(), site=fake.url(), body=fake.text(random.randint(50,300)), timestamp=fake.date_time_this_year(), reviewed=True, post=Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) salt = int(count * 0.1) for i in range(salt): # 未审核的评论 comment = Comment( author=fake.name(), email=fake.email(), site=fake.url(), body=fake.text(random.randint(50,300)), timestamp=fake.date_time_this_year(), reviewed=False, post=Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) # 管理员的评论 comment = Comment( author='AzureB4', email='*****@*****.**', site='archcst.me', body=fake.text(random.randint(50,300)), timestamp=fake.date_time_this_year(), reviewed=True, from_admin=True, 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(), email=fake.email(), site=fake.url(), body=fake.text(random.randint(50,300)), timestamp=fake.date_time_this_year(), reviewed=True, # replied_id=(random.randint(1, Comment.query.count())) 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): comment = Comment( author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, post=Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) salt = int(count * 0.1) for i in range(salt): # unreviewed comments comment = Comment( author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=False, post=Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) # from admin comment = Comment( author='Mima Kirigoe', email='*****@*****.**', site='example.com', body=fake.sentence(), timestamp=fake.date_time_this_year(), from_admin=True, reviewed=True, post=Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) db.session.commit() # replies for i in range(salt): comment = Comment( author=fake.name(), email=fake.email(), site=fake.url(), 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): # 添加审查通过的comment comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.text(), timestamp=fake.date_time_this_year(), post_id=Post.query.get( random.randint(1, Post.query.count())).id, reviewed=True) db.session.add(comment) salt = int(count * 0.1) for i in range(salt): # 添加未审查通过的comment comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.text(), timestamp=fake.date_time_this_year(), post_id=Post.query.get( random.randint(1, Post.query.count())).id, reviewed=False) db.session.add(comment) # from admin comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.text(), timestamp=fake.date_time_this_year(), post_id=Post.query.get( random.randint(1, Post.query.count())).id, reviewed=False, from_admin=True) db.session.add(comment) for i in range(salt): # replies comment = Comment( author=fake.name(), email=fake.email(), site=fake.url(), body=fake.text(), timestamp=fake.date_time_this_year(), post_id=Post.query.get(random.randint(1, Post.query.count())).id, replied=Comment.query.get(random.randint(1, Comment.query.count())), reviewed=False, from_admin=True) db.session.add(comment) try: db.session.commit() except IntegrityError: db.session.rollback()
def show_post(post_id): post = Post.query.get_or_404(post_id) comment_page = request.args.get('page', 1, type=int) comment_per_page = current_app.config['BLUELOG_COMMENT_PER_PAGE'] pagination = Comment.query.with_parent(post).filter_by( reviewed=True).order_by(Comment.timestamp.desc()).paginate( page=comment_page, per_page=comment_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', type=int) if replied_id: replied_comment = Comment.query.filter_by( id=replied_id).first_or_404() comment.replied = replied_comment send_new_reply_email(replied_comment) # 发送邮件给被恢复的用户 db.session.add(comment) db.session.commit() if current_user.is_authenticated: # 根据登录状态显示不同的提示信息 flash('评论已发布。', 'success') else: flash('感谢你的评论,审核通过后将会显示。', '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 setUp(self): super(AdminTestCase, self).setUp() self.login() category = Category(name='Default') post = Post(title='This is a admin test', body='A simple admin test.', category=category) comment_1 = Comment(body='A admin comment1', post=post, from_admin=True, reviewed=True) comment_2 = Comment(body='A admin comment2', post=post, from_admin=False, reviewed=False) comment_3 = Comment(body='A admin comment3', post=post, from_admin=False, reviewed=False) db.session.add_all([category, post, comment_1, comment_2, comment_3]) db.session.commit()
def show_post(post_id): post = Post.query.get_or_404(post_id) # 获取?page=xxx,默认为1 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 #如果管理员账户登录,在每一篇文章的评论部分会隐藏掉name、email、site字段的输入,使用当前账户信息自动赋值 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: # 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)) return render_template('blog/post.html', post=post, pagination=pagination, form=form, comments=comments)
def fake_comments(count=500): for i in range(count): comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) salt = int(count * 0.1) # 未审核的评论 for i in range(salt): comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=False, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) db.session.commit() # 管理员发表的评论 comment = Comment(author='康熙', email='*****@*****.**', site='www.kangxi.com', body='朕知道了', timestamp=fake.date_time_this_year(), reviewed=True, from_admin=True, 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(), email=fake.email(), site=fake.url(), 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 show_post(post_id): """显示文章""" post = Post.query.get_or_404(post_id) page = request.args.get('page', 1, type=int) perpage = current_app.config['BLUELOG_COMMENT_PER_PAGE'] pagination = Comment.query.with_parent(post).filter_by( reviewed=True).order_by(Comment.timestamp.asc()).paginate( page, perpage) 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: # 如果 URL 中 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: # 根据登录状态现实不同的提示信息 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 fake_comments(count=500): for i in range(count): comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) salt = int(count * 0.1) for i in range(salt): # comment without review comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=False, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) # comment by author comment = Comment(author='ZIO', email='*****@*****.**', site='zio.com', body=fake.sentence(), timestamp=fake.date_time_this_year(), from_admin=True, reviewed=True, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) db.session.commit() # replies for i in range(salt): replied = Comment.query.get(random.randint(1, Comment.query.count())) comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=False, replied=replied, post=replied.post) db.session.add(comment) db.session.commit()
def fake_comments(count=200): for i in range(count): comment = Comment( author = fake.name(), email = fake.email(), site = fake.url(), body = fake.sentence(), timestamp = fake.date_time_this_year(), reviewed = True, #审核过的评论 post = Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) for i in range(int(count * 0.05)): comment = Comment( author = fake.name(), email = fake.email(), site = fake.url(), body = fake.sentence(), timestamp = fake.date_time_this_year(), reviewed = False, #未审核过的评论 post = Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) for i in range(int(count * 0.05)):#来自管理员的评论 comment = Comment( author = 'MXD', email = '*****@*****.**', site = 'www.MXD.com', body = 'Good comment!', timestamp = fake.date_time_this_year(), from_admin = True, reviewed = True, post = Post.query.get(random.randint(1, Post.query.count())) ) db.session.add(comment) db.session.commit() for i in range(int(count * 0.1)): #评论的回复 comment = Comment( author = fake.name(), email = fake.email(), site = fake.url(), 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 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.desc()).paginate( page=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['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, reviewed=reviewed, post=post) 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('评论提交成功', '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, form=form, comments=comments, pagination=pagination)
def fake_comments(count=500): for i in range(count): comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) salt = int(count * 0.1) for i in range(salt): # 未审核的评论 comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=False, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) # 管理员发表的评论 comment = Comment(author='Mima Kirigoe', email='*****@*****.**', site='qq.com', body=fake.sentence(), timestamp=fake.date_time_this_year(), from_admin=True, reviewed=True, 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(), email=fake.email(), site=fake.url(), 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 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.desc()).paginate( page, per_page) comments = pagination.items # 通过是否登陆,登陆给出管理员评论表单,没登陆给出普通匿名用户的登陆表单 if current_user.is_authenticated: # logger.debug("是管理员登陆的!") form = AdminComment() else: # logger.debug("不是管理员登陆的!") form = CommentForm() if form.validate_on_submit(): # 是否为回复评论 reply_id = request.args.get('reply') if current_user.is_authenticated: # 是管理员的评论 admin = Admin.query.first() comment = Comment( author=admin.username, # email=url_for('blog.index'), site=url_for('blog.index'), body=form.body.data, post_id=post_id, from_admin=True, reviewed=True, replied_id=reply_id if reply_id else None) flash('评论发布成功', 'success') else: # 不是管理员的评论 comment = Comment(author=form.author.data, email=form.email.data, site=form.site.data, body=form.body.data, post_id=post_id, from_admin=False, reviewed=False, replied_id=reply_id if reply_id else None) db.session.add(comment) db.session.commit() flash('你的评论需要审核', 'info') 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_comments(count=500): # 500 verified comments for i in range(count): comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) # 50 unverified comments / admin comments / replies salt = int(count * 0.1) for i in range(salt): comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=False, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) comment = Comment(author='Tuna Hsu', email='*****@*****.**', site='https://tunahsu.com', body=fake.sentence(), timestamp=fake.date_time_this_year(), from_admin=True, reviewed=True, 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(), email=fake.email(), site=fake.url(), 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 show_post(post_id, page): post = Post.query.get_or_404(post_id) per_page = current_app.config['BLUELOG_COMMENT_PER_PAGE'] pagination = Comment.query.with_parent(post).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_username 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 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') 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, reviewed=reviewed) replied_id = request.args.get('reply') if replied_id: replied_comment = Comment.query.get_or_404(replied_id) comment.replied_id = replied_comment send_new_reply_email(replied_comment) db.session.add(comment) db.session.commit() if current_user.is_authenticated: flash('评论发布', '成功') else: flash('评论已加入审核队列,审核通过后将显示在评论列表中', '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 fake_comments(count=500): #默认生成500条评论 for i in range(count): comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) salt = int(count * 0.1) #另外再添加50条未审核评论、50条管理员评论、50条回复 for i in range(salt): #未审核评论 comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=False, post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) #管理员发表的评论 comment = Comment(author='Mima Kirigoe', email='*****@*****.**', site='example.com', body=fake.sentence(), timestamp=fake.date_time_this_year(), from_admin=True, 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(), email=fake.email(), site=fake.url(), body=fake.sentence(), timestamp=fake.date_time_this_year(), reviewed=True, #回复就从评论内挑就行 replied=Comment.query.get(random.randint(1, Commen.query.count())), post=Post.query.get(random.randint(1, Post.query.count()))) db.session.add(comment) db.session.commit()
def post(self, **kwargs): comment = Comment() for key, value in kwargs.items(): setattr(comment, key, value) db.session.add(comment) db.session.commit() return comment, 201
def fake_comments(count=500): for i in range(count): comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), reviewed=True, timestamp=fake.date_time_this_year(), post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) salt = int(count * 0.1) for i in range(salt): # 未审核评论 comment = Comment(author=fake.name(), email=fake.email(), site=fake.url(), body=fake.sentence(), reviewed=False, timestamp=fake.date_time_this_year(), post=Post.query.get( random.randint(1, Post.query.count()))) db.session.add(comment) # 管理员发表的评论 comment = Comment(author='fanggaoshang', email='*****@*****.**', site='fanggaoshang.cn', body=fake.sentence(), reviewed=True, 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(), email=fake.email(), site=fake.url(), body=fake.sentence(), reviewed=True, 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()))) db.session.add(comment)
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['BLOG_COMMENT_PER_PAGE'] pagination = Comment.query.with_parent(post).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_user.email from_admin = True read = True else: form = CommentForm() from_admin = False read = 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, read=read) 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() flash('评论发表成功', 'success') if not current_user.is_authenticated: # 访客发表评论,通知管理员 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 fake_comments(count=500): for i in range(count): comment = Comment(author=fake.name(), body=fake.sentence(), timestamp=fake.date_time_this_year(), article=Article.query.get( random.randint(1, Article.query.count()))) db.session.add(comment) salt = int(count * 0.1) for i in range(salt): comment = Comment(author=fake.name(), body=fake.sentence(), timestamp=fake.date_time_this_year(), article=Article.query.get( random.randint(1, Article.query.count()))) db.session.add(comment) db.session.commit()
def setUp(self): super(AdminTestCase, self).setUp() self.login() category = Category(name='Default') post = Post(title='Hello', category=category, body='Blah...') comment = Comment(body='A comment', post=post, from_admin=True) link = Link(name='GitHub', url='https://github.com/greyli') db.session.add_all([category, post, comment, link]) db.session.commit()
def setUp(self): super(BlogTestCase, self).setUp() self.login() category = Category(name='Default') post = Post(title='This is a test', body='A simple test.', category=category) comment = Comment(body='A comment', post=post, from_admin=True, reviewed=True) db.session.add(category) db.session.add(post) db.session.add(comment) db.session.commit()