def post(id): post = Post.query.get_or_404(id) page = request.args.get('page', 1, type=int) form = CommentForm() if form.validate_on_submit(): comment = Post( post_type=PostType.COMMENT, body=form.body.data, parent_post=post, author=current_user._get_current_object(), disabled=not current_user.can(Permission.MODERATE_COMMENTS)) db.session.add(comment) db.session.commit() return redirect(url_for('.post', id=post.id, page=-1)) if page == -1: page = (post.comments.count() - 1) // 10 + 1 pagination = post.comments.order_by(Post.timestamp.asc()).paginate( page, per_page=10, error_out=False) comments = pagination.items return render_template('post.html', posts=[post], page=page, form=form, comments=comments, pagination=pagination)
def favorite_comments(username, id): s = search() if s: return s user = User.query.filter_by(username=username).first() favorite = Favorite.query.get_or_404(id) form = CommentForm() if form.validate_on_submit(): comment = Comment.create(author=current_user._get_current_object(), body=form.body.data, favorite=favorite, topic_type='favorite') db.session.add(comment) db.session.commit() flash('评论添加成功') return redirect( url_for('.favorite_comments', username=user.username, id=favorite.id)) page = request.args.get('page', 1, type=int) pagination = favorite.comments.order_by(Comment.timestamp.desc()).paginate( page, per_page=current_app.config['ZHIDAO_COMMENT_PER_PAGE'], error_out=False) comments = pagination.items context = dict(form=form, pagination=pagination, comments=comments, user=user, favorite=favorite) return render_template('favorite/favorite_comments.html', **context)
def post(id): post = Post.query.get_or_404(id) form = CommentForm() if form.validate_on_submit(): comment = Comment(body=form.body.data, post=post, author=current_user._get_current_object()) db.session.add(comment) db.session.commit() flash('评论成功') return redirect(url_for('main.post', id=post.id)) page = request.args.get('page', 1, type=int) comments = post.comments.order_by(Comment.timestamp.desc()).paginate( page, current_app.config['POSTS_PER_PAGE'], False) print(comments.items) for item in comments.items: print(item) next_url = url_for('post.explore', page=comments.next_num) if comments.has_next else None prev_url = url_for('post.explore', page=comments.prev_num) if comments.has_prev else None return render_template('post.html', post=post, form=form, comments=comments.items, next_url=next_url, prev_url=prev_url)
def detail(slug): post = Post.query.filter_by(slug=slug).first_or_404() form1 = CommentForm() if form1.validate_on_submit(): comm = Comment(body=form1.body.data, post=post, author=current_user._get_current_object()) db.session.add(comm) db.session.commit() flash('Your comment has been published') return redirect(url_for('main.detail', slug=post.slug, page=-1)) page = request.args.get('page', 1, type=int) if page == -1: page = (post.comments.count() - 1) // current_app.config['COMMENTS_PER_PAGE'] + 1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( page, current_app.config['COMMENTS_PER_PAGE'], False) comments = pagination.items next_url = url_for( 'main.detail', slug=slug, page=pagination.next_num) if pagination.has_next else None prev_url = url_for( 'main.detail', slug=slug, page=pagination.prev_num) if pagination.has_prev else None return render_template('detail.html', post=post, form1=form1, comments=comments, pagination=pagination, next_url=next_url, prev_url=prev_url)
def post(id): """文章详情页""" s = search() if s: return s post = Post.query.get_or_404(id) form = CommentForm() if post.author != current_user: post.browsed() if form.validate_on_submit(): comment = Comment.create(author=current_user._get_current_object(), post=post, body=form.body.data, topic_type='post') db.session.add(comment) db.session.commit() flash('评论添加成功', 'success') return redirect(url_for('.post', id=post.id)) page = request.args.get('page', 1, type=int) pagination = post.comments.filter_by(topic_type='post'). \ order_by(Comment.timestamp.desc()).paginate(page, per_page=current_app.config['ZHIDAO_COMMENT_PER_PAGE'], error_out=False) comments = pagination.items if post.disable_comment: return render_template('post/post.html', post=post) context = dict(form=form, pagination=pagination, comments=comments, post=post) return render_template('post/post.html', **context)
def post(post_id, post_url): post = Post.query.filter_by(id=post_id).first() tags = Tag.query.filter_by(id=post_id) form = CommentForm() comments = Comment.query.filter_by(post_id=post_id).order_by( Comment.date.desc()) tags = Tag.query.filter_by(post_id=post_id) if form.validate_on_submit(): if current_user.is_authenticated: comment = Comment(content=form.content.data, post_id=post_id, author_id=current_user.id) db.session.add(comment) db.session.commit() flash('Your comment has been published') return redirect( url_for('main.post', post_id=post_id, post_url=post_url)) else: flash('You need to get logged in to comment') return render_template('main/post.html', post=post, form=form, comments=comments, tags=tags, post_id=post_id, post_url=post_url)
def book(id): book = Book.query.get_or_404(id) current_rating = Rating.query.filter_by(author=current_user, book=book).first() form = CommentForm() if form.validate_on_submit(): language = guess_language(form.body.data) if language == 'UNKNOWN' or len(language) > 5: language = '' comment = Comment(body=form.body.data, book=book, author=current_user._get_current_object(), language=language) comment.save() flash('Your comment has been published.') return redirect(url_for('main.book', id=book.id, page=1)) page = request.args.get('page', 1, type=int) comments = book.comments.order_by(Comment.time.desc()).paginate( page, current_app.config['POSTS_PER_PAGE'], False) comments_count = book.comments.count() next_url = url_for('main.book', id=book.id, page=comments.next_num) if comments.has_next else None prev_url = url_for('main.book', id=book.id, page=comments.prev_num) if comments.has_prev else None return render_template('book.html', title=_('book'), comments_count=comments_count, books=[book], form=form, comments=comments.items, prev_url=prev_url, next_url=next_url)
def comment(id): comment = Comment.query.get_or_404(id) parents = comment.get_parents(comment) parent_book = Book.query.filter_by(id=parents[0].book_id) form = CommentForm() if form.validate_on_submit(): language = guess_language(form.body.data) if language == 'UNKNOWN' or len(language) > 5: language = '' comment_reply = Comment(body=form.body.data, parent=comment, author=current_user._get_current_object(), language=language) comment_reply.save() flash('Your comment has been published.') return redirect(url_for('main.comment', id=comment.id, page=1)) page = request.args.get('page', 1, type=int) comments = Comment.query.filter_by(id=comment.id).first().replies.paginate( page, current_app.config['POSTS_PER_PAGE'], False) comments_count = Comment.query.filter_by( id=comment.id).first().replies.count() next_url = url_for('main.comment', id=comment.id, page=comments.next_num) if comments.has_next else None prev_url = url_for('main.comment', id=comment.id, page=comments.prev_num) if comments.has_prev else None return render_template('comment.html', title=_('comment'), parent_book=parent_book, parents=parents, comments_count=comments_count, comment=[comment], form=form, comments=comments.items, prev_url=prev_url, next_url=next_url)
def post(id): #id为文章id post = Post.query.get_or_404(id) #评论表格实例化 form = CommentForm() #表格提交认证 if form.validate_on_submit(): comment = Comment( body=form.body.data, post=post, #获取真正的评论者 author=current_user._get_current_object()) db.session.add(comment) db.session.commit() flash('Your comment has been published.') #重定向到显示自己评论的页面 return redirect(url_for('.post', id=post.id, page=-1)) #页面赋值 page = request.args.get('page', 1, type=int) #如果page== -1,page进行重新赋值为了显示最后页 if page == -1: page = (post.comments.count() - 1) // \ current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'], error_out=False) comments = pagination.items return render_template('post.html', posts=[post], form=form, comments=comments, pagination=pagination)
def post(id): post = Post.query.get_or_404(id) form = CommentForm() if form.validate_on_submit(): newc = Comment(body=form.body.data, post=post, author=current_user._get_current_object()) db.session.add(newc) db.session.commit() flash('Your comment has been published.') """ page=-1 é usado para requisitar a última página de comentários de mode que o comentário de acabou de ser inserido seja visto na última página. Os comentários novos ficam na utlimta pagina """ return redirect(url_for('.post', id=post.id, page=-1)) page = request.args.get('page', 1, type=int) if page == -1: page = (post.comments.count() - 1) // current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'], error_out=False) comments = pagination.items return render_template('post.html', posts=[post], form=form, comments=comments, pagination=pagination)
def blog(id): """ 文章详情 """ blog = Blog.query.get_or_404(id) blog.views = int(blog.views) + 1 db.session.add(blog) db.session.commit() form = CommentForm() if form.submit.data: # 没有评论权限 if not current_user.can_comment(): abort(403) if form.validate_on_submit(): comment = Comment(content=form.content.data, blog=blog, user=current_user._get_current_object()) db.session.add(comment) db.session.commit() flash('评论成功') return redirect(url_for('main.blog', id=id)) else: flash('评论内容不能为空') page = request.args.get('page', 1, type=int) pagination = blog.comments.order_by(Comment.timestamp.desc()).paginate( page=page, per_page=current_app.config['PER_PAGE_5'], error_out=False) comments = pagination.items return render_template('blog.html', blog=blog, page=page, comments=comments, pagination=pagination, form=form, next=request.url)
def post_page(post_id, subdomain='www'): post = Post.query.filter_by(company_id=current_user.company_id, id=post_id).first_or_404() comments = ( Comment.query.filter_by(company_id=current_user.company_id, post_id=post.id) # .order_by(Comment.thread_timestamp.desc(), Comment.path.asc()) .order_by(Comment.thread_score.desc(), Comment.path.asc()).all()) form = CommentForm() if form.validate_on_submit(): if current_user.is_authenticated: if current_user.can_comment(): comment = Comment( text=form.text.data, author=current_user, post_id=post.id, timestamp=datetime.utcnow(), thread_timestamp=datetime.utcnow(), company_id=current_user.company_id, ) comment.save() return redirect( url_for("main.post_page", subdomain=subdomain, post_id=post.id)) else: flash( f"You can only commment {current_app.config['USER_COMMENTS_PER_DAY']} times a day." ) else: return redirect(url_for("auth.login", subdomain=subdomain)) return [post, form, comments]
def show_post(id): post = Post.query.filter_by(id=id).first_or_404() form = CommentForm() if form.validate_on_submit(): comment = Comment(body=form.body.data, author=current_user) comment.post = post db.session.add(comment) db.session.commit() flash('Your comment is now live!') return redirect(url_for('main.show_post', id=id)) page = request.args.get('page', 1, type=int) comments = Comment.query.filter_by(post_id=id).order_by(Comment.created_at.desc())\ .paginate(page, current_app.config['COMMENTS_PER_PAGE'], False) next_url = url_for('main.show_post', id=id, page=comments.next_num) if comments.has_next else None prev_url = url_for('main.show_chat', id=id, page=comments.prev_num) if comments.has_prev else None return render_template('post.html', title=post.title, post=post, comments=comments.items, next_url=next_url, prev_url=prev_url, form=form)
def post(id): post = Post.query.get_or_404(id) form = CommentForm() if form.validate_on_submit(): comment = Comment(body=form.body.data, post=post, author=current_user._get_current_object()) db.session.add(comment) db.session.commit() flash('Your comment has been published.') return redirect(url_for('.post', id=post.id, page=-1)) page = request.args.get('page', 1, type=int) if page == -1: page = (post.comments.count() - 1) // \ current_app.config['COMMENTS_PER_PAGE'] + 1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate( page, per_page=current_app.config['COMMENTS_PER_PAGE'], error_out=False) comments = pagination.items return render_template('post.html', posts=[post], form=form, comments=comments, pagination=pagination)
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).order_by(Comment.timestamp.desc()).paginate(page, per_page) comments = pagination.items form = CommentForm() if current_user.is_authenticated: form.author.data = current_user.username form.email.data = current_user.email 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, 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) print('---comment-------',comment.body) db.session.commit() if current_user.is_authenticated: flash('评论已经发表', 'success') print(post_id) 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 ebook(id): ebook = Ebook.query.get_or_404(id) download_users = ebook.users_download.limit(10).all() form = CommentForm() if form.validate_on_submit(): comment = Comment(body=form.body.data, ebook=ebook, author=current_user._get_current_object()) db.session.add(comment) # flash('评论信息成功创建') return redirect(url_for('main.ebook', id=ebook.id, page=1)) page = request.args.get('page', 1, type=int) if page == -1: page = (ebook.comments.count() - 1) / current_app.config['FLASK_BMS_MAX_PER_PAGE'] + 1 pagination = ebook.comments.order_by(Comment.timestamp.desc()).paginate( page, per_page=current_app.config['FLASK_BMS_MAX_PER_PAGE'], error_out=False) tags = [] for i in ebook.tags: tags.append({'name': i.name, 'className': 'info'}) comments = pagination.items can_write_comment = current_user.can(Permission.WRITE_COMMENT) return custom_render_template('book/ebook.html', pagination=pagination, url_point='main.ebook', can_write_comment=can_write_comment, book=ebook, download_users=download_users, Permission=Permission, tags=tags, form=form, comments=comments)
def post(post_id): '''view func for post page''' # From obj: 'Comment' form = CommentForm() # form.validate_on_submit() will be true and return the data obj # to form instance from user enter, when HTTP request is POST 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 = 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 add_comment(postid): form = CommentForm() if form.validate_on_submit(): body = form.body.data comment = Comment(body=body, post_id=postid, author_id=current_user.id) db.session.add(comment) db.session.commit() return jsonify({'message': '{}'.format(form.body.data)}) return jsonify({'message': 'An error occurred.'})
def post_detail(post_id): post = Post.query.filter_by(id=post_id).first() comments = Comment.query.filter_by(post_id=post.id).all() form = CommentForm() if form.validate_on_submit(): comment = Comment(body=form.comment.data, author=current_user, post=post) db.session.add(comment) db.session.commit() flash(_('Your comment has been sent.')) return redirect(url_for('main.post_detail', post_id=post.id)) return render_template('post_detail.html', title=_('Post'), post=post, comments=comments, form=form)
def comment(id): posts = Post.query.filter_by(id=id).first() form = CommentForm() if form.validate_on_submit(): comment = Comment( comment_body=comment_body.form.data, posts=posts, author=current_user) db.session.add(comment) db.session.commit() flash('your comment has been added') return redirect(url_for('main.index', id=post.id )) return render_template('index.html', title='Comments', form=form)
def article(id): form=CommentForm() p = Post.query.get_or_404(id) if form.validate_on_submit(): comment = Comment(body=form.body.data, post=p, author=current_user._get_current_object()) db.session.add(comment) db.session.commit() flash('Your comment has been published.') return redirect(url_for('main.article',id=id)) return render_template('post.html',p=p,id=id,form=form)
def post(id): form = CommentForm() if form.validate_on_submit(): commentService.add(form.text.data, id, current_user.id) flash('Comment Added', 'success') post = postService.get(id) return render_template( 'post.html', post=post, form=form, comment_per_page=current_app.config['COMMENT_PER_PAGE'])
def reply(comment_id): parent = Comment.query.filter_by(id=comment_id).first_or_404() form = CommentForm() if form.validate_on_submit(): comment = Comment( text=form.text.data, author=current_user, post_id=parent.post_id, parent_id=parent.id, timestamp=datetime.utcnow(), thread_timestamp=parent.thread_timestamp, ) comment.save() return redirect(url_for("main.post_page", post_id=parent.post_id)) return render_template("reply.html", comment=parent, form=form)
def fullpost(id): title= f'Posts' post = Post.query.filter_by(id=id).first() comment = CommentForm() if comment.validate_on_submit(): comment = Comment(comment_body = comment.comment_body.data, post_id=id) db.session.add(comment) db.session.commit() print(comment) return redirect(url_for('main.fullpost', id=post.id)) allcomments = Comment.query.all() postcomments = Comment.query.filter_by(post_id=id).all() return render_template('fullpost.html', title=title, post=post, comment=comment, allcomments=allcomments ,postcomments=postcomments)
def post(slug): post = Post.query.filter_by(slug=slug).first_or_404() comment_form = CommentForm() if comment_form.validate_on_submit(): special = True if current_user.is_authenticated else False comment = Comment(name=comment_form.name.data, body_text=comment_form.body_text.data, special=special) session["name"] = comment.name post.comments.append(comment) return redirect(url_for("main.post", slug=slug, _anchor="write")) comment_form.name.data = session.get("name", "") comments = post.comments.order_by(Comment.timestamp.asc()).all() return render_template("main/post.html", post=post, date_format=date_format, comments=comments, comment_form=comment_form)
def create_comment(post_id): post = Post.query.filter_by(id=post_id).first_or_404() form = CommentForm() comments = Comment.query.filter_by(post_id=post_id).all() if form.validate_on_submit(): comment = Comment(body=form.body.data, commenter=current_user, comment_post=post) db.session.add(comment) db.session.commit() return redirect(url_for('main.create_comment', post_id=post_id)) return render_template('main/comment_section.html', post=post, form=form, comments=comments)
def comment(): paper = Paper.query.get(request.args.get('id')) form = CommentForm() if form.validate_on_submit(): comment = "\n" + current_user.firstname + ": " + form.comment.data if paper.comment: paper.comment = paper.comment + comment else: paper.comment = comment db.session.commit() return redirect(url_for('main.submit')) return render_template('main/comment.html', form=form, paper=paper, title='Comment')
def post(post_id): post = Post.query.get_or_404(post_id) form = CommentForm() if form.validate_on_submit(): comment = form.comment.data new_comment = Comments(comment=comment, post_id=post_id, user=current_user) new_comment.save_comments() return redirect(url_for('main.post', post_id=post.id)) comments = Comments.query.filter_by(post_id=post_id).all() return render_template('post.html', title=post.title, post=post, comment_form=form, comments=comments)
def post(id: int): post = Post.query.get_or_404(id) page = request.args.get('page', 1, type=int) pagination = post.comments.order_by(Comment.timestamp.desc()).paginate(page, per_page=current_app. config['FLASK_PER_PAGE'] or 10, error_out=False) comments = pagination.items form = CommentForm() if form.validate_on_submit(): post_id = id comment = Comment(post_id=post_id, user_id=current_user.id, content=form.content.data) db.session.add(comment) db.session.commit() return redirect(url_for('main.post', id=post_id)) return render_template('post.html', post=post, form=form, pagination=pagination, comments=comments)
def reply(comment_id): parent = Comment.query.filter_by(id=comment_id).first_or_404() form = CommentForm() if form.validate_on_submit(): comment = Comment( text=form.text.data, author=current_user, post_id=parent.post_id, parent_id=parent.id, timestamp=datetime.utcnow(), thread_timestamp=parent.thread_timestamp, company_id=current_user.company_id, ) comment.save() return parent.post_id return [parent, 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['COMMENTS_PER_PAGE'] comments = Comment.query.with_parent(post).filter_by(is_hidden=False).order_by(Comment.timestamp.asc()).paginate( page, per_page) if current_user.is_authenticated: form = UserCommentForm() form.author.data = current_user.username form.email.data = current_user.email form.site.data = url_for('main.index') from_post_author = True if post.author == current_user else False else: form = CommentForm() from_post_author = 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_post_author=from_post_author, 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('Comment published.') return redirect(url_for('main.show_post', post_id=post_id)) return render_template('post.html', post=post, pagination=comments, form=form, comments=comments.items)
def comment(id): post = Post.query.filter_by(id=id).first_or_404() form = CommentForm() if form.validate_on_submit(): comment = Comment(comment=form.comment.data, author=current_user._get_current_object(), post=post) db.session.add(comment) return redirect(url_for('.post', post=post, form=form)) comments = Comment.query.all() return render_template('post.html', post=post, form=form, comments=comments) return '评论列表'
def post(id): post = Post.query.get_or_404(id) form = CommentForm() if form.validate_on_submit(): comment = Comment(body=form.body.data, post=post, author=current_user._get_current_object()) db.session.add(comment) flash('Your comment has been published') return redirect(url_for('.post', id=post.id, page=-1)) page = request.args.get('page', 1, type=int) if page == -1: page = (post.comments.count() - 1) / current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1 pagination = post.comments.order_by(Comment.timestamp.asc()).paginate(page, per_page=current_app.config[ 'FLASKY_COMMENTS_PER_PAGE'], error_out=False) comments = pagination.items return render_template('post.html', posts=[post], form=form, comments=comments, pagination=pagination)