def populate_db(): b1 = User(name="Jim") b1.set_password("Marillion") b2 = User(name="Gandalf") b2.set_password("Gandalf") b3 = User(name="Saruman") b3.set_password("Saruman") db.session.add_all([b1, b2, b3]) db.session.commit() e1 = Forum(userID=1, book_title="Hitchhiker's Guide to the Galaxy", author="Douglas Adams") e2 = Forum(userID=2, book_title="The Silmarillion", author="J.R.R. Tolkien") e3 = Forum(userID=2, book_title="The Martian", author="Andy Weir") db.session.add_all([e1, e2, e3]) db.session.commit() p1 = Post(title="I liked this book", content="This book is really funny!", userID=3, forumID=1) p2 = Post(title="Depressing", content="This book had a lot of tragic stories.", userID=2, forumID=2) p3 = Post(title="Cool!", content="A fine read!", userID=1, forumID=2) p4 = Post(title="Weir Science", content="There's a lot of science in this book!", userID=2, forumID=3) db.session.add_all([p1, p2, p3, p4]) db.session.commit() r1 = Reply(userID=2, content="I disagree!", postID=2) r2 = Reply(userID=1, content="I agree!", postID=3) r3 = Reply(userID=3, content="I dunno.", postID=1) r4 = Reply(userID=1, content="huh.", postID=1) db.session.add_all([r1, r2, r3, r4]) db.session.commit() #p1 = PostToReply(postID=1, replyID=1) #p2 = PostToReply(postID=1, replyID=2) #p3 = PostToReply(postID=1, replyID=3) # p4 = PostToReply(postID=1, replyID=4) #db.session.add_all([p1, p2, p3, p4]) #db.session.commit() return render_template('base.html', title='Populate DB')
def test_question_comment(self): """ 测试评论添加删除 以及回复添加删除 :return: """ u = User.create(username='******', password='******') u1 = User.create(username='******', password='******') question = Question.create(author=u, description='dwdwdgg?') self.assertTrue(question.undelete_comments.count() == 0) self.assertTrue(question.comments.count() == 0) c = Comment.create(author=u1, question=question, topic_type='question', body='fdjhfjdj') c1 = Comment.create(author=u1, question=question, topic_type='question', body='lgkjgfdjhfjdj') self.assertTrue(question.comments.count() == 2) self.assertTrue(question.undelete_comments.count() == 2) r = Reply.create(comment=c, author=u, body='kllfdd') r1 = Reply.create(comment=c, author=u1, user=u, body='ldkkkfg') self.assertTrue(c.replies.count() == 2) u.delete_comment(c) # 先删除评论 self.assertTrue(c.replies.count() == 2) # 不会影响回复 self.assertTrue(question.comments.count() == 2) self.assertTrue(question.undelete_comments.count() == 1) u.delete_reply(r) # 删除一条回复 self.assertTrue(c.replies.count() == 1) u.delete_reply(r1) #删除最后一条回复 self.assertNotIn(c, question.comments.all()) #c不应该在所有评论里面
def get_blog(id): blog = Blog.query.filter_by(id=id).first_or_404() blog.read_count += 1 blog.save() form = ReplyForm() if form.validate_on_submit(): reply = Reply(body=form.body.data, username=form.username.data, email=form.email.data, avatar = randint(1, 5), blog=blog) reply.save() return render_template('blog.html', blog=blog, form=form)
def topic(postid): allUsers = User.query.all() myPost = Post.query.get(postid) if myPost is None: return render_template('404.html'), 404 rForm = ReplyForm() # bcForm = BlessCurseForm() if rForm.rSubmit.data and rForm.validate(): reply = Reply(post_id=postid, text=rForm.text.data, stance=('True' == rForm.stance.data), user_id=current_user.id) db.session.add(reply) db.session.commit() myReply = Post.query.get(postid).p_replies.all() return render_template('topicpage.html', title='Topic', post=myPost, replies=myReply, form=rForm, user=allUsers) # if bcForm.bcSubmit.data and bcForm.validate(): # blessed = ('bless' == bcForm.choice.data) # thePost = myPost # post_bc_query = thePost.user_votes.filter_by(user_id=current_user.id).first() # if post_bc_query == None: # post_bc = Post_BC(post_id=thePost.id, user_id=current_user.id, stance=blessed) # if blessed == True: # thePost.blesses = thePost.blesses + 1 # else: # thePost.curses = thePost.curses + 1 # db.session.add(post_bc) # db.session.commit() # elif post_bc_query.stance != blessed: # if blessed == True: # thePost.blesses += 1 # thePost.curses -= 1 # else: # thePost.curses += 1 # thePost.blesses -= 1 # post_bc_query.stance = blessed # db.session.commit() # # myReply = myPost.p_replies.all() # return render_template('topicpage.html', title='Topic', post=myPost, # replies=myReply, form=rForm, bcForm=bcForm, user=allUsers) myReply = myPost.p_replies.all() return render_template('topicpage.html', title='Topic', post=myPost, replies=myReply, form=rForm, user=allUsers)
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 reply_generator(count=1000): from app.models import UserInfo,Comment,Reply seed() u_count = UserInfo.query.count() c_count = Comment.query.count() for i in range(count): comment=Comment.query.offset(randint(0,c_count-1)).first() r=Reply(author=UserInfo.query.offset(randint(0,u_count-1)).first(), comment=comment, receiver=comment.author, content=forgery_py.lorem_ipsum.sentence(), created_time=forgery_py.date.date(True),) db.session.add(r) try: db.session.commit() except Exception: db.session.rollback()
def reply(request, pid): if request.method == 'POST': content = request.POST.get('content') username = request.session.get('username') user = User.objects.filter(username=username)[0] pone = Post.objects.filter(id=pid)[0] # pid = pid # data = { # 'content':content, # 'user':user, # 'pone':pone, # 'pid':pid, # } reply = Reply.create(content=content, post=pone, user=user) reply.save() return redirect('/postdetail/{}/'.format(pid))
def execute_reply(comment, form, user, topic_type, **kwargs): if comment.topic_type == topic_type and form.validate_on_submit(): r = Reply.create(author=current_user._get_current_object(), body=form.body.data, user=user) comment.add_reply(r) if topic_type == 'post': return redirect( url_for('main.{}'.format(topic_type), id=getattr(comment, topic_type).id)) return redirect( url_for('main.{}_comments'.format(topic_type), id=getattr(comment, topic_type).id)) replies = kwargs.get('replies') context = dict(form=form, user=user, replies=replies, comment=comment) return render_template('comment/reply.html', **context)
def reply(book_title, title): post = Post.query.filter_by(title=title).first() book = Forum.query.filter_by(book_title=book_title).first() form = ReplyForm() if form.validate_on_submit(): reply = Reply(userID=current_user.id, postID=post.id, content=form.content.data) db.session.add(reply) db.session.commit() #p2r = PostToReply(postID=post.id, replyID=reply.id) #db.session.add(p2r) #db.session.commit() flash('Reply posted') final_form = ReplyForm() render_template('reply.html', title='Reply', book=book, post=post, form=final_form) #session['book'] = Forum.query.filter_by(book_title=book_title).first() return redirect(url_for('forum', book_title=book_title)) return render_template('reply.html',title='Reply', book=book, post=post, form=form)
def execute_comment(form, comment, topic_type): if comment.topic_type == topic_type and form.validate_on_submit(): r = Reply.create(author=current_user._get_current_object(), body=form.body.data) comment.add_reply(r) if topic_type == 'post': return redirect( url_for('main.post', id=getattr(comment, topic_type).id)) elif topic_type == 'favorite': username = comment.favorite.user.username return redirect( url_for('main.favorite_comments', username=username, id=getattr(comment, topic_type).id)) return redirect( url_for('main.{}_comments'.format(topic_type), id=getattr(comment, topic_type).id)) item = getattr(comment, topic_type) context = dict(form=form, item=item, comment=comment) return render_template('comment/add_comment.html', **context)
def new_reply(name, title): community = Community.query.filter_by(name=name).first() if community: post = Post.query.filter_by(title=title).first() if post: form = NewReplyForm() if form.validate_on_submit(): reply = Reply(body=form.body.data, author=current_user, post=post) db.session.add(reply) db.session.commit() flash('Commented on post', 'primary') return redirect( url_for('post.get_post', name=community.name, title=post.title)) return render_template('reply/new_reply.html', form=form) else: abort(404) else: abort(404)
def reply(): if request.method != 'POST': return redirect(url_for('main.index')) else: article_id = int(request.form.get('article_id')) comment_id = int(request.form.get('comment_id')) receiver_id = int(request.form.get('receiver_id')) ReplyBody = request.form.get('ReplyBody') c = Comment.query.get(comment_id) if not c: info = ''' 你所恢复的评论并不存在,请刷新后再试。 ''' return render_template('blog/404.html', info=info) else: reply = Reply(comment_id=comment_id, author_id=current_user.id, receiver_id=receiver_id, content=ReplyBody) try: db.session.add(reply) except Exception: db.session.commit() return redirect(url_for('art.article', article_id=article_id))
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)