def detail(index): u = current_user() t = Topic.objects(id=ObjectId(index)).first() ts = Topic.objects(belong=t.id) form = ReplyForm() if form.validate_on_submit(): Topic.make_reply(t, content=form.content.data, username=u.username) return redirect(url_for('topic.detail', index=index)) return render_template('detail.html', topic=t, user=u, form=form, r_topics=ts)
def post_page(pid): form = ReplyForm() if form.validate_on_submit(): body = form.reply.data reply = Reply(body=body, pid=pid, user_id=current_user.id) db.session.add(reply) db.session.commit() redirect(url_for('post_page', pid=pid)) post = Post.query.filter_by(id=pid).first_or_404() replys = Reply.query.filter_by(pid=pid) return render_template("post.html", title=post.title, post=post, replys=replys, form=form)
def replyblog(): form = ReplyForm() if form.validate_on_submit(): if current_user.is_authenticated: # 获取当前登录用户对象 user = current_user._get_current_object() # 创建对象 p = Posts(content=form.content.data, user=user) # 保存到数据库 db.session.add(p) flash('发表成功') return redirect(url_for('main.index')) else: flash('请登录后再发表') return redirect(url_for('user.login')) flash('回复成功') return render_template('main/replyblog.html', form=form)
def detail(id): form = ReplyForm() page = request.args.get('page', 1, type=int) post = Post.query.get(id) allpost = Post.query.filter_by(rid=id).order_by(Post.timestamp.desc()) pagination = allpost.paginate(page, per_page=3, error_out=False) posts = pagination.items if current_user.is_authenticated: if form.validate_on_submit(): user = current_user._get_current_object() p = Post(content=form.content.data, rid=id, user=user) db.session.add(p) flash('帖子回复成功') return redirect(url_for('post.detail', id=id)) return render_template('post/detail.html', pagination=pagination, post=post, id=id, form=form, posts=posts) else: flash('登陆后才能回复帖子') return redirect(url_for('user.login'))
def post(id): post = Post.query.filter_by(id=id).first() post.increment_views() db.session.commit() likeForm = LikeForm() commentForm = CommentForm() replyForm = ReplyForm() if commentForm.validate_on_submit(): comment = Comment(body=commentForm.comment.data, post=post, author=current_user) post.decrement_views() db.session.add(comment) post.increment_comments_counter() db.session.commit() flash('Your comment is now live!') commentForm.comment.data = '' return render_template("post.html", title='Post', post=post, likeForm=likeForm, commentForm=commentForm, replyForm=replyForm, scrollToAnchor=comment.id) elif replyForm.validate_on_submit(): comment = Comment.query.filter_by(id=replyForm.commentId.data).first() reply = Reply(body=replyForm.reply.data, comment=comment, author=current_user, post=post) post.decrement_views() db.session.add(reply) post.increment_comments_counter() db.session.commit() flash('Your reply is now live!') replyForm.reply.data = '' return render_template("post.html", title='Post', post=post, likeForm=likeForm, commentForm=commentForm, replyForm=replyForm, scrollToAnchor=reply.id) elif likeForm.validate_on_submit(): liked = post.liked_by.filter_by(user_id=current_user.id).first() if not liked: like = Like(author=current_user, post=post) post.increment_likes() post.decrement_views() db.session.commit() likeForm.like.data = '' flash('Your like is now live!') else: post.liked_by.remove(liked) post.decrement_likes() post.decrement_views() db.session.commit() likeForm.like.data = '' flash('Your unlike is now live!') print(post.liked_by.filter_by(user_id=current_user.id).first()) return render_template("post.html", title='Post', post=post, likeForm=likeForm, commentForm=commentForm, replyForm=replyForm, scrollToAnchor='likePost') return render_template("post.html", title='Post', post=post, likeForm=likeForm, commentForm=commentForm, replyForm=replyForm)