-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
126 lines (88 loc) · 3.5 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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():
return redirect(url_for('books_view_all'))
# Books
@app.route('/books/view/all', methods=['GET'])
@app.route('/books/', methods=['GET'])
def books_view_all():
books = Book.query.all()
return render_template('books/view_all.html', **locals())
@app.route('/books/add/', methods=['GET', 'POST'])
def books_add():
form = BookForm(request.form)
if request.method == "POST" and form.validate():
new_book = Book(title=form.data.get('title'))
new_book.authors = form.data.get('authors')
db.session.add(new_book)
db.session.commit()
flash("Book added")
return redirect(url_for('books_view_all'))
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)
form = BookForm(request.form, obj=book)
if request.method == "POST" and form.validate():
book.title = form.data.get('title')
db.session.commit()
flash("Book changed")
return redirect(url_for('books_view_all'))
return render_template('books/edit.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)
if request.method == "POST":
db.session.delete(book)
db.session.commit()
flash("Book deleted")
return redirect(url_for('books_view_all'))
return render_template('books/delete.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())