def comment(): tid = request.args.get('tid') form = ReplyForm() if request.method == 'GET': pid = request.args.get('pid') pn = request.args.get('pn') if not pn: pn = 1 else: pn = int(pn) if pn == 1: comment = Comment.query.filter_by(tid=tid).first() else: comment = None title = Comment.query.filter_by(tid=tid).first().title reply_list = Reply.query.filter_by( tid=tid).order_by('reply.rid').paginate(pn, MAX_REPLY_NUM_ONE_PAGE) return render_template('comment.html', pid = pid, tid = tid, title = title, comment = comment, pn = pn, \ reply_list = reply_list, form = form) else: reply = Reply(tid = tid, userid = current_user.userid, nickname = current_user.nickname, \ content = form.content.data, post_time = get_now_time()) comment = Comment.query.filter_by(tid=tid).first() comment.re += 1 db.session.add(reply) db.session.commit() return redirect(request.referrer)
def get(self, topic_id): topic_id = int(topic_id) page = force_int(self.get_argument('page', 0), 0) topic = Topic.get(id=topic_id) if not topic: raise tornado.web.HTTPError(404) action = self.get_argument('action', None) category = self.get_argument('category', None) user = self.current_user if action and user: if action == 'up': if topic.user_id != user.id: result = user.up(topic_id=topic_id) else: result = {'status': 'info', 'message': '不能为自己的主题投票'} if action == 'down': if topic.user_id != user.id: result = user.down(topic_id=topic_id) else: result = {'status': 'info', 'message': '不能为自己的主题投票'} if action == 'collect': result = user.collect(topic_id=topic_id) if action == 'thank': result = user.thank(topic_id=topic_id) if action == 'report': result = user.report(topic_id=topic_id) if self.is_ajax: return self.write(result) else: self.flash_message(result) return self.redirect_next_url() if not category: category = 'all' if category == 'all': reply_count = topic.reply_count url = topic.url elif category == 'hot': reply_count = count(topic.get_replies(page=None, category=category)) url = topic.url + '?category=hot' page_count = (reply_count + config.reply_paged - 1) // config.reply_paged if page == 0: page = page_count replies = topic.get_replies(page=page, category=category) form = ReplyForm() return self.render("topic/index.html", topic=topic, replies=replies, form=form, category=category, page=page, page_count=page_count, url=url)
def post(request, post_id): comment_form = CommentForm() reply_form = ReplyForm() try: post = Post.objects.get(id=post_id) tags = Tag.objects.filter(tag_posts__id=post_id) categories = Category.objects.all() if post: comments = Comment.objects.filter(comment_post__id=post_id) reg_form = UserRegForm() likes = len(Like.objects.filter(like_post__id=post_id)) dislikes = len(Dislike.objects.filter(dislike_post__id=post_id)) forbidden_words = Forbidden.objects.all() comments_replies = [] if comments: for comment in comments: ## check comments for forbidden words for forbidden_word in forbidden_words: comment.comment_text = comment.comment_text.replace( forbidden_word.word, ("*" * len(forbidden_word.word))) replies = Reply.objects.filter( reply_comment__id=comment.id) comments_replies.append(replies) if replies: for reply in replies: ## check replies for forbidden words for forbidden_word in forbidden_words: reply.reply_text = reply.reply_text.replace( forbidden_word.word, ("*" * len(forbidden_word.word))) context = { 'post': post, 'categories': categories, 'comments': comments, 'replies': comments_replies, 'comment_form': comment_form, 'reply_form': reply_form, 'likes': likes, 'dislikes': dislikes, 'tags': tags, } except Post.DoesNotExist: raise Http404("Post does not exist") return render(request, 'post.html', context)
def post(self, reply_id): reply = Reply.get(id=reply_id) if not reply or (reply.author != self.current_user and not self.current_user.is_admin): return self.redirect_next_url() user = self.current_user form = ReplyForm(self.request.arguments) if form.validate(): reply = form.save(user=user, topic=reply.topic, reply=reply) reply.put_notifier() result = {'status': 'success', 'message': '评论修改成功', 'reply_url': reply.url} return self.send_result(result, reply.url) data = dict(form=form, reply=reply) return self.send_result_and_render(form.result, "reply/edit.html", data)
def post(self): page = int(self.get_argument('page', 1)) category = self.get_argument('category', 'index') topic_id = int(self.get_argument('topic_id', 0)) topic = Topic.get(id=topic_id) if not topic_id: result = {'status': 'error', 'message': '无此主题,不能创建评论'} if self.is_ajax: return self.write(result) else: self.flash_message(**result) return self.redirect_next_url() user = self.current_user form = ReplyForm(self.request.arguments) if form.validate(): reply = form.save(user=user, topic=topic) reply.put_notifier() result = {'status': 'success', 'message': '评论创建成功', 'content': reply.content, 'name': reply.author.name, 'nickname': reply.author.nickname, 'author_avatar': reply.author.get_avatar(size=48), 'author_url': reply.author.url, 'author_name': reply.author.name, 'author_nickname': reply.author.nickname, 'reply_url': reply.url, 'created': reply.created, 'id': reply.id, 'floor': reply.floor} if self.is_ajax: return self.write(result) self.flash_message(**result) return self.redirect(topic.url) reply_count = topic.reply_count page_count = (reply_count + config.reply_paged - 1) // config.reply_paged replies = topic.get_replies(page=page, category=category) data = dict(form=form, topic=topic, replies=replies, category=category, page=page, page_count=page_count, url=topic.url) return self.send_result_and_render(form.result, "topic/index.html", data)