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_article_edit(*, request, id, title, category, tag, content, summary): r = web.Response() blog = yield from Blogs.findOne(id) blog = Blogs(id = id, title = title, summary = summary, content = content, update_time = currentTime(), create_time = blog.get('create_time'), user_id = blog.get('user_id'), view_count = blog.get('view_count')) result = yield from blog.update() category = urldecode(category) category = category[0].split(',') tags = tag.split('/') blog_category_id = 0 blog_tag_id = 0 if result > 0: #文章分类插入 blog_category_sql = 'delete from blog_category where blog_id = ?' yield from execute(blog_category_sql, [id]) blog_tag_sql = 'delete from blog_tag where blog_id = ?' yield from execute(blog_tag_sql, [id]) 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 int(id) > 0 and int(blog_category_id) > 0 and int(blog_tag_id) > 0: result = APIResult(1, '', '修改成功') else: result = APIResult(0, '', '修改失败') return jsonResult(r, result)