def api_comments(*, page='1'): page_index = get_page_index(page) num = yield from Comment.findNumber('count(id)') p = Page(num, page_index) if num == 0: return dict(page=p, comments=()) comments = yield from Comment.findAll(orderBy='created_at desc', limit=(p.offset, p.limit)) return dict(page=p, comments=comments)
def new_comment(article_id): post_data = flask.request.form comment = Comment(name=post_data['name'], text=post_data['text']) if post_data['location'] is not None: comment.location = post_data['location'] article = Article.query.get(article_id) article.comments.append(comment) article.content = post_data['content'] db.session.commit() return flask.jsonify(content=article.content, comments=[c.serialize for c in article.comments.all()])
def new_comment(article_id): post_data = flask.request.form comment = Comment(name=post_data['name'], text=post_data['text']) if post_data['location'] is not None: comment.location = post_data['location'] article = Article.query.get(article_id) article.comments.append(comment) article.content = post_data['content'] db.session.commit() return flask.jsonify( content=article.content, comments=[c.serialize for c in article.comments.all()])
def api_delete_comments(id, request): check_admin(request) c = yield from Comment.find(id) if c is None: raise APIResourceNotFoundError('Comment') yield from c.remove() return dict(id=id)
def api_create_comment(id, request, *, content): user = request.__user__ if user is None: raise APIPermissionError('Please signin first.') if not content or not content.strip(): raise APIValueError('content') blog = yield from Blog.find(id) if blog is None: raise APIResourceNotFoundError('Blog') comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) yield from comment.save() return comment
def get_blog(id): blog = yield from Blog.find(id) comments = yield from Comment.findAll('blog_id=?', [id], orderBy='created_at desc') for c in comments: c.html_content = text2html(c.content) blog.html_content = markdown2.markdown(blog.content) return {'__template__': 'blog.html', 'blog': blog, 'comments': comments}
def post(self): commentbody = self.get_argument("commentbody", None) share_id = self.get_argument("share_id", None) html = markdown2.markdown(commentbody) Comment.create( user_id=self.current_user["user_id"], share_id=share_id, commentbody=commentbody) Share.update( commentnum=F('commentnum') + 1).where(id=share_id).execute() name = tornado.escape.xhtml_escape(self.current_user["user_name"]) gravatar = get_avatar(self.current_user["user_email"], 50) newcomment = '' newcomment += ' <div class="comment">' newcomment += '<div class="avatar">' newcomment += '<img src="' + gravatar + '" />' newcomment += '</div>' newcomment += '<div class="name">' + name + '</div>' newcomment += '<div class="date" title="at"></div>' newcomment += html newcomment += '</div>' self.write(newcomment)
def get(self): realpath = self.request.path[1:] try: share = Share.get(slug=realpath) except: self.redirect("/404") share.markdown = markdown.markdown(share.markdown) if self.current_user: share.is_liking = Like.select().where( share_id=share.id, user_id=self.current_user["user_id"]).count() > 0 comments = Comment.select().where(share_id=share.id) for comment in comments: user = User.get(id=comment.user_id) comment.name = user.user_name comment.domain = user.user_domain comment.gravatar = get_avatar(user.user_email, 50) Share.update(hitnum=F('hitnum') + 1).where( id=share.id).execute() if self.current_user: is_hitted = Hit.select().where( share_id=share.id, user_id=self.current_user["user_id"]).count() > 0 Hit.create( hitnum=1, share_id=share.id, user_id=self.current_user["user_id"], ) else: is_hitted = self.get_cookie(share.id) if not is_hitted: self.set_cookie(str(share.id), "1") posts = Share.select() suggest = {} for post in posts: post.score = 100 + post.id - post.user_id + post.commentnum * 3 post.score += post.likenum * 4 + post.hitnum * 0.01 post.score += randint(1, 999) * 0.001 if post.sharetype == share.sharetype: post.score += 5 if self.current_user: is_hitted = Hit.select().where( share_id=post.id, user_id=self.current_user["user_id"]).count() > 0 else: is_hitted = self.get_cookie(share.id) if is_hitted: post.score -= 50 suggest[post.score] = post.id print(post.id) print(post.score) realsuggest = [] i = 1 for key in sorted(suggest.iterkeys(), reverse=True): post = Share.get(id=suggest[key]) share_post = {'id': post.id, 'title': post.title, } realsuggest.append(share_post) i = i + 1 if i > 3: break self.render( "sharee.html", share=share, comments=comments, realsuggest=realsuggest)
async def pull_course_comments(course_id): sq = Comment.select().where(Comment.course_id == course_id) comments = await manager.execute(sq) return comments