-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit1.py
511 lines (427 loc) · 19.4 KB
/
init1.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#Import Flask Library
from datetime import date
from flask import Flask, render_template, request, session, url_for, redirect, flash
import pymysql.cursors
import hashlib
#for uploading photo:
from app import app
#from flask import Flask, flash, request, redirect, render_template
from werkzeug.utils import secure_filename
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
###Initialize the app from Flask
##app = Flask(__name__)
##app.secret_key = "secret key"
#Configure MySQL
conn = pymysql.connect(host='localhost',
port = 8889,
user='root',
password='root',
db='FlaskDemo',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
def allowed_image(filename):
if not "." in filename:
return False
ext = filename.rsplit(".", 1)[1]
if ext.upper() in app.config["ALLOWED_IMAGE_EXTENSIONS"]:
return True
else:
return False
def allowed_image_filesize(filesize):
if int(filesize) <= app.config["MAX_IMAGE_FILESIZE"]:
return True
else:
return False
# Define a route to hello function
@app.route('/')
def hello():
return render_template('index.html')
# @app.route("/")
# def index():
# query = request.args.get("q")
# songs = search_songs(query)
# return render_template("index.html")
@app.route("/showSearchBar", methods=['GET','POST'])
def show_bar():
return render_template('browse.html')
# @app.route("/searchSong_genre", methods=['POST'])
# def searchSong_genre():
# cursor = conn.cursor()
# genre = request.form['genre']
# # rating_input = request.form['rating']
# # fname_input, lname_input = request.form['artist']
# query = 'SELECT songID, title FROM song NATURAL JOIN songGenre WHERE genre = "%s"'
# cursor.execute(query, (genre))
# data = cursor.fetchall()
# conn.commit()
# cursor.close()
# return render_template('browse.html',posts=data)
@app.route('/search', methods=['GET','POST'])
def search_songs():
genre_input = request.form['genre']
stars_input = request.form['stars']
fname_input = request.form['fname']
cursor = conn.cursor()
if(stars_input and genre_input and fname_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE stars >= %s AND genre = %s AND fname = %s'
cursor.execute(query, (int(stars_input), genre_input, fname_input))
data = cursor.fetchall()
cursor.close()
return render_template('show_search.html', posts=data)
if(genre_input and stars_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE stars >= %s AND genre = %s'
cursor.execute(query, (int(stars_input), genre_input))
data = cursor.fetchall()
cursor.close()
return render_template('show_search.html', posts=data)
if(genre_input and fname_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE genre = %s AND fname = %s'
cursor.execute(query, (genre_input, fname_input))
data = cursor.fetchall()
cursor.close()
return render_template('show_search.html', posts=data)
if(stars_input and fname_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE stars >= %s AND fname = %s'
cursor.execute(query, (stars_input, fname_input))
data = cursor.fetchall()
cursor.close()
return render_template('show_search.html', posts=data)
if(genre_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE genre = %s'
cursor.execute(query, genre_input)
data = cursor.fetchall()
cursor.close()
return render_template('show_search.html', posts=data)
if(stars_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE stars >= %s'
cursor.execute(query, int(stars_input))
data = cursor.fetchall()
cursor.close()
return render_template('show_search.html', posts=data)
if(fname_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE fname = %s'
cursor.execute(query, fname_input)
data = cursor.fetchall()
cursor.close()
return render_template('show_search.html', posts=data)
return render_template('home.html')
@app.route('/search_guest', methods=['GET','POST'])
def search_guest():
genre_input = request.form['genre']
stars_input = request.form['stars']
fname_input = request.form['fname']
cursor = conn.cursor()
if(stars_input and genre_input and fname_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE stars >= %s AND genre = %s AND fname = %s'
cursor.execute(query, (int(stars_input), genre_input, fname_input))
data = cursor.fetchall()
cursor.close()
return render_template('show_search_guest.html', posts=data)
if(genre_input and stars_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE stars >= %s AND genre = %s'
cursor.execute(query, (int(stars_input), genre_input))
data = cursor.fetchall()
cursor.close()
return render_template('show_search_guest.html', posts=data)
if(genre_input and fname_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE genre = %s AND fname = %s'
cursor.execute(query, (genre_input, fname_input))
data = cursor.fetchall()
cursor.close()
return render_template('show_search_guest.html', posts=data)
if(stars_input and fname_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE stars >= %s AND fname = %s'
cursor.execute(query, (stars_input, fname_input))
data = cursor.fetchall()
cursor.close()
return render_template('show_search_guest.html', posts=data)
if(genre_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE genre = %s'
cursor.execute(query, genre_input)
data = cursor.fetchall()
cursor.close()
return render_template('show_search_guest.html', posts=data)
if(stars_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE stars >= %s'
cursor.execute(query, int(stars_input))
data = cursor.fetchall()
cursor.close()
return render_template('show_search_guest.html', posts=data)
if(fname_input):
query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist WHERE fname = %s'
cursor.execute(query, fname_input)
data = cursor.fetchall()
cursor.close()
return render_template('show_search_guest.html', posts=data)
# query = 'SELECT songID, title, fname, lname, albumID FROM songInAlbum NATURAL JOIN song NATURAL JOIN rateSong NATURAL JOIN songGenre NATURAL JOIN artistPerformsSong NATURAL JOIN artist'
# cursor.execute(query)
# data = cursor.fetchall()
# cursor.close()
# return render_template('show_search_guest.html', posts=data)
return render_template('index.html')
@app.route('/rateSong', methods=['GET','POST'])
def rateSong():
username = session['username']
cursor = conn.cursor()
songID_input = request.form['Song']
rating_input = request.form['rateSong']
today = date.today()
query = 'INSERT INTO rateSong (username, songID, stars, ratingDate) VALUES(%s, %s, %s, %s)'
cursor.execute(query, (username, songID_input, int(rating_input), today))
conn.commit()
cursor.close()
return redirect(url_for('home'))
@app.route('/reviewSong', methods=['GET','POST'])
def reviewSong():
username = session['username']
cursor = conn.cursor()
songID_input = request.form['Song']
review_input = request.form['reviewSong']
today = date.today()
query = 'INSERT INTO reviewSong (username, songID, reviewText, reviewDate) VALUES(%s, %s, %s, %s)'
cursor.execute(query, (username, songID_input,review_input, today))
conn.commit()
cursor.close()
return redirect(url_for('home'))
@app.route('/reviewFriend', methods=['GET','POST'])
def reviewFriendFollower():
username_input = session['username']
cursor = conn.cursor()
query = 'SELECT reviewText, username FROM reviewSong, friend WHERE reviewSong.username <> %s AND (friend.user1 = %s OR friend.user2 = %s) AND friend.acceptStatus = "Accepted"'
cursor.execute(query, (username_input,username_input,username_input))
data = cursor.fetchall()
conn.commit()
cursor.close()
return render_template('reviewFriend.html', posts=data)
#Define route for login
@app.route('/login')
def login():
return render_template('login.html')
#Define route for register
@app.route('/register')
def register():
return render_template('register.html')
#Authenticates the login
@app.route('/loginAuth', methods=['GET', 'POST'])
def loginAuth():
#grabs information from the forms
username = request.form['username']
password = request.form['password']
# DIO THIS IS THE HASHING PART
password = password + 'salt'
password = hashlib.md5(password.encode()).hexdigest()
#cursor used to send queries
cursor = conn.cursor()
#executes query
query = 'SELECT * FROM user WHERE username = %s and password = %s'
cursor.execute(query, (username, password))
#stores the results in a variable
data = cursor.fetchone()
#use fetchall() if you are expecting more than 1 data row
cursor.close()
error = None
if(data):
#creates a session for the the user
#session is a built in
session['username'] = username
return redirect(url_for('home'))
else:
#returns an error message to the html page
error = 'Invalid login or username'
return render_template('login.html', error=error)
@app.route("/showFriendRequest", methods=['GET','POST'])
def show():
username=session['username']
cursor = conn.cursor()
query = 'SELECT requestSentBy FROM friend WHERE user2=%s AND acceptStatus="Pending"'
cursor.execute(query,(username))
data=cursor.fetchall()
conn.commit()
cursor.close()
return render_template('showFriendRequest.html',posts=data)
@app.route("/showsendFriendRequest", methods=['GET','POST'])
def showsendFriendRequest():
username=session['username']
cursor = conn.cursor()
query = 'SELECT username FROM user WHERE username!=%s'
cursor.execute(query,(username))
data=cursor.fetchall()
conn.commit()
cursor.close()
return render_template('showsendFriendRequest.html',poster_name=username,posts=data)
@app.route("/sendFriendRequest",methods=['POST'])
def sendFriendRequest():
username=session['username']
cursor=conn.cursor()
user2_input=request.form['user2']
status = "Pending"
query = 'INSERT INTO friend (user1, user2, acceptStatus, requestSentBy) values(%s, %s, %s, %s)'
cursor.execute(query,(username, user2_input,status,username))
data = cursor.fetchall()
conn.commit()
cursor.close()
return redirect(url_for('home'))
@app.route('/submit-data', methods=['POST'])
def submit_data():
username=session['username']
cursor=conn.cursor()
input_data = request.form['input-data']
submit_btn = request.form['submit-btn']
if submit_btn == 'accept':
# Store data in the database as accepted
query='UPDATE friend SET acceptStatus = %s WHERE user1 = %s AND user2 = %s'
cursor.execute(query,(submit_btn,input_data,username))
conn.commit()
else:
# Store data in the database as rejected
query='UPDATE friend SET acceptStatus = %s WHERE user1 = %s AND user2 = %s'
cursor.execute(query,(submit_btn,input_data,username))
conn.commit()
cursor.close()
return redirect(url_for('home'))
@app.route('/playlist', methods=['POST'])
def playlist():
username = session['username']
playlistName_input = request.form['playlistName']
songID_input = request.form['songID']
cursor = conn.cursor()
query = 'INSERT INTO playlist (username, playlistName, songID) VALUES(%s,%s,%s)'
cursor.execute(query, (username, playlistName_input, songID_input))
conn.commit()
cursor.close()
return render_template('home.html')
@app.route("/new")
def new():
username = session['username']
today=date.today()
# Get the user's friends and followers.
cursor = conn.cursor()
cursor.execute('SELECT * FROM friend WHERE user1 = %s', (username,))
friends = cursor.fetchall()
cursor.execute('SELECT * FROM follows WHERE follower = %s', (username,))
followers = cursor.fetchall()
# Get the list of new reviews.
cursor.execute('SELECT * FROM reviewSong WHERE reviewDate = %s', (today,))
new_reviews = cursor.fetchall()
# Get the list of new songs by artists the user is a fan of.
cursor.execute ('SELECT * FROM song WHERE releaseDate = %s', (today))
new_songs = cursor.fetchall()
# Close the cursor.
cursor.close()
# Render the new items of interest page.
return render_template("new.html", friends=friends, followers=followers, new_reviews=new_reviews, new_songs=new_songs)
#Authenticates the register
@app.route('/registerAuth', methods=['GET', 'POST'])
def registerAuth():
#grabs information from the forms
username = request.form['username']
password = request.form['password']
#cursor used to send queries
cursor = conn.cursor()
#executes query
query = 'SELECT * FROM user WHERE username = %s'
cursor.execute(query, (username))
#stores the results in a variable
data = cursor.fetchone()
#use fetchall() if you are expecting more than 1 data row
error = None
if(data):
#If the previous query returns data, then user exists
error = "This user already exists"
return render_template('register.html', error = error)
else:
# DIO THIS IS THE HASHING PART
password = password + 'salt'
password = hashlib.md5(password.encode()).hexdigest()
ins = 'INSERT INTO user VALUES(%s, %s)'
cursor.execute(ins, (username, password))
conn.commit()
cursor.close()
return render_template('index.html')
@app.route('/home')
def home():
user = session['username']
cursor = conn.cursor();
query = 'SELECT ts, blog_post FROM blog WHERE username = %s ORDER BY ts DESC'
song_query = 'SELECT DISTINCT title, songID FROM song'
cursor.execute(query,song_query)
data = cursor.fetchall()
cursor.execute(query, (user))
cursor.execute(song_query)
data = cursor.fetchall()
cursor.close()
return render_template('home.html', username=user, posts=data, songs=data )
@app.route('/artistFan', methods=['GET','POST'])
def artistFan():
username = session['username']
cursor = conn.cursor();
query = 'SELECT title, fname, lname FROM userFanOfArtist NATURAL JOIN artist NATURAL JOIN artistPerformsSong NATURAL JOIN song WHERE username = %s'
cursor.execute(query, username)
data = cursor.fetchall()
conn.commit()
cursor.close()
return render_template('artistFan.html', posts=data)
@app.route('/post', methods=['GET', 'POST'])
def post():
username = session['username']
cursor = conn.cursor();
blog = request.form['blog']
query = 'INSERT INTO blog (blog_post, username) VALUES(%s, %s)'
cursor.execute(query, (blog, username))
conn.commit()
cursor.close()
return redirect(url_for('home'))
@app.route('/select_blogger')
def select_blogger():
#check that user is logged in
#username = session['username']
#should throw exception if username not found
cursor = conn.cursor();
query = 'SELECT DISTINCT username FROM blog'
cursor.execute(query)
data = cursor.fetchall()
cursor.close()
return render_template('select_blogger.html', user_list=data)
@app.route('/show_posts', methods=["GET", "POST"])
def show_posts():
poster = request.args['poster']
cursor = conn.cursor();
query = 'SELECT ts, blog_post FROM blog WHERE username = %s ORDER BY ts DESC'
cursor.execute(query, poster)
data = cursor.fetchall()
cursor.close()
return render_template('show_posts.html', poster_name=poster, posts=data)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/', methods=['POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flash('File successfully uploaded')
return redirect('/')
else:
flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif')
return redirect(request.url)
@app.route('/logout')
def logout():
session.pop('username')
return redirect('/')
app.secret_key = 'some key that you will never guess'
#Run the app on localhost port 5000
#debug = True -> you don't have to restart flask
#for changes to go through, TURN OFF FOR PRODUCTION
if __name__ == "__main__":
app.run('127.0.0.1', 5000, debug = True)