def api_author_posts(author_name): posts = Post.get_posts_by_author_name(author_name) posts = map(lambda post: dump_post(post), posts) posts = list(posts) return ok({ 'author_name': author_name, 'posts': posts, })
def posts_id_delete(id): try: post = Post.get(id=id) except Post.DoesNotExist: return error('post does not exist', 404) if post.magazine_posts.count(): return error('存在关联的杂志引用,确定删除需要先删除关联的杂志引用') post.delete_instance() return ok()
def posts_public(id): try: post = Post.get(id=id) except Post.DoesNotExist: return error('post does not exist', 404) print(post.category) if post.category != 'source': post.update_visits() if post.category in ('recipe', 'recipe_list'): return redirect(post.content) else: return render_template('posts.html', post=post)
def posts_new(): json_data = request.get_json() user_id = current_user.id author_name = json_data.get('author') category = json_data.get('category') title = json_data.get('title') content = json_data.get('content') if not all((author_name, category, title, content)): return error('没有提供所有参数') post = Post.create_post(user_id=user_id, author_name=author_name, category=category, title=title, content=content) return ok(dump_post(post, mode='only_id'))
def posts_new(): json_data = request.get_json() user_id = current_user.id author_name = json_data.get('author') category = json_data.get('category') title = json_data.get('title') content = json_data.get('content') if not all((author_name, category, title, content)): return error('没有提供所有参数') post = Post.create_post( user_id=user_id, author_name=author_name, category=category, title=title, content=content) return ok(dump_post(post, mode='only_id'))
def posts_id_update(id): try: post = Post.get(id=id) except Post.DoesNotExist: return error('post does not exist', 404) json_data = request.get_json() author_name = json_data.get('author') category = json_data.get('category') title = json_data.get('title') content = json_data.get('content') if not all((author_name, category, title, content)): return error('没有提供所有参数') post.update_post(author_name=author_name, category=category, title=title, content=content) return ok()
def posts_id_update(id): try: post = Post.get(id=id) except Post.DoesNotExist: return error('post does not exist', 404) json_data = request.get_json() author_name = json_data.get('author') category = json_data.get('category') title = json_data.get('title') content = json_data.get('content') if not all((author_name, category, title, content)): return error('没有提供所有参数') post.update_post( author_name=author_name, category=category, title=title, content=content) return ok()
def dump_post_list(): posts_data = [] posts = Post.all() for post in posts: posts_data.append(dump_post(post, mode='without_content')) return posts_data
def posts_id(id): try: post = Post.get(id=id) except Post.DoesNotExist: return error('post does not exist', 404) return ok(dump_post(post))