commit 54ffb721ddf963e0b47dc560d444ee5da71586f7 Author: Milan Martin Jäckel Date: Sat Jun 13 17:48:59 2026 +0200 Inital commit, working non plished website diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..29b9070 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.venv/ +__pycache__/ +*.pyc +questions.db +.env +.vscode/ +.idea/ diff --git a/app.py b/app.py new file mode 100644 index 0000000..97522e4 --- /dev/null +++ b/app.py @@ -0,0 +1,135 @@ +from flask import Flask, render_template, request, redirect +from datetime import date +import sqlite3 +import hashlib + +app = Flask(__name__) + +DB = "questions.db" + + +def get_db(): + return sqlite3.connect(DB) + + +@app.route("/") +def home(): + + conn = get_db() + cur = conn.cursor() + + cur.execute(""" + SELECT * + FROM questions + ORDER BY id DESC + LIMIT 1 + """) + + question = cur.fetchone() + + conn.close() + + return render_template( + "index.html", + question=question + ) + + +@app.route("/vote", methods=["POST"]) +def vote(): + + qid = request.form["qid"] + vote = request.form["vote"] + + ip = request.remote_addr + + voter_hash = hashlib.sha256( + ip.encode() + ).hexdigest() + + conn = get_db() + cur = conn.cursor() + + cur.execute(""" + SELECT * + FROM votes + WHERE voter_hash = ? + AND question_id = ? + """, (voter_hash, qid)) + + existing = cur.fetchone() + + if not existing: + + cur.execute(""" + INSERT INTO votes + (question_id, vote, voter_hash) + VALUES (?, ?, ?) + """, (qid, vote, voter_hash)) + + conn.commit() + + conn.close() + + return redirect("/results") + + +@app.route("/results") +def results(): + + conn = get_db() + cur = conn.cursor() + + cur.execute(""" + SELECT * + FROM questions + ORDER BY id DESC + LIMIT 1 + """) + + question = cur.fetchone() + + qid = question[0] + + cur.execute(""" + SELECT COUNT(*) + FROM votes + WHERE question_id = ? + AND vote = 'A' + """, (qid,)) + + votes_a = cur.fetchone()[0] + + cur.execute(""" + SELECT COUNT(*) + FROM votes + WHERE question_id = ? + AND vote = 'B' + """, (qid,)) + + votes_b = cur.fetchone()[0] + + conn.close() + + total = votes_a + votes_b + + if total == 0: + percent_a = 0 + percent_b = 0 + else: + percent_a = round(votes_a / total * 100) + percent_b = round(votes_b / total * 100) + + return render_template( + "results.html", + question=question, + votes_a=votes_a, + votes_b=votes_b, + percent_a=percent_a, + percent_b=percent_b, + total=total + ) + + +if __name__ == "__main__": + app.run(debug=True) \ No newline at end of file diff --git a/init_db.py b/init_db.py new file mode 100644 index 0000000..570c59e --- /dev/null +++ b/init_db.py @@ -0,0 +1,39 @@ +import sqlite3 + +conn = sqlite3.connect("questions.db") +cursor = conn.cursor() + +cursor.execute(""" +CREATE TABLE IF NOT EXISTS questions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + question TEXT NOT NULL, + option_a TEXT NOT NULL, + option_b TEXT NOT NULL, + date TEXT NOT NULL UNIQUE +) +""") + +cursor.execute(""" +CREATE TABLE IF NOT EXISTS votes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + question_id INTEGER, + vote TEXT NOT NULL, + voter_hash TEXT NOT NULL +) +""") + +cursor.execute(""" +INSERT OR IGNORE INTO questions +(question, option_a, option_b, date) +VALUES ( + 'Would you rather be blind or deaf?', + 'Blind', + 'Deaf', + '2030-01-01' +) +""") + +conn.commit() +conn.close() + +print("Database created.") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f253ae5 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +blinker==1.9.0 +click==8.4.1 +Flask==3.1.3 +itsdangerous==2.2.0 +Jinja2==3.1.6 +MarkupSafe==3.0.3 +Werkzeug==3.1.8 diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..38881cf --- /dev/null +++ b/static/style.css @@ -0,0 +1,19 @@ +body { + max-width: 800px; + margin: auto; + padding-top: 60px; + text-align: center; + font-family: Arial, sans-serif; +} + +button { + width: 250px; + height: 100px; + margin: 10px; + font-size: 22px; + cursor: pointer; +} + +a { + font-size: 20px; +} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..5283c12 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,38 @@ + + + + Would You Rather + + + + +

{{ question[1] }}

+ +
+ + + + + + + +
+ + + \ No newline at end of file diff --git a/templates/results.html b/templates/results.html new file mode 100644 index 0000000..2066140 --- /dev/null +++ b/templates/results.html @@ -0,0 +1,24 @@ + + + + Results + + + + +

{{ question[1] }}

+ +

{{ question[2] }}

+

{{ percent_a }}% ({{ votes_a }} votes)

+ +

{{ question[3] }}

+

{{ percent_b }}% ({{ votes_b }} votes)

+ +
+ +

Total votes: {{ total }}

+ +Back + + + \ No newline at end of file