Exemplo n.º 1
0
def del_post(id):

    check_post(id)

    db = get_db()
    delete_post(db, id)

    return jsonify({'result': True})
Exemplo n.º 2
0
def post(id):
    post = dict(check_post(id))
    error = None

    # post with id does not exist
    if not post:
        error = json.dumps({"error": "Post id {0} not available.".format(id)})
    if error:
        return Response(
            error,
            status=404,
            mimetype="application/json"
        )

    # get post by id
    if request.method == "GET":
        post['created'] = post['created'].strftime("%d-%b-%Y (%H:%M:%S)")
        data = json.dumps(dict(post))
        return Response(
            data,
            status=200,
            mimetype="application/json"
        )

    # update post by id
    elif request.method == "PUT":
        data = request.get_json()
        title = data.get('title', '')
        body = data.get('body', '')

        if not title:
            error = json.dumps({"error": "Title is required"})
        if error:
            return Response(
                error,
                status=400,
                mimetype="application/json"
            )

        db = get_db()
        update_post(db, title, body, id)
        message = json.dumps({'message': 'Post id {0} update successfully'.format(id)})
        return Response(
            message,
            status=200,
            mimetype="application/json"
        )
    
    # delete post by id
    elif request.method == "DELETE":
        db = get_db()
        delete_post(db, id)
        message = json.dumps({'message': 'Post id {0} delete successfully'.format(id)})
        return Response(
            message,
            status=200,
            mimetype="application/json"
        )
Exemplo n.º 3
0
def delete(id):
    """Delete a post.

    Ensures that the post exists and that the logged in user is the
    author of the post.
    """
    check_post(id)
    db = get_db()
    delete_post(db, id)
    return redirect(url_for("blog.index"))
Exemplo n.º 4
0
def delete(id):
    """Delete a post.

    Ensures that the post exists and that the logged in user is the
    author of the post.
    """
    check_post(id)
    db = get_db()
    delete_post(db, id)
    return Response("Post deleted", status=200)
Exemplo n.º 5
0
def delete(id):
    """Delete a post.
    Ensures that the post exists and that the logged in user is the
    author of the post.
    """
    post = check_post(id)

    if not post:
        return Response("post not found", status=404)

    db = get_db()
    delete_post(db, id)

    return Response("post was deleted", status=200)
Exemplo n.º 6
0
def delete(post_id):
    """Delete a post."""
    check_post(post_id)
    delete_post(get_db(), post_id)
    return Response("Success: post was deleted", 200)