|
| 1 | +from cs50 import SQL |
| 2 | +from flask import Flask, flash, redirect, render_template, request, session, url_for |
| 3 | +from flask_session import Session |
| 4 | +from passlib.apps import custom_app_context as pwd_context |
| 5 | +from tempfile import mkdtemp |
| 6 | +from passlib.context import CryptContext |
| 7 | +from helpers import * |
| 8 | +import helpers |
| 9 | + |
| 10 | +# configure application |
| 11 | +app = Flask(__name__) |
| 12 | + |
| 13 | +# ensure responses aren't cached |
| 14 | +if app.config["DEBUG"]: |
| 15 | + @app.after_request |
| 16 | + def after_request(response): |
| 17 | + response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" |
| 18 | + response.headers["Expires"] = 0 |
| 19 | + response.headers["Pragma"] = "no-cache" |
| 20 | + return response |
| 21 | + |
| 22 | +# custom filter |
| 23 | +app.jinja_env.filters["usd"] = usd |
| 24 | + |
| 25 | +# configure session to use filesystem (instead of signed cookies) |
| 26 | +app.config["SESSION_FILE_DIR"] = mkdtemp() |
| 27 | +app.config["SESSION_PERMANENT"] = False |
| 28 | +app.config["SESSION_TYPE"] = "filesystem" |
| 29 | +Session(app) |
| 30 | + |
| 31 | +# configure CS50 Library to use SQLite database |
| 32 | +db = SQL("sqlite:///finance.db") |
| 33 | + |
| 34 | +@app.route("/") |
| 35 | +@login_required |
| 36 | +def index(): |
| 37 | + |
| 38 | + return render_template("index.html") |
| 39 | + |
| 40 | +@app.route("/regs") |
| 41 | +def regs(): |
| 42 | + rows = db.execute("SELECT * from users") |
| 43 | + return render_template("registrants.html", registrants=rows) |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +@app.route("/unregister", methods=["GET", "POST"]) |
| 48 | +def unregister(): |
| 49 | + if request.method == "GET": |
| 50 | + rows = db.execute("SELECT * from users") |
| 51 | + return render_template("unregister.html", registrants=rows) |
| 52 | + elif request.method == "POST": |
| 53 | + if request.form["id"]: |
| 54 | + id=request.form.get("id") |
| 55 | + db.execute("DELETE FROM users WHERE id=:id;", id=id) |
| 56 | + return redirect(url_for("regs")) |
| 57 | + |
| 58 | + |
| 59 | +@app.route("/play") |
| 60 | +@login_required |
| 61 | +def play(): |
| 62 | + """Show history of transactions.""" |
| 63 | + return render_template("play.html") |
| 64 | + |
| 65 | +@app.route("/leaderboard") |
| 66 | +@login_required |
| 67 | +def play2(): |
| 68 | + """Show history of transactions.""" |
| 69 | + return render_template("play2.html") |
| 70 | + |
| 71 | + |
| 72 | +@app.route("/login", methods=["GET", "POST"]) |
| 73 | +def login(): |
| 74 | + """Log user in.""" |
| 75 | + |
| 76 | + # forget any user_id |
| 77 | + session.clear() |
| 78 | + |
| 79 | + # if user reached route via POST (as by submitting a form via POST) |
| 80 | + if request.method == "POST": |
| 81 | + |
| 82 | + # ensure username was submitted |
| 83 | + if not request.form.get("username"): |
| 84 | + return apology("must provide username") |
| 85 | + |
| 86 | + # ensure password was submitted |
| 87 | + elif not request.form.get("password"): |
| 88 | + return apology("must provide password") |
| 89 | + |
| 90 | + # query database for username |
| 91 | + rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username")) |
| 92 | + |
| 93 | + # ensure username exists and password is correct |
| 94 | + if len(rows) != 1 or not pwd_context.verify(request.form.get("password"), rows[0]["hash"]): |
| 95 | + return apology("invalid username and/or password") |
| 96 | + |
| 97 | + # query database for username |
| 98 | + db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username")) |
| 99 | + # remember which user has logged in |
| 100 | + session["user_id"] = rows[0]["id"] |
| 101 | + |
| 102 | + flash('Welcome back!') |
| 103 | + # redirect user to home page |
| 104 | + return redirect(url_for("index")) |
| 105 | + |
| 106 | + # else if user reached route via GET (as by clicking a link or via redirect) |
| 107 | + else: |
| 108 | + return render_template("login.html") |
| 109 | + |
| 110 | +@app.route("/logout") |
| 111 | +def logout(): |
| 112 | + """Log user out.""" |
| 113 | + |
| 114 | + # forget any user_id |
| 115 | + session.clear() |
| 116 | + |
| 117 | + # redirect user to login form |
| 118 | + return redirect(url_for("login")) |
| 119 | + |
| 120 | + |
| 121 | +@app.route("/register", methods=["GET", "POST"]) |
| 122 | +def register(): |
| 123 | + # forget any user_id |
| 124 | + session.clear() |
| 125 | + # if user reached route via POST (as by submitting a form via POST) |
| 126 | + if request.method == "POST": |
| 127 | + # ensure username was submitted |
| 128 | + if not request.form.get("username"): |
| 129 | + return apology("must provide username") |
| 130 | + # ensure password was submitted |
| 131 | + elif not request.form.get("password"): |
| 132 | + return apology("must provide password") |
| 133 | + # ensure password was submitted |
| 134 | + elif not request.form.get("password2"): |
| 135 | + return apology("must provide password") |
| 136 | + # ensure password was submitted |
| 137 | + if request.form.get("password") != request.form.get("password2"): |
| 138 | + return apology("passwords should be the same!") |
| 139 | + # query database for username |
| 140 | + rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username")) |
| 141 | + # ensure username exists and password is correct |
| 142 | + if len(rows) == 1: |
| 143 | + return apology("username exists") |
| 144 | + # ensure doesn't exist |
| 145 | + if len(rows) != 1: |
| 146 | + #encrypt pass and save them to db |
| 147 | + encrypted=pwd_context.encrypt(request.form.get("password")) |
| 148 | + db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)",username=request.form.get("username"),hash=encrypted) |
| 149 | + # query database for username |
| 150 | + rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username")) |
| 151 | + # remember which user has logged in |
| 152 | + session["user_id"] = rows[0]["id"] |
| 153 | + # redirect user to home page |
| 154 | + return redirect(url_for("index")) |
| 155 | + # else if user reached route via GET (as by clicking a link or via redirect) |
| 156 | + else: |
| 157 | + return render_template("register.html") |
| 158 | + |
| 159 | +@app.route("/change", methods=["GET", "POST"]) |
| 160 | +@login_required |
| 161 | +def change(): |
| 162 | + #return apology("TODO") |
| 163 | + id = session["user_id"] |
| 164 | + # if user reached route via POST (as by submitting a form via POST) |
| 165 | + if request.method == "POST": |
| 166 | + |
| 167 | + # ensure password was submitted |
| 168 | + if not request.form.get("password"): |
| 169 | + return apology("must provide the old password") |
| 170 | + |
| 171 | + # query database for username |
| 172 | + rows = db.execute("SELECT * FROM users WHERE id = :id", id=id) |
| 173 | + |
| 174 | + # ensure username exists and password is correct |
| 175 | + if len(rows) != 1 or not pwd_context.verify(request.form.get('password'), rows[0]['hash']): |
| 176 | + return apology("old password invalid") |
| 177 | + |
| 178 | + # ensure password2 was submitted |
| 179 | + elif not request.form.get("password2"): |
| 180 | + return apology("must provide the new password") |
| 181 | + |
| 182 | + encrypted2=pwd_context.encrypt(request.form.get("password2")) |
| 183 | + |
| 184 | + |
| 185 | + db.execute("UPDATE users SET hash=:hashes WHERE id=:id",id=id, hashes=encrypted2) |
| 186 | + |
| 187 | + return redirect(url_for('logout')) |
| 188 | + |
| 189 | + # else if user reached route via GET (as by clicking a link or via redirect) |
| 190 | + else: |
| 191 | + return render_template("change.html") |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | +@app.route("/uchange", methods=["GET", "POST"]) |
| 196 | +@login_required |
| 197 | +def uchange(): |
| 198 | + #return apology("TODO") |
| 199 | + id = session["user_id"] |
| 200 | + |
| 201 | + if request.method == "POST": |
| 202 | + |
| 203 | + # ensure password was submitted |
| 204 | + if not request.form.get("name"): |
| 205 | + return apology("must provide the old name") |
| 206 | + |
| 207 | + # query database for username |
| 208 | + rows = db.execute("SELECT * FROM users WHERE id = :id", id=id) |
| 209 | + |
| 210 | + # ensure username exists and password is correct |
| 211 | + if len(rows) != 1 or not request.form.get('name') == rows[0]['username']: |
| 212 | + return apology("old username invalid") |
| 213 | + |
| 214 | + # ensure password2 was submitted |
| 215 | + elif not request.form.get("name2"): |
| 216 | + return apology("must provide the new username") |
| 217 | + |
| 218 | + # query database for username |
| 219 | + rowss = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("name2")) |
| 220 | + # ensure username exists and password is correct |
| 221 | + if len(rowss) == 1: |
| 222 | + return apology("username already taken") |
| 223 | + |
| 224 | + else: |
| 225 | + db.execute("UPDATE users SET username=:username WHERE id=:id",id=id, username=request.form.get('name2')) |
| 226 | + |
| 227 | + return redirect(url_for('logout')) |
| 228 | + |
| 229 | + # else if user reached route via GET (as by clicking a link or via redirect) |
| 230 | + else: |
| 231 | + return render_template("uchange.html") |
0 commit comments