Example #1
0
def edit(id):
    book = get_book_or_404(id)
    if request.method == 'POST':
        if request.form.getlist("lp_checkbox"):
            form = BookForm(request.form, obj=book)
            if form.validate():
                lpelements = sum(
                    list(map(int, request.form.getlist("lp_checkbox"))))
                book = form.save_book(Book())
                book.lpelement_id = lpelements
                db.session.add(book)
                db.session.commit()
                flash(f"Book {book.title} has been updated.", "success")
                return redirect(url_for('books.detail', id=book.id))
        else:
            flash("You must select at least one Learning Plan Element",
                  "danger")
            return redirect(url_for('books.edit', id=book.id))
    else:
        form = BookForm(obj=book)

    return render_template('books/edit.html',
                           book=book,
                           form=form,
                           lpes=get_lpes())
Example #2
0
def create():
    if request.method == 'POST':
        if request.form.getlist("lp_checkbox"):
            form = BookForm(request.form)
            if form.validate():
                lpelements = sum(
                    list(map(int, request.form.getlist("lp_checkbox"))))
                book = form.save_book(Book())
                book.lpelement_id = lpelements
                db.session.add(book)
                db.session.commit()
                flash(f"Book {book.title} created successfully.", "success")
                return redirect(url_for('books.detail', id=book.id))
        else:
            flash("You must select at least one Learning Plan Element",
                  "danger")
            return redirect(url_for('books.create'))
    else:
        form = BookForm()

    return render_template('books/create.html', form=form, lpes=get_lpes())