-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
336 lines (261 loc) · 11.3 KB
/
application.py
File metadata and controls
336 lines (261 loc) · 11.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
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
from library50 import cs50
from flask import Flask, flash, redirect, render_template, request, session, url_for, jsonify
from flask_session import Session
from flask_jsglue import JSGlue
from passlib.apps import custom_app_context as pwd_context
from tempfile import gettempdir
from time import gmtime, strftime
import random
import string
import smtplib
import csv
import os
import urlparse
# import urllib.request
from functools import wraps
import sqlalchemy
import psycopg2
urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
# taken from CS50 Python Library due to issues with importing
class SQL(object):
def __init__(self, url):
try:
self.engine = sqlalchemy.create_engine(url)
except Exception as e:
raise RuntimeError(e)
def execute(self, text, *multiparams, **params):
try:
statement = sqlalchemy.text(text).bindparams(*multiparams, **params)
result = self.engine.execute(str(statement.compile(compile_kwargs={"literal_binds": True})))
# SELECT
if result.returns_rows:
rows = result.fetchall()
return [dict(row) for row in rows]
# INSERT
elif result.lastrowid is not None:
return result.lastrowid
# DELETE, UPDATE
else:
return result.rowcount
except sqlalchemy.exc.IntegrityError:
return None
except Exception as e:
raise RuntimeError(e)
# configure application
app = Flask(__name__)
JSGlue(app)
# 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"] = gettempdir()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# configure CS50 Library to use SQLite database
db = SQL(os.environ["DATABASE_URL"])
# app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://localhost/Anya'
# db = SQLAlchemy(app)
def login_required(f):
"""
Decorate routes to require login.
http://flask.pocoo.org/docs/0.11/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect(url_for("login", next=request.url))
return f(*args, **kwargs)
return decorated_function
@app.route("/index", methods=["GET", "POST"])
@login_required
def index():
"""Home page."""
# get todos for user
rows = db.execute("SELECT todo, category FROM todos WHERE user_id = :id ", id = session["user_id"])
# initialize index table
list1, list2, list3, list4 = [], [], [], []
#store table values
for i in range(len(rows)):
if rows[i]["category"] == 1:
list1.append(rows[i]["todo"])
if rows[i]["category"] == 2:
list2.append(rows[i]["todo"])
if rows[i]["category"] == 3:
list3.append(rows[i]["todo"])
if rows[i]["category"] == 4:
list4.append(rows[i]["todo"])
# return
return render_template("index.html", list1 = list1, list2 = list2, list3 = list3, list4 = list4)
@app.route("/")
def guide():
"""User's guide page"""
return render_template("guide.html")
@app.route("/login", methods=["GET", "POST"])
def login():
"""Log user in."""
# forget any user_id
session.clear()
# if user reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# ensure username was submitted
if not request.form.get("username"):
return render_template("login.html")
# ensure password was submitted
elif not request.form.get("password"):
return render_template("login.html")
# query database for username
rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
# ensure username exists and password is correct
if len(rows) != 1 or not pwd_context.verify(request.form.get("password"), rows[0]["hash"]):
return render_template("incorrect_pass.html")
# remember which user has logged in
session["user_id"] = rows[0]["id"]
# redirect user to home page
return redirect(url_for("index"))
# else if user reached route via GET (as by clicking a link or via redirect)
else:
return render_template("login.html")
@app.route("/forgot_pass", methods=["GET", "POST"])
def forgot_pass():
"""Send User Password"""
if request.method == "POST":
# ensure email was submitted
if not request.form.get("username"):
return render_template("forgot_pass.html")
#query database for username
rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
#ensure username exists
if len(rows) == 0:
return render_template("reset_failed.html")
#change password & send email
else:
new_pass = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(8))
db.execute("UPDATE users SET hash = :hash WHERE id = :id", hash = pwd_context.encrypt(new_pass), id = rows[0]["id"])
sender = 'prioritas.cs50@gmail.com'
receivers = [rows[0]["username"]]
message = """Subject: Password Reset
\nHi! Your new password is below.
\nNew Password: {}
\nPlease login with your reset password. If you wish to change this temporary password, click "change password" once you are logged in.
""".format(new_pass)
try:
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
type(smtpObj)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(' prioritas.cs50@gmail.com ', ' prioritas ')
smtpObj.sendmail(sender, receivers, message)
smtpObj.quit()
return render_template("reset_success.html")
except smtplib.SMTPException:
return render_template("reset_failed.html")
else:
return render_template("forgot_pass.html")
@app.route("/incorrect_pass", methods=["GET", "POST"])
def incorrect_pass():
"""Display for incorrect passwords."""
if request.method == "POST":
return login()
else:
return render_template("incorrect_pass.html")
@app.route("/logout")
def logout():
"""Log user out."""
# forget any user_id
session.clear()
# redirect user to login form
return redirect(url_for("login"))
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user."""
# forget any user_id
session.clear()
# if user reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# query database for username
rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
# ensure username does not already exist
if len(rows) != 0:
return render_template("username_taken.html")
# add user to users
db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)", username=request.form.get("username"), hash = pwd_context.encrypt(request.form["password"]))
# update existing session
rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
session["user_id"] = rows[0]["id"]
# Let user know registration successful
return render_template("register_success.html")
# else if user reached route via GET (as by clicking a link or via redirect)
else:
return render_template("register.html")
@app.route("/password", methods=["GET", "POST"])
@login_required
def password():
"""Change password."""
# if user reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# ensure old password was submitted
if not request.form.get("old"):
return apology("please provide current password")
# ensure old password is correct
rows = db.execute("SELECT * FROM users WHERE id = :id", id = session["user_id"])
if not pwd_context.verify(request.form.get("old"), rows[0]["hash"]):
return apology("Current password incorrect")
# ensure new password was submitted
elif not request.form.get("new") or not request.form.get("new2"):
return apology("must provide new password")
# ensure new passwords are equal
elif not request.form.get("new") == request.form.get("new2"):
return apology("passwords don't match")
# update database
db.execute("UPDATE users SET hash = :hash WHERE id = :id", hash = pwd_context.encrypt(request.form.get("new2")), id = session["user_id"])
# redirect user to home page
return redirect(url_for("index"))
# else if user reached route via GET (as by clicking a link or via redirect)
else:
return render_template("password.html")
@app.route("/saveTodo")
def saveTodo():
"""Save user-inputted task to SQL."""
# add todo & corresponding data to table
db.execute("INSERT INTO todos (user_id, todo, category) VALUES (:id, :todo, :category)", id=session["user_id"], todo=request.args.get("todo"), category=request.args.get("category"))
return jsonify(dict());
@app.route("/removeTodo")
def removeTodo():
"""Delete task from SQL."""
# remove todo & corresponding data from table
db.execute("DELETE FROM todos WHERE (user_id = :id AND todo = :todo AND category = :category)", id=session["user_id"], todo=request.args.get("todo"), category=request.args.get("category"))
return jsonify(dict());
@app.route("/updateTime")
def updateTime():
# update number of pomorodos for todo
db.execute("UPDATE todos SET pomodoros = :pomodoros WHERE (user_id = :id AND todo = :todo AND category = :category)", pomodoros=request.args.get("pomodoros"), id=session["user_id"], todo=request.args.get("todo"),
category=request.args.get("category"))
return jsonify(dict());
@app.route("/getTime")
def getTime():
# get number of pomodoros for todo
time = db.execute("SELECT pomodoros FROM todos WHERE (user_id = :id AND todo = :todo AND category = :category)", id=session["user_id"], todo=request.args.get("todo"),
category=request.args.get("category"))
return jsonify(time)
if __name__ == '__main__':
app.debug = True
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
@app.route("/googlea61637b83bd80dce.html")
def googleVerify():
"""Verify ownership for Google Search Console"""
return render_template("googlea61637b83bd80dce.html")