Ejemplo n.º 1
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)