Skip to content

Commit

Permalink
Added ability to register, log in and add averages
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmatu committed Jan 27, 2022
1 parent 0e03dc6 commit d34802a
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env
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 added __pycache__/app.cpython-38.pyc
Binary file not shown.
75 changes: 75 additions & 0 deletions app.py
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("/")
4 changes: 4 additions & 0 deletions schema.sql
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);

14 changes: 14 additions & 0 deletions templates/index.html
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 %}
11 changes: 11 additions & 0 deletions templates/login.html
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>
14 changes: 14 additions & 0 deletions templates/mainpage.html
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 %}
9 changes: 9 additions & 0 deletions templates/register.html
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>

0 comments on commit d34802a

Please sign in to comment.