def add_author():
    form = AuthorForm(request.form)
    if request.method == 'POST' and form.validate():
        author = Author(form.name.data)
        db_session.add(author)
        db_session.commit()
        flash('The author was added to the Library.')
        return redirect(url_for('view_author', id=author.id))
    return render_template('library/add_author.html', form=form)
def delete_book(id):
    error = None
    book = Book.query.get(id)
    if not book:
        error = 'Sorry, we don\'t have the book with id {0}.'.format(id)
    if request.method == 'POST':
        db_session.delete(book)
        db_session.commit()
        flash('The book was deleted.')
        return redirect(url_for('view_book'))
    return render_template('library/delete_book.html', book=book, error=error)
def delete_author(id):
    error = None
    author = Author.query.get(id)
    if not author:
        error = 'Sorry, we don\'t have the author with id {0}.'.format(id)
    if request.method == 'POST':
        db_session.delete(author)
        db_session.commit()
        flash('The author and his books were deleted.')
        return redirect(url_for('view_author'))
    return render_template('library/delete_author.html', author=author,
                           error=error)
def add_book():
    form = BookForm(request.form)
    form.authors.choices = sorted([(a.id, a.name) for a in Author.query.all()],
        key=lambda author: author[1].split()[-1])
    if request.method == 'POST' and form.validate():
        book = Book(form.name.data)
        for id in form.authors.data:
            book.authors.append(Author.query.get(id))
        db_session.add(book)
        db_session.commit()
        flash('The book was added to the Library.')
        return redirect(url_for('view_book', id=book.id))
    return render_template('library/add_book.html', form=form)
def edit_author(id):
    error = None
    author = Author.query.get(id)
    if not author:
        error = 'Sorry, we don\'t have the author with id {0}.'.format(id)
    form = AuthorForm(request.form, obj=author)
    if request.method == 'POST' and form.validate():
        form.populate_obj(author)
        db_session.add(author)
        db_session.commit()
        return redirect(url_for('view_author', id=author.id))
    return render_template('library/edit_author.html', form=form, author=author,
                           error=error)
def register():
    errors = []
    form = RegistrationForm(request.form)
    if request.method == 'POST' and form.validate():
        q = User.query
        if q.filter(User.name == form.name.data).first():
            errors.append('Username "{0}" already exists.'
                          .format(form.name.data))
        if q.filter(User.email == form.email.data).first():
            errors.append('Email "{0}" has been already registered.'
                          .format(form.email.data))
        if not errors:
            user = User(form.name.data, form.email.data,
                        form.password.data)
            db_session.add(user)
            db_session.commit()
            flash('Thank you for registration.')
            return redirect(url_for('login'))
    return render_template('users/register.html', form=form, errors=errors)
def edit_book(id):
    error = None
    book = Book.query.get(id)
    if not book:
        error = 'Sorry, we don\'t have the book with id {0}.'.format(id)
    form = BookForm(request.form, obj=book)
    form.authors.choices = sorted([(a.id, a.name) for a in Author.query.all()],
        key=lambda author: author[1].split()[-1])
    if request.method == 'POST' and form.validate():
        book.name = form.name.data
        book.authors = []
        for id in form.authors.data:
            book.authors.append(Author.query.get(id))
        db_session.add(book)
        db_session.commit()
        return redirect(url_for('view_book', id=book.id))
    form.authors.data = [a.id for a in book.authors]
    return render_template('library/edit_book.html', form=form, book=book,
                           error=error)