-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
293 lines (242 loc) · 9.49 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
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
import os
import sqlite3
from flask import Flask, render_template, request, session, redirect, url_for, flash
from passlib.hash import sha256_crypt
import db_edit
from util import routes, transit, music
app = Flask(__name__)
app.secret_key = os.urandom(8)
@app.route('/')
def home():
''' this function loads up home session, from where user can login and navigate through the website'''
#checks if there is a session
if 'user' in session:
#if there is then just show the welcome screen
return render_template('welcome.html',
user=session['user'])
else:
#if not just ask for info
return render_template('home.html')
@app.route('/login', methods=['POST', 'GET'])
def login():
'''logs the user in by checking if their login info matches with registered user'''
username = request.form['usr'].strip()
password = request.form['pwd'].strip()
user_exists = db_edit.findInfo('users', db_edit.checkApos(username), 'username', fetchOne = True)
'''find password for username'''
if user_exists:
# print (user_exists)
# print (password)
# print (sha256_crypt.verify(password, user_exists[2]))
if sha256_crypt.verify(password, user_exists[2]):
session['user'] = username
return redirect(url_for('home'))
else:
flash("Wrong password!")
return render_template('home.html')
else:
flash("Wrong username!")
return redirect(url_for('home'))
@app.route('/register', methods=['POST', 'GET'])
def register():
'''registers new account for user'''
password = request.form['new_pwd'].strip()
username= request.form['new_usr'].strip()
pwdCopy = request.form['re_pwd'].strip()
if username.find("'") == -1:
if password == pwdCopy:
# db_edit.insert('users', [username, sha256_crypt.encrypt(password), ""])
try:
db_edit.insert('users', [username, '', sha256_crypt.encrypt(password)])
'''insert username and password into database'''
flash("Registration complete! Please re-enter your login info.");
except:
flash("That username is taken. Please use a different one.");
else:
flash('Passwords do not match.')
else:
flash("Pick a username without apostrophes")
return redirect(url_for('home'))
@app.route('/route', methods=['POST', 'GET'])
def route():
start = request.form['start'].strip()
destination = request.form['destination'].strip()
mode = request.form['mode']
top_hit = music.get_top_tracks(1)
print ('---TOP HIT---')
print (top_hit)
if start and destination:
info = routes.getDirectionsInfo(start, destination, "shortest")
map = routes.get_maps(info)
distance = routes.get_distance(info)
# add driving to this
if mode == "Walking" or mode == "Driving" or mode == "Bicycle":
'''runs the route.py algorithm'''
'''ROUTE DIRECTIONS'''
if mode == "Walking":
try:
info = routes.getDirectionsInfo(start, destination, "pedestrian")
route = routes.get_directions(info)
except:
flash("You can't walk that far! Please select a different transportation type.")
return redirect(url_for('home'))
elif mode == "Bicycle":
info = routes.getDirectionsInfo(start, destination, "bicycle")
else:
info = routes.getDirectionsInfo(start, destination, "shortest")
route = routes.get_directions(info)
time = routes.get_time(info)
# time = time[3:5] + ' minutes and ' + time[7:9] + ' seconds'
# print ("-----ROUTE INFO-----")
# print (info)
else:
'''TRANSIT DIRECTIONS'''
info = transit.get_transit_info(start, destination)
route = []
for i in range (0, len(info)):
#per route found
one_route = {}
#in seconds
time = transit.get_total_time(info[i])
directions = transit.get_directions(info[i])
one_route['time'] = time
one_route['directions'] = directions
route.append(one_route)
# print ("----TRANSIT ROUTES----")
# print (route)
print("\n\n-----TOP HITS-----")
print(time)
print(top_hit)
return render_template('route.html',
mode=mode,
time=time,
distance=distance,
map=map,
routes=route,
top_hit = top_hit)
else:
flash("Please fill in all address forms.")
return redirect(url_for('home'))
@app.route('/play', methods=['POST', 'GET'])
def play():
'''runs the song algorithm'''
tags = request.form.get('tags')
s_tags = tags
tags = tags.split(',')
artist = request.form.get('artist')
transit_length = request.form.get('transit_length')
route_length = request.form.get('route_length')
if transit_length:
time = int(transit_length)
else:
time = int(route_length)
print ('---PLAY IS CALLED---')
print (type(time))
print (time)
# playlist = music.get_tracks_tagged(time, tags)
# if len(tags) == 0:
#
# playlist = music.get_tracks_tagged("None", "None", "None", time)
# elif len(tags) == 1:
# playlist = music.get_tracks_tagged(tags[0], "None", "None", time)
# elif len(tags) == 2:
# playlist = music.get_tracks_tagged(tags[0], tags[1], "None", time)
# else:
# playlist = music.get_tracks_tagged(tags[0], tags[1], tags[2], time)
if len(tags) == 0:
playlist = music.gen_playlist(time, "None", "None", "None")
elif len(tags) == 1:
playlist = music.gen_playlist(time, tags[0], "None", "None")
elif len(tags) == 2:
playlist = music.gen_playlist(time, tags[0], tags[1], "None")
else:
playlist = music.gen_playlist(time, tags[0], tags[1], tags[2])
# if len(tags) == 0:
# playlist = music.gen_playlist(time, "None", "None", "None")
# elif len(tags) == 1:
# playlist = music.gen_playlist(time, tags[0], "None", "None")
# elif len(tags) == 2:
# playlist = music.gen_playlist(time, tags[0], tags[1], "None")
# else:
# playlist = music.gen_playlist(time, tags[0], tags[1], tags[2])
length = len(playlist)
time = music.get_total_time(playlist)
print (playlist)
db_edit.insert('playlists', [time])
playlist_id = db_edit.findInfo('playlists', time, 'Songs')[0][0]
print ('play here')
print (playlist_id)
# print('---HERE---')
# print (playlists)
# print (transit_time)
# playlist = music.gen_playlist(time, tags)
for song in playlist:
songstuff = [playlist_id, song[2], song[1], song[0]]
print (songstuff)
db_edit.insert('songs', songstuff)
return render_template('play.html',
tags=s_tags,
playlist = playlist,
playlist_id = playlist_id,
time = time,
length = length
)
# Consider: Do we need an edit page?
#
@app.route('/edit', methods=['POST', 'GET'])
def edit():
'''displays the playlist with options to delete, select, and shuffle'''
# playlist = request.form['playlist']
playlist_id = request.form.get('save')
print ('---PLAYLIST LOOKS LIKE---')
print (playlist_id)
print ('-------------------------')
user = session['user']
# songs = db_edit.findInfo('songs',playlist_id,'PlaylistID')
# print ('--songs--')
# print (songs)
db_edit.modify('users', 'playlists', playlist_id, 'Username', user)
flash("Playlist has been saved!")
# getting songs and to display
return redirect(url_for('home'))
# return redirect(url_for('play')
@app.route('/profile', methods=['POST', 'GET'])
def profile():
user=session['user']
playlist_id = db_edit.findInfo('users', user, 'Username')[0][1]
print ('playlist_id')
print (playlist_id)
songs = db_edit.findInfo('songs',playlist_id,'PlaylistID')
return render_template('profile.html',
songs=songs,
playlist_id=playlist_id,
user=session['user'],
)
@app.route('/user_profile', methods=['POST', 'GET'])
def user_profile():
user = request.form['user']
playlist_id = db_edit.findInfo('users', user, 'Username')[0][1]
songs = db_edit.findInfo('songs',playlist_id,'PlaylistID')
# print ('--user profile--')
# print (user)
return render_template('user_profile.html',
username=user,
songs=songs,
playlist_id=playlist_id,
)
@app.route('/users', methods=['POST', 'GET'])
def users():
user = session['user']
users = db_edit.findInfo('users',user,'Username', notEqual = True)
print ('our users')
print (users)
return render_template('users.html',
users=users
)
@app.route('/logout')
def logout():
'''pops user from session, brings user back to home page'''
session.pop('user')
return redirect(url_for('home'))
if __name__ == "__main__":
app.run(debug=True)