def manage_create_blog(): return { '__template__': 'manage_blog_edit.html', 'id': '', 'action': '/api/blogs', 'alltags': json.dumps([{ "key": tag.id, "value": tag.name } for tag in (yield from Tags.find_all())]) }
def api_edit_blog(request, *, id, name, description, summary, content, category_id, tags): check_admin(request) if not name or not name.strip(): raise APIValueError('name', 'name can not be empty') if not summary or not summary.strip(): raise APIValueError('summary', 'summary can not be empty') if not content or not content.strip(): raise APIValueError('content', 'content can not be empty') #blog = Blog(user_id = request.__user__.id, user_name= request.__user__.name, user_image = request.__user__.image, name = name.strip(), summary = summary.strip(), content = content.strip()) blog = yield from Blog.find(id) if not blog: raise APIValueError('id', 'request path error, id : {}'.format(id)) category = yield from Category.find(category_id) if category: #raise APIValueError('category', 'can not find category, category_id:{}'.format(category_id)) category_id = category.id category_name = category.name else: category_name = '' tag_ids = [] if len(tags) > 0: for tag in tags: if tag["key"]: rs = yield from Tags.find(tag["key"]) if rs: tag_ids.append(rs.id) else: rs = yield from Tags.find_all("name=?", [tag["value"]]) if len(rs) > 0: tag_ids.append(rs[0].id) else: #create new tag tag = Tags(name=tag["value"]) rows, lastrowid = yield from tag.save() tag_ids.append(lastrowid) blog.name = name blog.description = description blog.summary = summary blog.content = content blog.category_id = category_id blog.category_name = category_name blog.tags = ",".join([str(id) for id in tag_ids]) blog.updated_at = time.time() yield from blog.update() return blog
def api_get_blog(*, id): blog = yield from Blog.find(id) tags = [] if blog.tags: for tag_id in blog.tags.split(","): tag = yield from Tags.find(tag_id) if tag: tags.append({"key": tag.id, "value": tag.name}) blog.tags = tags blog.alltags = [{ "key": tag.id, "value": tag.name } for tag in (yield from Tags.find_all())] return blog