-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
203 lines (157 loc) · 7.23 KB
/
application.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
import os
import math
from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session
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 passlib.apps import custom_app_context as pwd_context
import datetime
from helpers import apology, login_required
# Configure application
app = Flask(__name__)
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
# Ensure responses aren't cached
@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)
# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///hours.db")
@app.route("/register", methods=["GET", "POST"])
def register():
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure password was submitted
if request.form.get("password") != request.form.get("confirmation"):
return apology("passwords do not match", 403)
result = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hashed)", username = request.form.get("username"), hashed = generate_password_hash(request.form.get("password")))
if not result:
return apology("username already exists, try a different one", 403)
session["user_id"] = result
return render_template("login.html")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("register.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":
# 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 check_password_hash(rows[0]["hash"], request.form.get("password")):
return apology("invalid username and/or password", 403)
# Remember which user has logged in
session["user_id"] = rows[0]["id"]
# Redirect user to home page
return redirect("/dash")
# 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("/")
def index():
return render_template("title.html")
@app.route("/dash")
@login_required
def dash():
activities = db.execute("SELECT * FROM activities WHERE id = :id", id=session["user_id"])
hour = []
Day = []
for activities in activities:
for i in reversed(range(7)):
day = datetime.datetime.now() - datetime.timedelta(days = i)
check = db.execute("SELECT * FROM log WHERE id = :id AND activity = :activity AND date = :date", id=session["user_id"], activity=activities["activity"], date=day)
if not check:
hour.append(0)
else:
hour.append(int(check[0]["hours"]))
Day.append(day)
print(hour)
activities = db.execute("SELECT * FROM activities WHERE id = :id", id=session["user_id"])
return render_template("dash.html", rows=activities, array = hour, day = Day)
@app.route("/new", methods=["GET", "POST"])
@login_required
def new():
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
activity = request.form.get("name")
goal = int(request.form.get("goal"))
curAct = db.execute("SELECT * FROM activities WHERE id = :id AND activity = :activity", id=session["user_id"], activity=activity)
if curAct:
return apology("You have already created this activity", 403)
else:
db.execute("INSERT INTO activities (activity, goal, id) VALUES(:activity, :goal, :id)", activity=activity, goal = goal, id=session["user_id"])
# Redirect user to home page
return redirect("/log")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("new.html")
@app.route("/log", methods=["GET", "POST"])
@login_required
def log():
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
activity = request.form.get("active")
if activity == "":
return apology("Please select an activity", 403)
if request.form.get("hour") == "":
hour = 0
else:
hour = int(request.form.get("hour"))
if request.form.get("minutes") == "":
minutes = 0
else:
minutes = int(request.form.get("minutes"))
time = hour + (minutes/60)
curLog = db.execute("SELECT * FROM log WHERE id = :id AND activity = :activity AND date=:date", id=session["user_id"], activity=activity, date= datetime.date.today())
if not curLog:
db.execute("INSERT INTO log (id, activity, hours) VALUES (:id, :activity, :hours)", id = session["user_id"], activity=activity, hours=time)
else:
totHours = curLog[0]["hours"] + time
db.execute("UPDATE log SET hours=:hours WHERE id = :id AND activity = :activity AND date=:date", hours=totHours, id = session["user_id"], activity=activity, date= datetime.date.today())
total = db.execute("SELECT * FROM activities WHERE id = :id AND activity = :activity", id = session["user_id"], activity=activity)
if total[0]["total"] is None:
totalHours = time
else:
totalHours = total[0]["total"] + time
totalHours = round(totalHours,2)
db.execute("UPDATE activities SET total=:total WHERE id = :id AND activity = :activity", total=totalHours, id = session["user_id"], activity=activity)
db.execute("INSERT INTO history (id, activity, hours, minutes) VALUES(:id, :activity, :hours, :minutes)", id=session["user_id"], activity=activity, hours=hour, minutes=minutes)
# Redirect user to home page
return redirect("/dash")
# User reached route via GET (as by clicking a link or via redirect)
else:
activities = db.execute("SELECT * FROM activities WHERE id = :id", id=session["user_id"])
return render_template("log.html", rows=activities)
@app.route("/history")
@login_required
def history():
row = db.execute("SELECT * FROM history WHERE id = :ids", ids = session["user_id"])
return render_template("history.html", row = reversed(row))
def errorhandler(e):
"""Handle error"""
return apology(e.name, e.code)
# listen for errors
for code in default_exceptions:
app.errorhandler(code)(errorhandler)