def api_article_add(*, request, title, category, tag, content, summary): r = web.Response() blog = Blogs(title = title, user_id = getWebCookie(request, 'id'), summary = summary, content = content) id = yield from blog.save() category = urldecode(category) category = category[0].split(',') tags = tag.split('/') if id > 0: #文章分类插入 for i in category: blog_category = BlogCategory(blog_id = id, category_id = i) blog_category_id = yield from blog_category.save() #标签分类插入 for t in tags: t = t.strip() tag_title = yield from Tag.findAll('title=?', [t]) #如果标签存在,则插入博客标签表,若不存在,则两张表都要插入 if len(tag_title) > 0: tag_id = tag_title[0].get('id') else: tag = Tag(title = t) tag_id = yield from tag.save() blog_tag = BlogTag(blog_id = id, tag_id = tag_id) blog_tag_id = yield from blog_tag.save() if id > 0 and blog_category_id > 0 and blog_tag_id > 0: result = APIResult(1, '', '发布成功') else: result = APIResult(0, '', '发布失败') return jsonResult(r, result)
def api_create_blog(request, name, summary, content): if not name or not name.strip(): raise APIValueError('name','empty name') if not summary or not summary.strip(): raise APIValueError('summary', 'empty summary') if not content or not content.strip(): raise APIValueError('content', 'empty content') blog = Blogs(user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, name=name.strip(), summary=summary.strip(), content=content.strip()) yield from blog.save() r = aiohttp.web.Response() r.content_type = 'application/json' r.body = json.dumps(blog,ensure_ascii=False).encode('utf-8') return r