Inital commit, working non plished website
This commit is contained in:
commit
54ffb721dd
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
.venv/
|
||||
__pycache__/
|
||||
*.pyc
|
||||
questions.db
|
||||
.env
|
||||
.vscode/
|
||||
.idea/
|
||||
135
app.py
Normal file
135
app.py
Normal file
@ -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)
|
||||
39
init_db.py
Normal file
39
init_db.py
Normal file
@ -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.")
|
||||
7
requirements.txt
Normal file
7
requirements.txt
Normal file
@ -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
|
||||
19
static/style.css
Normal file
19
static/style.css
Normal file
@ -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;
|
||||
}
|
||||
38
templates/index.html
Normal file
38
templates/index.html
Normal file
@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Would You Rather</title>
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>{{ question[1] }}</h1>
|
||||
|
||||
<form action="/vote" method="POST">
|
||||
|
||||
<input
|
||||
type="hidden"
|
||||
name="qid"
|
||||
value="{{ question[0] }}"
|
||||
>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
name="vote"
|
||||
value="A"
|
||||
>
|
||||
{{ question[2] }}
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
name="vote"
|
||||
value="B"
|
||||
>
|
||||
{{ question[3] }}
|
||||
</button>
|
||||
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
24
templates/results.html
Normal file
24
templates/results.html
Normal file
@ -0,0 +1,24 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Results</title>
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>{{ question[1] }}</h1>
|
||||
|
||||
<h2>{{ question[2] }}</h2>
|
||||
<p>{{ percent_a }}% ({{ votes_a }} votes)</p>
|
||||
|
||||
<h2>{{ question[3] }}</h2>
|
||||
<p>{{ percent_b }}% ({{ votes_b }} votes)</p>
|
||||
|
||||
<hr>
|
||||
|
||||
<p>Total votes: {{ total }}</p>
|
||||
|
||||
<a href="/">Back</a>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in New Issue
Block a user