-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
293 lines (221 loc) · 10.3 KB
/
app.py
File metadata and controls
293 lines (221 loc) · 10.3 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from flask import Flask, render_template
from flask import request, redirect, flash, url_for
from db_connector import connect_to_database, execute_query
# create the web application
app = Flask(__name__)
app.secret_key = 'SECRETKEY'
@app.route('/')
@app.route('/index')
def homepage():
return render_template('index.html')
# -----------------------------------------------
# ROUTES PERTAINING TO STUDENTS
# -----------------------------------------------
@app.route('/students')
def students():
db_connection = connect_to_database()
query = "SELECT studentID, first_name, last_name, email FROM Students;"
result = execute_query(db_connection, query).fetchall()
return render_template('students.html', students=result)
@app.route('/addstudent', methods=['GET', 'POST'])
def addstudent():
db_connection = connect_to_database()
# Show form to add student if method is GET
if request.method == 'GET':
return render_template('addstudent.html')
# Add new student to database if method is POST
if request.method == 'POST':
first_name_input = request.form['fname']
last_name_input = request.form['lname']
email_input = request.form['email']
query = "INSERT INTO Students (first_name, last_name, email) \
VALUES (%s, %s, %s)"
data = (first_name_input, last_name_input, email_input)
result = execute_query(db_connection, query, data)
flash('A Student Has Been Added!', 'success')
return redirect(url_for('students'))
@app.route('/updatestudent/<int:id>', methods=['GET', 'POST'])
def updatestudent(id):
db_connection = connect_to_database()
if request.method == 'GET':
query = "SELECT studentID, first_name, last_name, email FROM Students WHERE studentID = %s" % id
student_result = execute_query(db_connection, query).fetchall()
studentIDResult = execute_query(db_connection, query).fetchone()
if student_result == None:
return "No student found!"
return render_template('updatestudent.html', student=student_result, studentID=studentIDResult)
elif request.method == 'POST':
studentID_input = request.form['id']
first_name_input = request.form['fname']
last_name_input = request.form['lname']
email_input = request.form['email']
query = "UPDATE Students \
SET first_name = %s, last_name = %s, email = %s \
WHERE studentID = %s"
data = (first_name_input, last_name_input,
email_input, studentID_input)
result = execute_query(db_connection, query, data)
flash('Student updated!', 'success')
return redirect(url_for('students'))
@app.route('/deletestudent/<int:id>')
def deletestudent(id):
db_connection = connect_to_database()
query = "DELETE FROM Students WHERE studentID = %s"
data = (id, )
result = execute_query(db_connection, query, data)
flash('Student has been deleted!', 'success')
return redirect(url_for('students'))
# -----------------------------------------------
# ROUTES PERTAINING TO BOOKS
# -----------------------------------------------
@app.route('/books')
def books():
db_connection = connect_to_database()
query = "SELECT bookID, title, year_published, Books.authorID, \
Authors.first_name, Authors.last_name, Genres.genre_name \
FROM Books \
INNER JOIN Authors ON Books.authorID = Authors.authorID \
INNER JOIN Genres ON Books.genreID = Genres.genreID \
ORDER BY bookID"
result = execute_query(db_connection, query).fetchall()
return render_template('books.html', books=result)
@app.route('/addbook', methods=['GET', 'POST'])
def addbook():
db_connection = connect_to_database()
# Show form to add book if method is GET
if request.method == 'GET':
# Get data of Authors and Genres so user can link an author to a book
author_query = "SELECT * FROM Authors"
author_result = execute_query(db_connection, author_query).fetchall()
genre_query = "SELECT * FROM Genres"
genre_result = execute_query(db_connection, genre_query).fetchall()
return render_template('addbook.html', authors=author_result, genres=genre_result)
# Add new book to database if method is POST
if request.method == 'POST':
title_input = request.form['title']
year_published_input = request.form['year']
author_input = request.form['author']
genre_input = request.form['genre']
query = "INSERT INTO Books (title, year_published, authorID, genreID) \
VALUES (%s, %s, %s, %s)"
data = (title_input, year_published_input, author_input, genre_input)
result = execute_query(db_connection, query, data)
flash('A Book Has Been Added!', 'success')
return redirect(url_for('books'))
@app.route('/updatebook/<int:id>', methods=['GET', 'POST'])
def updatebook(id):
db_connection = connect_to_database()
if request.method == 'GET':
query = "SELECT bookID, title, year_published, Books.authorID, \
Authors.first_name, Authors.last_name, Genres.genre_name \
FROM Books \
INNER JOIN Authors ON Books.authorID = Authors.authorID \
INNER JOIN Genres ON Books.genreID = Genres.genreID \
WHERE bookID = %s" % id
book_result = execute_query(db_connection, query).fetchall()
bookID_result = execute_query(db_connection, query).fetchone()
if book_result == None:
return "No book found!"
return render_template('updatebook.html', book=book_result, bookID=bookID_result)
elif request.method == 'POST':
bookID_input = request.form['id']
title_input = request.form['title']
year_input = request.form['year']
query = "UPDATE Books \
SET title = %s, year_published = %s \
WHERE bookID= %s"
data = (title_input, year_input, bookID_input)
result = execute_query(db_connection, query, data)
flash('Book updated!', 'success')
return redirect(url_for('books'))
@app.route('/deletebook/<int:id>')
def deletebook(id):
db_connection = connect_to_database()
query = "DELETE FROM Books WHERE bookID = %s"
data = (id, )
result = execute_query(db_connection, query, data)
flash('Book has been deleted!', 'success')
return redirect(url_for('books'))
# -----------------------------------------------
# ROUTES PERTAINING TO LOANING BOOKS
# -----------------------------------------------
@app.route('/booksonloan')
def booksonloan():
db_connection = connect_to_database()
query = "SELECT loanID, Books.title, \
Books_On_Loan.studentID, Students.first_name, Students.last_name, \
date_checkout, date_due, date_returned, late_fee \
FROM Books_On_Loan \
INNER JOIN Books ON Books_On_Loan.bookID = Books.bookID \
INNER JOIN Students ON Books_On_Loan.studentID = Students.studentID \
ORDER BY loanID"
result = execute_query(db_connection, query).fetchall()
return render_template('booksonloan.html', booksonloan=result)
@app.route('/addloanbook', methods=['GET', 'POST'])
def addloanbook():
db_connection = connect_to_database()
# Show form to add a loaned book if method is GET
if request.method == 'GET':
# Get data of books and students to display options to user
book_query = "SELECT * FROM Books"
book_result = execute_query(db_connection, book_query).fetchall()
student_query = "SELECT * FROM Students"
student_result = execute_query(db_connection, student_query).fetchall()
return render_template('addloanbook.html', books=book_result, students=student_result)
# Add loaned book to database if method is POST
if request.method == 'POST':
book_input = request.form['book']
student_input = request.form['student']
loandate_input = request.form['loandate']
duedate_input = request.form['duedate']
date_returned = None
latefee_input = request.form['latefee']
query = "INSERT INTO Books_On_Loan (bookID, studentID, \
date_checkout, date_due, date_returned, late_fee) \
VALUES (%s, %s, %s, %s, %s, %s)"
data = (book_input, student_input, loandate_input,
duedate_input, date_returned, latefee_input)
result = execute_query(db_connection, query, data)
flash('A book has been successfully loaned!', 'success')
return redirect(url_for('booksonloan'))
@app.route('/updateloanbook/<int:id>', methods=['GET', 'POST'])
def updateloanbook(id):
db_connection = connect_to_database()
if request.method == 'GET':
# Get information to be shown to user when updating a book loan
query = "SELECT loanID, Books.title, \
Books_On_Loan.studentID, Students.first_name, Students.last_name, \
date_checkout, date_due, date_returned, late_fee \
FROM Books_On_Loan \
INNER JOIN Books ON Books_On_Loan.bookID = Books.bookID \
INNER JOIN Students ON Books_On_Loan.studentID = Students.studentID \
WHERE loanID = %s" % id
bookloan_result = execute_query(db_connection, query).fetchall()
loanID_result = execute_query(db_connection, query).fetchone()
if bookloan_result == None:
return "No book loan data found!"
return render_template('updateloanbook.html', bookloan=bookloan_result, loanID=loanID_result)
elif request.method == 'POST':
# Get data from form
loanID_input = request.form['id']
duedate_input = request.form['duedate']
if not request.form['datereturned']:
date_returned_input = None
else:
datereturned_input = request.form['datereturned']
# Perform query to update loan book data
query = "UPDATE Books_On_Loan \
SET date_due = %s, date_returned = %s \
WHERE loanID = %s"
data = (duedate_input, datereturned_input, loanID_input)
result = execute_query(db_connection, query, data)
flash('Loan data updated!', 'success')
return redirect(url_for('booksonloan'))
@app.route('/deleteloanbook/<int:id>')
def deleteloanbook(id):
db_connection = connect_to_database()
query = "DELETE FROM Books_On_Loan WHERE loanID = %s"
data = (id, )
result = execute_query(db_connection, query, data)
flash('Loaned book data has been deleted!', 'success')
return redirect(url_for('booksonloan'))