def api_edit_blog(request, *, id, name, summary, content): """edit the blog: if exist id: update the blog else: save the blog """ check_admin(request) if not name or not name.strip(): raise APIValueError('name', 'blog name cannot be empty.') if not summary or not summary.strip(): raise APIValueError('summary', 'blog summary cannot be empty.') if not content or not content.strip(): raise APIValueError('content', 'blog content cannot be empty.') # save the blog if id == '': 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()) yield from save_model(blog) # update the blog else: blog = yield from find_model(model='blog', id=id) blog.name = name blog.summary = summary blog.content = content yield from update_model(blog) return blog