-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
67 lines (57 loc) · 2.32 KB
/
app.py
File metadata and controls
67 lines (57 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask, render_template, request, flash, redirect, url_for, session
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import desc
from sqlalchemy.sql import text
app = Flask(__name__, template_folder='templates')
app.secret_key = "neartobull3216"
db_name = 'nttb_answers.db'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + db_name
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
class User(db.Model):
table_name = 'nttb_answers'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
answer = db.Column(db.Float)
absdiff = db.Column(db.Float)
def __init__(self, name, answer, absdiff):
self.name = name
self.answer = answer
self.absdiff = absdiff
db.create_all()
@app.route("/hello")
def index():
flash("Hello, who are you?")
return render_template("front.html")
@app.route("/question", methods=['POST', 'GET'])
def greeter():
flash(str(request.form['name_input']) + ", how many functioning London Underground stations are there?")
session['nttb_name'] = request.form['name_input']
return (render_template("question.html"))
@app.route("/thank-you", methods=['POST', 'GET'])
def thanks():
nttb_name = session.get('nttb_name', None)
nttb_answers = User(nttb_name, request.form['answer_input'], abs(float(request.form['answer_input']) - 272))
db.session.add(nttb_answers)
db.session.commit()
session.clear()
return render_template("end.html")
@app.route("/results", methods=['POST', 'GET'])
def thanks2():
error = None
if request.method == 'POST':
if request.form['username'] == 'admin' and request.form['password'] == 'admin':
session['admin'] = request.form['username']
return redirect(url_for('inventory'))
else:
error = 'Nope! Wrong login'
return render_template("results.html", error = error)
@app.route('/results-table')
def inventory():
if 'admin' not in session:
return redirect(url_for('index'))
else:
session.clear()
guesses = User.query.order_by(desc(User.absdiff)).all()
worstguess = User.query.order_by(desc(User.absdiff)).limit(1)
return render_template('resultstable.html', guesses=guesses, worstguess=worstguess)