def api_delete_blog(blog_id): check_admin() blog = Blogs.get(blog_id) if blog is None: raise APIResourceNotFoundError('Blog') blog.delete() return dict(id=blog_id)
def blog(blog_id): blog = Blogs.get(blog_id) if blog is None: raise HttpError.notfound() blog.html_content = markdown.markdown(blog.content) comments = Comments.find_by( 'where blog_id=? order by created_at desc limit 1000', blog_id) return dict(blog=blog, comments=comments, user=ctx.request.user)
def api_create_blog(): check_admin() i = ctx.request.input(name='', summary='', content='') name = i.name.strip() summary = i.summary.strip() content = i.content.strip() if not name: raise APIValueError('name', 'name cannot be empty.') if not summary: raise APIValueError('summary', 'summary cannot be empty.') if not content: raise APIValueError('content', 'content cannot be empty.') user = ctx.request.user blog = Blogs(user_id=user.id, user_name=user.name, name=name, summary=summary, content=content) blog.insert() return blog
def manage_blogs_edit(blog_id): blog = Blogs.get(blog_id) if blog is None: raise HttpError.notfound() return dict(id=blog.id, name=blog.name, summary=blog.summary, content=blog.content, action=path_join(_MODULE, '/api/blogs/%s' % blog_id), redirect=path_join(_MODULE, '/manage/blogs'), user=ctx.request.user)
def api_create_blog_comment(blog_id): user = ctx.request.user if user is None: raise APIPermissionError('Need signin.') blog = Blogs.get(blog_id) if blog is None: raise APIResourceNotFoundError('Blog') content = ctx.request.input(content='').content.strip() if not content: raise APIValueError('content') c = Comments(blog_id=blog_id, user_id=user.id, user_name=user.name, user_image=user.image, content=content) c.insert() return dict(comment=c)
def api_update_blog(blog_id): check_admin() i = ctx.request.input(name='', summary='', content='') name = i.name.strip() summary = i.summary.strip() content = i.content.strip() if not name: raise APIValueError('name', 'name cannot be empty.') if not summary: raise APIValueError('summary', 'summary cannot be empty.') if not content: raise APIValueError('content', 'content cannot be empty.') blog = Blogs.get(blog_id) if blog is None: raise APIResourceNotFoundError('Blog') blog.name = name blog.summary = summary blog.content = content blog.update() return blog
def _get_blogs_by_page(): total = Blogs.count_all() page = Page(total, _get_page_index()) blogs = Blogs.find_by('order by created_at desc limit ?,?', page.offset, page.limit) return blogs, page.to_dict()
def api_get_blog(blog_id): blog = Blogs.get(blog_id) if blog: return blog raise APIResourceNotFoundError('Blog')