from flask import Flask, g, render_template, flash, redirect, session, request, url_for, abort


app = Flask(__name__, static_url_path='')
app.config.from_pyfile('config.py')


from models import db, Book, Author
from forms import AuthorForm, BookForm


# ERRORS
if not app.config['DEBUG']:
    @app.errorhandler(404)
    def page_not_found(error):
        return 'This page does not exist', 404

    @app.errorhandler(500)
    def page_not_found(error):
        return 'Server error', 500


# VIEWS
@app.route('/')
def index():
    form = BookForm()
    return render_template('index.html', **locals())


# Books
@app.route('/books/view/all', methods=['GET','POST'])
def books_view_all():
    books = Book.query.all()
    return render_template('books/view_all.html', **locals())


@app.route('/books/view/<int:book_id>', methods=['GET','POST'])
def books_view(book_id):
    book = Book.query.get_or_404(book_id)
    return render_template('books/view.html', **locals())


@app.route('/books/add/', methods=['GET', 'POST'])
def books_add():
    form = BookForm(request.form)
    form.authors.choices = [(a.id, a.title) for a in Author.query.all()]

    if request.method == "POST" and form.validate():
        # Add book
        pass
        # return redirect(url_for('book_view', book_id=book.id))

    return render_template('books/add.html', **locals())


@app.route('/books/edit/<int:book_id>', methods=['GET','POST'])
def books_edit(book_id):
    book = Book.query.get_or_404(book_id)
    return render_template('books/view.html', **locals())


@app.route('/books/delete/<int:book_id>', methods=['GET','POST'])
def books_delete(book_id):
    book = Book.query.get_or_404(book_id)
    return render_template('books/view.html', **locals())


# Authors
@app.route('/authors/view/all', methods=['GET'])
@app.route('/authors/', methods=['GET'])
def authors_view_all():
    authors = Author.query.all()
    return render_template('authors/view_all.html', **locals())


@app.route('/authors/add/', methods=['GET', 'POST'])
def authors_add():
    form = AuthorForm(request.form)

    if request.method == "POST" and form.validate():
        new_author = Author(name=form.data.get('name'))
        db.session.add(new_author)
        db.session.commit()
        flash("Author added")
        return redirect(url_for('authors_view_all'))

    return render_template('authors/add.html', **locals())


@app.route('/authors/edit/<int:author_id>', methods=['GET', 'POST'])
def authors_edit(author_id):
    author = Author.query.get_or_404(author_id)

    form = AuthorForm(request.form, obj=author)

    if request.method == "POST" and form.validate():
        author.name = form.data.get('name')
        db.session.commit()
        flash("Author changed")
        return redirect(url_for('authors_view_all'))

    return render_template('authors/edit.html', **locals())


@app.route('/authors/delete/<int:author_id>', methods=['GET', 'POST'])
def authors_delete(author_id):
    author = Author.query.get_or_404(author_id)

    if request.method == "POST":
        db.session.delete(author)
        db.session.commit()
        flash("Author deleted")
        return redirect(url_for('authors_view_all'))

    return render_template('authors/delete.html', **locals())

@@ -1073,153 +1073,200 @@
 rm)%0A
+%0A
     
-form.authors.choices = %5B(a.id, a.title) for a in Author.query.all()%5D%0A%0A    if request.method == %22POST%22 and form.validate():%0A        # Add 
+if request.method == %22POST%22 and form.validate():%0A        new_book = Book(title=form.data.get('title'))%0A        new_book.authors = form.data.get('authors')%0A        db.session.add(new_
 book
+)
 %0A   
@@ -1274,22 +1274,63 @@
     
-pass%0A        #
+db.session.commit()%0A        flash(%22Book added%22)%0A       
  ret
@@ -1359,31 +1359,19 @@
 book
+s
 _view
-', book_id=book.id
+_all'
 ))%0A%0A
@@ -1552,32 +1552,284 @@
 or_404(book_id)%0A
+    form = BookForm(request.form, obj=book)%0A%0A    if request.method == %22POST%22 and form.validate():%0A        book.title = form.data.get('title')%0A        db.session.commit()%0A        flash(%22Book changed%22)%0A        return redirect(url_for('books_view_all'))%0A%0A
     return rende
@@ -1838,36 +1838,36 @@
 template('books/
-view
+edit
 .html', **locals
@@ -1999,32 +1999,208 @@
 or_404(book_id)%0A
+%0A    if request.method == %22POST%22:%0A        db.session.delete(book)%0A        db.session.commit()%0A        flash(%22Book deleted%22)%0A        return redirect(url_for('books_view_all'))%0A%0A
     return rende
@@ -2209,36 +2209,38 @@
 template('books/
-view
+delete
 .html', **locals

Implement books view