-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to register, log in and add averages
- Loading branch information
Showing
8 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DATABASE_URL=postgresql+psycopg2:https:///matiason | ||
SECRET_KEY=2ffcaeeb61ba9a0b031090ef2604d975 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from flask import Flask | ||
from os import getenv | ||
from flask import render_template, redirect, request, session | ||
from flask_sqlalchemy import SQLAlchemy | ||
from werkzeug.security import check_password_hash, generate_password_hash | ||
|
||
|
||
app = Flask(__name__) | ||
app.secret_key = getenv("SECRET_KEY") | ||
app.config["SQLALCHEMY_DATABASE_URI"] = getenv("DATABASE_URL") | ||
db = SQLAlchemy(app) | ||
|
||
@app.route("/") | ||
def index(): | ||
return redirect("/mainpage") | ||
|
||
@app.route("/register", methods = ["POST", "GET"]) | ||
def register(): | ||
|
||
if request.method == "GET": | ||
return render_template("register.html") | ||
if request.method == "POST": | ||
username = request.form["username"] | ||
password = request.form["password"] | ||
sql = "INSERT INTO users (username, password) VALUES (:username, :password)" | ||
hash_value = generate_password_hash(password) | ||
db.session.execute(sql, {"username": username, "password": hash_value}) | ||
db.session.commit() | ||
return redirect("/") | ||
|
||
@app.route("/login", methods = ["POST", "GET"]) | ||
def login(): | ||
|
||
if request.method == "GET": | ||
return render_template("login.html") | ||
if request.method == "POST": | ||
username = request.form["username"] | ||
password = request.form["password"] | ||
session["username"] = username | ||
sql = "SELECT id, password FROM users WHERE username=:username" | ||
result = db.session.execute(sql, {"username": username}) | ||
user = result.fetchone() | ||
if not user: | ||
return render_template("login.html", error="Incorrect username or password") | ||
else: | ||
hash_value = user.password | ||
if check_password_hash(hash_value, password): | ||
return redirect("/") | ||
return redirect("/login") | ||
|
||
@app.route("/logout") | ||
def logout(): | ||
del session["username"] | ||
return redirect("/") | ||
|
||
@app.route("/mainpage", methods = ["POST", "GET"]) | ||
def mainpage(): | ||
if request.method == "GET": | ||
if session.get("username"): | ||
username = session["username"] | ||
user = db.session.execute("SELECT id FROM users WHERE username=:username", {"username": username}) | ||
user_id = user.fetchone()[0] | ||
averages = db.session.execute("SELECT average_date, average FROM averages WHERE user_id=:user_id", {"user_id": user_id}) | ||
return render_template("mainpage.html", avgs=averages) | ||
else: | ||
return render_template("mainpage.html") | ||
if request.method == "POST": | ||
average = request.form["addaverage"] | ||
username = session["username"] | ||
user = db.session.execute("SELECT id FROM users WHERE username=:username", {"username": username}) | ||
user_id = user.fetchone()[0] | ||
sql = "INSERT INTO averages (average_date, user_id, average) VALUES (current_date, :user_id, :average)" | ||
db.session.execute(sql, {"user_id": user_id, "average": average}) | ||
db.session.commit() | ||
return redirect("/") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CREATE TABLE users (id SERIAL PRIMARY KEY, username TEXT UNIQUE, password TEXT); | ||
CREATE TABLE averages (id SERIAL PRIMARY KEY, average_date DATE, user_id INTEGER REFERENCES users, average REAL); | ||
CREATE TABLE games (id SERIAL PRIMARY KEY, game_date DATE, player_one INTEGER REFERENCES users, player_two INTEGER REFERENCES users, result INTEGER); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<title>Darts scores</title> | ||
<h1>Darts scores</h1> | ||
{% if session.username %} | ||
<p>Olet kirjautunut nimellä {{ session.username }}</p> | ||
<a href="/logout">Kirjaudu ulos</a> | ||
{% else %} | ||
<form action="/register" method="POST"> | ||
<p>Tunnus:<br> | ||
<input type="text" name="username"></p> | ||
<p>Salasana:<br> | ||
<input type="password" name="password"></p> | ||
<input type="submit" value="Kirjaudu"> | ||
</form> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<title>Darts scores</title> | ||
<h1>Darts scores</h1> | ||
<p>{{ error }}</p> | ||
<form action="/login" method="POST"> | ||
<p>Username:<br> | ||
<input type="text" name="username"></p> | ||
<p>Password:<br> | ||
<input type="password" name="password"></p> | ||
<input type="submit" value="Log in"> | ||
</form> | ||
<p>Register a new account <a href="/register"><b>HERE</b></a></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{% if session.username %} | ||
<p>You are logged in as <b>{{ session.username }}</b> <a href="/logout">Log out</a></p> | ||
<form action="/mainpage" method="POST"> | ||
Add an average: <input type="number" step="0.1" min=0 max=180 name="addaverage"> | ||
<br> | ||
<input type="submit" value="Submit"> | ||
</form> | ||
<h2>Your averages:</h2> | ||
{% for avg in avgs %} | ||
<p>{{ avg.average_date }} {{ avg.average }}</p> | ||
{% endfor %} | ||
{% else %} | ||
<a href="/login">Log in</a> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<title>Darts scores</title> | ||
<h1>Darts scores</h1> | ||
<form action="/register" method="POST"> | ||
<p>Username:<br> | ||
<input type="text" name="username"></p> | ||
<p>Password:<br> | ||
<input type="password" name="password"></p> | ||
<input type="submit" value="Register"> | ||
</form> |