forked from Harvard-WalkWithMe/WalkWithMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.~c9_invoke_Ascwvp.py
357 lines (286 loc) · 13.6 KB
/
.~c9_invoke_Ascwvp.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
#RUN BEFORE OPERATING FLASK:
#export API_KEY=AIzaSyBBd2ui_STN7z6EyoawLpQ9oS8PZ-0stN8
import time
import requests
import json, random
import os
import re
from cs50 import SQL, eprint
from flask import Flask, flash, redirect, render_template, request, session, Response, jsonify
from flask_session import Session
from tempfile import mkdtemp
from werkzeug.exceptions import default_exceptions
from werkzeug.security import check_password_hash, generate_password_hash
from helpers import apology, login_required
from datetime import datetime
location = None;
destination = None;
arrivalTime = None;
# API Key
key = "AIzaSyDMhsvLB5Sa0jizEcPExguTmTPLyDi_fNU"
# Configure applicxation
app = Flask(__name__)
# Ensure responses aren't cached
if app.config["DEBUG"]:
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# set the secret key. keep this really secret:
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///walk.db")
# configure global variable for username, set to null
db = SQL("sqlite:///walk.db")
# my_name = None
# global current_request
# current_request = None
# global matches
# matches = None
@app.route("/")
def index():
"""Homepage"""
return render_template("index.html")
@app.route("/login", methods=["GET", "POST"])
def login():
"""Log user in"""
# Forget any user_id
session.clear()
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# lowercase username
username = request.form.get("username").lower()
# Ensure username was submitted
if not request.form.get("username"):
return apology("must provide username", 400)
# Ensure password was submitted
elif not request.form.get("password"):
return apology("must provide password", 400)
# Query database for username
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=username)
# Ensure username exists and password is correct
if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
return apology("invalid username and/or password", 400)
# Remember which user has logged in
session["user_id"] = rows[0]["user_id"]
session["my_name"] = username
# Redirect user to home page
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("login.html")
@app.route("/logout")
def logout():
"""Log user out"""
# Forget any user_id
session.clear()
# Redirect user to login form
return redirect("/")
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# lowercase username
username = request.form.get("username").lower()
# Ensure username was submitted and ≠ users
if not request.form.get("username") or username == "users":
return apology("must provide new username", 400)
# Ensure password was submitted
elif not request.form.get("password"):
return apology("must provide password", 400)
# Ensure confirmation was submitted
elif not request.form.get("confirmation"):
return apology("must provide password again", 400)
# Ensure terms are agreed to
elif not request.form.get("terms"):
return apology("must agree to Terms & Conditions", 400)
# Ensure password = confirmation
elif request.form.get("password") != request.form.get("confirmation"):
return apology("must provide matching passwords", 400)
# Generate hash for password
hash = generate_password_hash(request.form.get("password"))
# Check to see if username already exists
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=username)
if len(rows) != 0:
return apology("username already taken", 400)
# Insert new user into database into master users list
new_user_id = db.execute("INSERT INTO users (username, hash, first_name, last_name) VALUES (:username, :hash, :first, :last)",
username=username, hash=hash, first=request.form.get("first").title(), last=request.form.get("last").title())
# Query database for userid
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=username)
# Remember which user has logged in
session["user_id"] = rows[0]["user_id"]
# Forget any user_id
session.clear()
# Redirect user to login page
return redirect("/login")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("register.html")
@app.route("/account", methods=["GET", "POST"])
@login_required
def account():
"""Account settings"""
print(request.form)
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
if request.form['submission'] == "change":
# Ensure password was submitted
if not request.form.get("password"):
return flash("You must provide a password")
# Ensure confirmation was submitted
elif not request.form.get("confirmation"):
return flash("You must provide your password again")
# Ensure password = confirmation
elif request.form.get("password") != request.form.get("confirmation"):
return flash("Passwords do not match")
# Generate hash for password
hash = generate_password_hash(request.form.get("password"))
# update password in database
update = db.execute("UPDATE users SET hash = :hash WHERE user_id = :id",
id=session["user_id"], hash=hash)
if update:
# Redirect user to login page
session["user_id"] = None
flash("Your password has been successfully changed!")
return redirect("/")
else:
return apology("Error", 400)
# if attempting to delete account
elif request.form['submission'] == "delete":
# ensure confirmation box is checked
if not request.form.get("deletecheck"):
flash("Check the box below if you would like to delete your account.")
# delete account (remove user from master users and remove usertable)
else:
# remove user from master users table
delete = db.execute("DELETE FROM users WHERE user_id = :user", user=session["user_id"])
# remove all rows that include user in friends
friendslist = db.execute("DELETE FROM friends WHERE friend = :name OR user = :name", name=session["my_name"])
# Forget any user_id
session.clear()
# Redirect to homepage
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
return render_template("change.html")
@app.route("/map", methods = ['POST', 'GET'])
@login_required
def map():
"""map"""
if not key:
raise RuntimeError("API_KEY not set")
return render_template("map.html", key = key)
@app.route("/friends", methods=["GET", "POST"])
@login_required
def friends():
"""friends"""
# Query for list of friends
friends = db.execute("SELECT friend FROM friends WHERE user = :name", name=session["my_name"])
# initiate list of friends
friendslist = list()
# for each item in sql output, find which is friend and add to friendslist
for row in friends:
# query for all information from users about each, append to list to send to send to jinja/html in friends.html
friend = db.execute("SELECT username, destination, dep_time, location FROM users WHERE username = :name", name=row["friend"])
friendslist.append(friend[0])
# collect current_request
current = db.execute("SELECT destination, dep_time, location FROM users WHERE user_id = :id", id=session["user_id"])
global current_request
current_request = current[0]
# # compare friends list with current_request to see if any have same destination or time, respectively. matches is list of all users on users friend list who has same destination and time
# if current_request and friendslist:
# global matches
# matches = list()
# i = 0
# for friend in friendslist:
# if (friendslist[i]['destination'] == current_request['destination']) and (friendslist[i]['location'] == current_request['location']) and (friendslist[i]['dep_time'] == current_request['dep_time']):
# matches.append(friend[i]['username'])
# i+=1
# User reached route via POST (as by submitting a form via GET)
if request.method == "POST":
# Ensure username was submitted
if not request.form.get("newfriend"):
flash("You must provide a username to add!")
# ensure username exists
newfriend = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("newfriend").lower())
if len(newfriend) != 1:
flash("Username does not exist!")
elif newfriend[0]["user_id"] == session["user_id"]:
flash("You cannot add yourself!")
else:
# check to see if you are on their list
check = db.execute("SELECT * FROM friends WHERE user = :friend_name AND friend = :my_name", friend_name=request.form.get("newfriend").lower(), my_name=session["my_name"])
if len(check) != 0:
newusername = request.form.get("newfriend").lower()
flash(f"You're already friends with {newusername}!")
else:
# add to list
addfriend = db.execute("INSERT INTO friends (friend, user) VALUES (:friend, :username)",
friend=request.form.get("newfriend").lower(), username=session["my_name"])
if not addfriend:
newusername = request.form.get("newfriend").lower()
flash(f"You've already added {newusername}!")
# add to list
addfriend = db.execute("INSERT INTO friends (user, friend) VALUES (:friend, :username)",
friend=request.form.get("newfriend").lower(), username=session["my_name"])
if not addfriend:
newusername = request.form.get("newfriend").lower()
flash(f"You've already added {newusername}!")
return redirect("/friends")
return render_template("friends.html", friends=friendslist)
def errorhandler(e):
"""Handle error"""
return apology(e.name, e.code)
@app.route("/matches")
def matches():
"""Look up friend matches"""
#global my_name
info = db.execute("SELECT * FROM users WHERE username = :user", user = session["my_name"])
friends = db.execute("SELECT * FROM users JOIN friends ON friends.user = users.username WHERE friends.friend = :user AND users.destination = :destination AND users.dep_time = :time"
, user = session["my_name"], destination = info[0]["destination"], time = info[0]["dep_time"])
return jsonify(friends)
@app.route("/position")
def position():
"""Look up friend matches"""
global my_name
info = db.execute("SELECT * FROM users WHERE username = :user", user = session["my_name"])
return jsonify(info)
@app.route("/request", methods=["GET", "POST"])
@login_required
def order():
"""request"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
if request.form['submission'] == "request":
if not request.form.get("location"):
flash("You must provide where you are")
if not request.form.get("destination"):
flash("You must provide a destination")
if not request.form.get("arrival"):
flash("You must provide an arrival time")
update = db.execute("UPDATE users SET destination = :destination, dep_time = :arrival, location = :location WHERE user_id = :id",
id=session["user_id"], destination = request.form.get("destination"), arrival=request.form.get("arrival"), location = request.form.get("location"))
location = request.form.get("location")
destination = request.form.get("destination")
arrivalTime = request.form.get("arrival")
return redirect("/map")
if request.form['submission'] == "clear":
update = db.execute("UPDATE users SET destination = NULL, dep_time = NULL WHERE user_id = :id", id=session["user_id"])
select = db.execute("SELECT destination, dep_time FROM users WHERE user_id = :id", id=session["user_id"])
destination = select[0]['destination']
arrival = select[0]['dep_time']
return render_template("request.html", destination=destination, arrival=arrival)
# listen for errors
for code in default_exceptions:
app.errorhandler(code)(errorhandler)