Exemplo n.º 1
0
def detailsBook(id):
    global apiInfo

    try:
        # Using eval to convert string to a dictionairy
        bookList = json.loads(
            requests.get(app.config['API_ROOT_URL'] + '/books' + '/' +
                         str(id)).content)
    except:
        bookList = []

    for book in bookList:
        # There is one and only one book
        orgBook = Book(
            book['id'],
            book['name'],
            book['price'],  # Two decimals
            book['isbn'],
            book['isObsolete'],
            book['bookType'])

    orgBook.price = ConvertToTwoDecimals(orgBook.price)
    orgBook.isObsolete = ConvertBooleanToText(orgBook.isObsolete)
    orgBook.bookType = ConvertEnumBookTypeToDescription(orgBook.bookType)

    return render_template('books/details.html',
                           actionTitle='Book details',
                           appTitle=app.config['APP_TITLE'],
                           api=apiInfo,
                           book=vars(orgBook))
Exemplo n.º 2
0
def addBook():
    global apiInfo

    orgBook = Book()

    form = EditBookForm()

    if request.method == 'GET':
        form.id.data = orgBook.id
        form.name.data = orgBook.name
        form.price.data = orgBook.price
        form.isbn.data = orgBook.isbn
        form.isObsolete.data = orgBook.isObsolete
        form.bookType.data = orgBook.bookType

    if request.method == 'POST' and form.validate(
    ):  # Equivalent to validate_on_submit()
        newBook = Book()
        newBook.name = request.form['name']
        newBook.isbn = request.form['isbn']
        newBook.price = request.form['price']
        newBook.isObsolete = form.isObsolete.data  # TODO (bug) request.form['<booelan>'] does not return
        newBook.bookType = request.form['bookType']

        # TODO (bug) Error when doing the api-call
        addedBook = json.loads(
            requests.post(app.config['API_ROOT_URL'] + '/books',
                          json=vars(newBook)).content)
        newBook.id = addedBook['id']

        flash('Added book {}'.format(vars(newBook)))
        return redirect('/books')

    return render_template('books/edit.html',
                           actionTitle='Add book',
                           appTitle=app.config['APP_TITLE'],
                           api=apiInfo,
                           book=vars(orgBook),
                           form=form)