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 api_create_comment(id, request, *, content): user = request.__user__ if user is None: raise APIPermissionError('Please singin 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 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 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 }
async def api_create_comments(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 = await Blog.find(id) if blog is None: raise APIValueError('Blog') comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) await comment.save() return comment