예제 #1
0
파일: app.py 프로젝트: yoann-dev/flask_api
def update_book(isbn):
    request_data = request.get_json()
    if 'name' in request_data:
        Book.update_book_name(isbn, request_data['name'])
    if 'price' in request_data:
        Book.update_book_price(isbn, request_data['price'])
    return Response(status=204)
예제 #2
0
def add_book():
    request_data = request.get_json()
    if (validBookObject(request_data)):
        try:
            Book.add_book(request_data['name'], request_data['price'],
                          request_data['isbn'])
        except:
            print('Unexpected error: ', sys.exc_info()[0])
            print('more error', sys.exc_info()[1])

        response = Response("", status=201, mimetype='application/json')
        response.headers['Location'] = "/books/" + str(request_data['isbn'])
        return response
    else:
        invalidBookObjectErrorMsg = {
            "error":
            "Invalid book object passed in request",
            "helpString":
            "Data passed in similar to this {'name': 'bookname', 'price': 7.99, 'isbn': 792321354533 }"
        }

        response = Response(json.dumps(invalidBookObjectErrorMsg),
                            status=400,
                            mimetype='application/json')
        return response
예제 #3
0
def add_book():
    new_book = {
        "isbn": request.form['isbn'],
        "title": request.form['title'],
        "author": request.form['author'],
        "quote": request.form['quote']
    }
    if (validBookObject(new_book)):
        Book.add_book(new_book['title'], new_book['author'], new_book['isbn'],
                      new_book['quote'])
        # Response constructor
        bookInsertedMsg = "New book successfully inserted as follow: {0}".format(
            new_book)
        response = Response(json.dumps(bookInsertedMsg),
                            status=200,
                            mimetype='application/json')
        return response
    else:
        invalidBookObjectErrorMsg = {
            "error": "Invalid book object",
            "help": "{'isbn': 1, title': '2', 'author': '3', 'quote': '4'}"
        }
        response = Response(invalidBookObjectErrorMsg,
                            status=400,
                            mimetype='application/json')
        return response
예제 #4
0
def replace_book():
    changed_book = {
        "isbn": request.form['isbn'],
        "title": request.form['title'],
        "author": request.form['author'],
        "quote": request.form['quote']
    }
    if (not validBookObject(changed_book)):
        invalidBookObjectErrorMsg = {
            "error": "Invalid book object",
            "help": "{'title': '1', 'author': '2', 'quote': '3'}"
        }
        response = Response(json.dumps(invalidBookObjectErrorMsg),
                            status=400,
                            mimetype='application/json')
        return response

    Book.replace_book(oldIsbn, changed_book["isbn"], changed_book["title"],
                      changed_book["author"], changed_book["quote"])
    response = Response("Book changed!", status=204)
    replacementCompletedMsg = {
        'success': 'Book replaced successfully:{0}'.format(changed_book)
    }
    response = Response(json.dumps(replacementCompletedMsg),
                        status=200,
                        mimetype='application/json')
    return response
예제 #5
0
파일: app.py 프로젝트: nntrn/books
def patch_book(isbn):
    request_data = request.get_json()
    if 'name' in request_data:
        Book.update_name(isbn, request_data['name'])
    if 'price' in request_data:
        Book.update_price(isbn, request_data['price'])
    response = Response("", status=204)
    response.headers['Location'] = 'books' + str(isbn)
    return response
예제 #6
0
파일: app.py 프로젝트: yoann-dev/flask_api
def replace_book(isbn):
    request_data = request.get_json()
    if valid_book_object_put(request_data):
        Book.replace_book(isbn, request_data['name'], request_data['price'])
        return Response(status=204)
    else:
        invalid_error_msg = {'error': "Invalid book passed in request"}
        return Response(response=json.dumps(invalid_error_msg),
                        status=400,
                        mimetype='application/json')
예제 #7
0
파일: app.py 프로젝트: nntrn/books
def add_book():
    request_data = request.get_json()
    if validate_book(request_data):
        Book.add_book(request_data['isbn'], request_data['name'], request_data['price'])
        response = Response("", 201, mimetype='application/json')
        response.headers['Location'] = '/books/' + str(request_data['isbn'])
        return response
    else:
        return Response('Invalid book object. Must include "name", "price", and "isbn"', status=400,
                        mimetype='application/json')
예제 #8
0
def update_book(isbn):
    request_data = request.get_json()
    if ("name" in request_data):
        Book.update_book_name(isbn, request_data['name'])
    if ("price" in request_data):
        Book.update_book_price(isbn, request_data['price'])

    response = Response("", status=204)
    response.headers['Location'] = '/books' + str(isbn)
    return response
예제 #9
0
def update_book(isbn):
    request_data = request.get_json()

    # Do the updates
    if "name" in request_data:
        Book.update_book_name(isbn, request_data['name'])
    if "price" in request_data:
        Book.update_book_price(isbn, request_data['price'])

    response = Response("", status=204)
    response.headers['Location'] = "/books/" + str(isbn)
    return response
예제 #10
0
def add_book():
    request_data = request.get_json()
    if validBookObject(request_data):
        Book.add_book(request_data['name'], request_data['price'], request_data['isbn'], )
        response = Response('', 201, mimetype='application/json')
        response.headers['Location'] = '/books/' + str(request_data['isbn'])
        return response
    else:
        invalidBookObjectErrorMsg = {
            "error": "Invalid book object passed in request",
            "helpString": "Data passed in similar to this { 'name' : 'bookName' , 'price' : 2.99, 'isbn' : 1234232435 }"
        }
        response = Response(json.dumps(invalidBookObjectErrorMsg), status=400, mimetype='application/json')
        return response
예제 #11
0
파일: app.py 프로젝트: yoann-dev/flask_api
def add_book():
    request_data = request.get_json()
    if valid_book_object_post(request_data):
        Book.add_book(request_data['name'], request_data['price'],
                      request_data['isbn'])
        response = Response(status=201, mimetype='application/json')
        response.headers['Location'] = "/books/" + str(request_data['isbn'])
        return response
    else:
        invalid_error_msg = {'error': "Invalid book passed in request"}
        response = Response(response=json.dumps(invalid_error_msg),
                            status=400,
                            mimetype='application/json')
        return response
예제 #12
0
def replace_book(isbn):
    request_data = request.get_json()
    if not valid_put_request_data(request_data):
        invalidBookObjectErrorMsg = {
            "error": "Invalid book object passed in request",
            "helpString": "Data passed in similar to this { 'name' : 'bookName' , 'price' : 2.99 }"
        }
        response = Response(json.dumps(invalidBookObjectErrorMsg), status=400, mimetype='application/json')
        return response

    Book.replace_book(isbn, request_data['name'], request_data['price'])
    response = Response('', 201, mimetype='application/json')
    response.headers['Location'] = '/books/' + str(isbn)
    return response
예제 #13
0
파일: app.py 프로젝트: nntrn/books
def add_book_form():
    
    # this block is only entered when the form is submitted
    if request.method == 'POST':
        isbn = request.form.get('isbn')
        name = request.form['name']
        price = request.form['price']
        
        Book.add_book(isbn, name, price)

        return '''<h1>You added a new book!</h1>
                  <div>{}, {}, ${}</div>
                  <a href="/">View</a>'''.format(isbn, name, price)

    return '''<form method="POST">
예제 #14
0
파일: app.py 프로젝트: nntrn/books
def delete_book(isbn):
    if Book.delete_book(isbn):
        response = Response('', status=204)
        return response
    else:
        response = Response('ISBN not found. Unable to delete.', status=404, mimetype='application/json')
        return response
예제 #15
0
def update_book(isbn):
    request_data = request.get_json()
    if not valid_patch_request_data(request_data):
        invalidBookObjectErrorMsg = {
            "error": "Invalid book object passed in request"
        }
        response = Response(json.dumps(invalidBookObjectErrorMsg), status=400, mimetype='application/json')
        return response

    if 'name' in request_data:
        Book.update_book_name(isbn, request_data['name'])
    if 'price' in request_data:
        Book.update_book_price(isbn, request_data['price'])

    response = Response('', 201, mimetype='application/json')
    response.headers['Location'] = '/books/' + str(isbn)
    return response
예제 #16
0
def replace_book(isbn):
    request_data = request.get_json()
    if (not valid_put_request_data(request_data)):
        invalidBookObjectErrorMsg = {
            "error":
            "Invalid book object passed in request",
            "helpString":
            "Data passed in similar to this {'name': 'bookname', 'price': 7.99 }"
        }
        response = Response(json.dumps(invalidBookObjectErrorMsg),
                            status=400,
                            mimetype='application/json')
        return response

    Book.replace_book(isbn, request_data['name'], request_data['price'])
    response = Response("", status=204, mimetype='application/json')
    return response
예제 #17
0
def delete_book(isbn):
    if Book.delete_book(isbn):
        response = Response("", 204)
        return response

    errorMsg = {"error": "Book with ISBN " + str(isbn) + " not found."}
    response = Response(json.dumps(errorMsg), 404, mimetype='application/json')
    return response
예제 #18
0
def get_paginated_books(page_number):
    print(type(request.args.get('limit')))
    LIMIT = request.args.get('limit', DEFAULT_PAGE_LIMIT, int)
    start_index = page_number * LIMIT
    end_index = page_number * LIMIT
    print(start_index)
    print(end_index)
    return jsonify({'books': Book.get_all_books()[start_index:end_index]})
예제 #19
0
def replace_book(isbn):
    request_data = request.get_json()
    if (not (validBookObject(request_data))):
        invalidBookObjectErrorMsg = {
            "error":
            "Valid book object passed must be passed in the request",
            "helpString":
            "Data passed in should look similar to this {'name':'bookname','price':1.01}"
        }
        response = Response(json.dumps(invalidBookObjectErrorMsg),
                            status=400,
                            mimetype='application/json')
        return response

    Book.replace_book(isbn, request_data['name'], request_data['price'])
    response = Response("", status=204)
    return response
예제 #20
0
def add_book():
    request_data = request.get_json()
    if (valid_post_request_data(request_data)):
        Book.add_book(request_data['name'], request_data['price'],
                      request_data['isbn'])
        response = Response("", 201, mimetype='application/json')
        response.headers['Location'] = "/books/" + str(request_data['isbn'])
        return response
    errorMsg = {
        "error":
        "Invalid book object passed in request",
        "helpString":
        "Data passed in similar to this {'name': 'bookName', 'price': 8.99, 'isbn': 1234567890}"
    }
    response = Response(json.dumps(errorMsg),
                        status=400,
                        mimetype='application/json')
    return response
예제 #21
0
def delete_book(isbn):
    if Book.delete_book(isbn):
        return Response('', status=204)

    invalidIsbnErrorMsg = {
        "error": "Book with Isbn number is not found, please try with valid isbn"
    }
    response = Response(json.dumps(invalidIsbnErrorMsg), status=404, mimetype='application/json')
    return response
예제 #22
0
def addbook():
    if request.method == 'POST':
        book_name = request.form['book_name']
        book_description = request.form['book_description']
        book_price = request.form['book_price']
        book = Book(title=book_name, description=book_description, price=book_price)
        db.session.add(book)
        db.session.commit()

    return render_template('addbook.html')
예제 #23
0
def replace_book(isbn):
    request_data = request.get_json()

    # Invalid object data in request
    if not valid_put_request_data(request_data):
        errorMsg = {
            'error':
            'Valid book object must be passed in the request',
            'helpString':
            'Data passed in similar to this {"name": "bookName", "price": 7.99}'
        }
        response = Response(json.dumps(errorMsg),
                            status=400,
                            mimetype='application/json')
        return response

    Book.replace_book(isbn, request_data['name'], request_data['price'])

    response = Response("", status=204)
    return response
예제 #24
0
def delete_book(isbn):
    if (Book.delete_book(isbn)):
        response = Response("", status=204)
        return response
    invalidBookObjectErrorMsg = {
        "error":
        "Book with the ISBN number that was provided was not found, so therefore unable to delete"
    }
    return Response(json.dumps(invalidBookObjectErrorMsg),
                    status=404,
                    mimetype='application/json')
예제 #25
0
def add_book():
    request_data = request.get_json()
    #f
    # ## (valid_post_request_data(request_data)):

    if (valid_post_request_data(request_data)):
        Book.add_book(request_data['name'], request_data['price'],
                      request_data['isbn'])
        response = Response("", 201, mimetype='application/json')
        response.headers['Location'] = "/books/" + str(request_data['isbn'])
        return response
    errorMsg = {
        "error": "Invalid book object passed in request",
        "helpString": ""
    }

    # response =Response(json.dumps(errorMsg), status=400, mimetype='ablication/json')
    response = Response(json.dumps(errorMsg),
                        status=400,
                        mimetype='application/json')
    return response
예제 #26
0
def update_book(isbn):
    request_data = request.get_json()
    if (not valid_patch_request_data(request_data)):
        invalidBookObjectErrorMsg = {
            "error":
            "Invalid book object passed in request",
            "helpString":
            "Data passed in similar to this {'name': 'bookname', 'price': 7.99 }"
        }
        response = Response(json.dumps(invalidBookObjectErrorMsg),
                            status=400,
                            mimetype='application/json')
        return response

    if ("name" in request_data):
        Book.update_book_name(isbn, request_data['name'])
    if ("price" in request_data):
        Book.update_book_price(isbn, request_data['price'])

    response = Response("", status=204, mimetype='application/json')
    response.headers['Location'] = "/books/" + str(isbn)
    return response
예제 #27
0
def get_paginated_books(page_number):
    books = Book.get_all_books()
    LIMIT = request.args.get('limit', DEFAULT_PAGE_LIMIT, int)
    startIndex = (page_number - 1) * LIMIT
    endIndex = len(books)

    if (LIMIT < endIndex):
        endIndex = LIMIT * page_number
        if (endIndex > len(books)):
            endIndex = len(books)

    print(startIndex)
    print(endIndex)
    return jsonify({'books': books[startIndex:endIndex]})
예제 #28
0
def delete_book(isbn):
    if (Book.delete_book(isbn)):
        deletionCompletedMsg = {
            'success':
            'Book with ISBN {0} was deleted from collection.'.format(isbn)
        }
        response = Response(json.dumps(deletionCompletedMsg),
                            status=200,
                            mimetype='application/json')
        return response
    invalidBookObjectErrorMsg = {'error': 'Book with this ISBN was not found.'}
    response = Response(json.dumps(invalidBookObjectErrorMsg),
                        status=404,
                        mimetype='application/json')
    return response
예제 #29
0
def get_book_by_isbn(isbn):
    return_value = Book.get_book(isbn)
    return jsonify(return_value)
예제 #30
0
def books_list():
    return jsonify({'books': Book.get_all_books()})