Beispiel #1
0
def edit_post(post_id):
    if not post_exists(post_id):
        return error('Post does not exist.', 404)
    data = request.get_json()
    post_data = {
        field: data[field]
        for field in Post._meta.allowed_fields if data.get(field) is not None
    }
    Post.update(post_data).where(Post.id == post_id).execute()
    return jsonify(Post.get_by_id(post_id).to_dict()), 200
Beispiel #2
0
def edit_users_post(user_id, post_id):
    user = user_exists(user_id, return_user=True)
    if not user:
        return error('User does not exist.', 404)
    if user.posts.where(Post.id == post_id).first() is None:
        return error('Selected user does not have the post.', 404)
    data = request.get_json()
    post_data = {field: data[field] for field in Post._meta.allowed_fields
                 if data.get(field) is not None}
    Post.update(post_data).where(Post.id == post_id).execute()
    post = Post.get_by_id(post_id)
    return jsonify(post.to_dict()), 200