Skip to content

Commit

Permalink
Users are now able to register
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmaclaren committed Mar 27, 2024
1 parent 9a83251 commit 498af37
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
38 changes: 30 additions & 8 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import sqlite3

from flask import Flask, render_template, request, session, redirect
from helpers import login_required
from flask_session import Session
from helpers import login_required, apology
from werkzeug.security import check_password_hash, generate_password_hash
import sqlite3

db = sqlite3.connect('workout.db', check_same_thread=False)
app = Flask(__name__)

app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

conn = sqlite3.connect('workout.db', check_same_thread=False)
db = conn.cursor()
db.execute("""CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
username TEXT NOT NULL,
hash TEXT NOT NULL)""")

app = Flask(__name__)
conn.commit()

@app.after_request
def after_request(response):
"""Ensure responses aren't cached"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response

@app.route("/")
def home():
Expand All @@ -34,11 +50,15 @@ def register():
elif request.form.get("password") != request.form.get("confirmation"):
return apology("passwords must match", 400)

# if username is already taken
rows = db.execute(
"SELECT * FROM users WHERE username = ?", (request.form.get("username"),)
)
conn.commit()

if len(rows) >= 1:
data = rows.fetchall()

if data:
return apology(
"username is already taken, please use another username", 400
)
Expand All @@ -47,11 +67,13 @@ def register():

new_user = db.execute(
"INSERT INTO users (username, hash) VALUES (?, ?)",
request.form.get("username"),
hash_password
(request.form.get("username"),
hash_password,)
)
conn.commit()


session["user_id"] = new_user
"""session["user_id"] = new_user"""

return redirect("/")

Expand Down
27 changes: 26 additions & 1 deletion helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
from functools import wraps
from flask import request, redirect, url_for
from flask import Flask, render_template, request, redirect, url_for

def apology(message, code=400):
"""Render message as an apology to user."""

def escape(s):
"""
Escape special characters.
https://github.com/jacebrowning/memegen#special-characters
"""
for old, new in [
("-", "--"),
(" ", "-"),
("_", "__"),
("?", "~q"),
("%", "~p"),
("#", "~h"),
("/", "~s"),
('"', "''"),
]:
s = s.replace(old, new)
return s

return render_template("apology.html", top=code, bottom=escape(message)), code


def login_required(f):
@wraps(f)
Expand Down
10 changes: 10 additions & 0 deletions templates/apology.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "layout.html" %}

{% block title %}
Apology
{% endblock %}

{% block main %}
<!-- https://memegen.link/ -->
<img alt="{{ top }}" class="border img-fluid" src="http://memegen.link/custom/{{ top | urlencode }}/{{ bottom | urlencode }}.jpg?alt=https://i.imgur.com/CsCgN7Ll.png&width=400" title="{{ top }}">
{% endblock %}

0 comments on commit 498af37

Please sign in to comment.