def getBlog(id): blog = yield from Blog.find(id) comments = yield from Comment.findAll('blog_id=?', [id], orderBy='create_time desc') for c in comments: c.htmlContent = text2Html(c.content) return {'__template__': 'blog.html', 'blog': blog, 'comments': comments}
def api_delete_blog(request, *, id): #校验当前用户权限: check_admin(request) #数据库Blog表中查询指定文章信息: blog = yield from Blog.find(id) #将Blog信息从数据库删除: yield from blog.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 = www.markdown2.markdown(blog.content) return {'__template__': 'blog.html', 'blog': blog, 'comments': comments}
def find_model(model, id): if model == 'blog': blog = yield from Blog.find(id) return blog if model == 'user': user = yield from User.find(id) return user if model == 'comment': comment = yield from Comment.find(id) 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 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 api_update_blog(id, request, *, name, summary, content): check_admin(request) blog = yield from Blog.find(id) if not name or not name.strip(): raise APIValueError('name', 'name cannot be empty.') if not summary or not summary.strip(): raise APIValueError('summary', 'summary cannot be empty.') if not content or not content.strip(): raise APIValueError('content', 'content cannot be empty.') blog.name = name.strip() blog.summary = summary.strip() blog.content = content.strip() yield from blog.update() return blog
def get_blog(id): #通过id在数据库Blog表中查询对应内容: blog = yield from Blog.find(id) #通过id在数据库Comment表中查询对应内容: comments = yield from Comment.findAll('blog_id=?', [id], orderBy='created_at desc') for c in comments: #将content值从text格式转换成html格式: c.html_content = text2html(c.content) blog.html_content = markdown2.markdown(blog.content) return { '__template__': 'blog.html', 'blog': blog, 'comments': comments }
def apiUpdateBlog(id, request, *, name, summary, content): checkAdmin(request) blog = yield from Blog.find(id) if not name or not name.strip(): raise APIValueError('文章标题', '文章标题不能为空') if not summary or not summary.strip(): raise APIValueError('文章概要', '文章概要不能为空') if not content or not content.strip(): raise APIValueError('文章内容', '文章内容不能为空') blog.name = name.strip() blog.summary = summary.strip() blog.content = content.strip() yield from blog.merge() return blog
def apiCreateComment(id, request, *, content): user = request.__user__ if user is None: raise APIPermissionError('请先登录') if not content or not content.strip(): raise APIValueError('内容为空') blog = yield from Blog.find(id) if blog is None: raise APIResourceNotFoundError('文章不存在') comment = Comment(blogId=blog.id, userId=user.id, userName=user.name, userImage=user.image, content=content.strip()) yield from Comment.save(comment) return content
async def api_create_comment(blog_id, request, *, content): check_admin(request) user = request.__user__ if user is None: raise APIPermissionError() if not content or not content.strip(): raise APIValueError('content') blog = Blog.find(blog_id) if blog is None: raise APIResourceNotFoundError('Blog') comment = Comment(blog_id=blog_id, user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, content=content) await comment.save() return comment
def api_create_comment(id, request, *, content): #获取请求中的用户信息: user = request.__user__ #用户信息为None则抛出异常: if user is None: raise APIPermissionError('Please signin first.') #参数中内容信息为空,抛出异常: if not content or not content.strip(): raise APIValueError('content') #数据库Blog表中查询指定文章信息: blog = yield from Blog.find(id) #查询无结果则抛出异常: if blog is None: raise APIResourceNotFoundError('Blog') #创建comment实例: comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) #将Comment信息存储到数据库: yield from comment.save() return comment
def get_blog(id,request): cookie_str = request.cookies.get(COOKIE_NAME) user = '' if cookie_str: if 'deleted' in cookie_str: user = '' else: user = yield from cookie2user(cookie_str) 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 = www.markdown2.markdown(blog.content) return { '__template__': 'blog.html', 'blog': blog, 'comments': comments, 'user':user }
def api_update_blog(id, request, *, name, summary, content): #校验当前用户权限: check_admin(request) #数据库Blog表中查询指定文章信息: blog = yield from Blog.find(id) #校验传递值中参数‘name’是否为空或空串,为空则抛出异常: if not name or not name.strip(): raise APIValueError('name', 'name cannot be empty.') #校验传递值中参数‘summary’是否为空或空串,为空则抛出异常: if not summary or not summary.strip(): raise APIValueError('summary', 'summary cannot be empty.') #校验传递值中参数‘content’是否为空或空串,为空则抛出异常: if not content or not content.strip(): raise APIValueError('content', 'content cannot be empty.') #将传递值中的信息赋值到blog实例中: blog.name = name.strip() blog.summary = summary.strip() blog.content = content.strip() #将Blog信息更新到数据库: yield from blog.update() return blog
def api_get_blog(*, id): blog = yield from Blog.find(id) return blog
def getBlog(id): blog = yield from Blog.find(id) return blog
def api_delete_blog(request, *, id): check_admin(request) blog = yield from Blog.find(id) yield from blog.remove() return dict(id=id)