-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrivia.py
107 lines (94 loc) · 3.26 KB
/
trivia.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import ConfigParser
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
# Configuration
config = ConfigParser.RawConfigParser()
try:
config.read('trivia.config')
except:
config.read('trivia.config.default')
# Load default config and override config from an environment variable
app.config.update(dict(
DATABASE=os.path.join(app.root_path, config.get('trivia','database')),
SECRET_KEY=config.get('trivia','secret_key'),
USERNAME=config.get('trivia','admin_user'),
PASSWORD=config.get('trivia','admin_pass')
))
app.config.from_envvar('TRIVIA_SETTINGS', silent=True)
def connect_db():
"""Connects to the specific database."""
print app.config['DATABASE']
rv = sqlite3.connect(app.config['DATABASE'])
rv.row_factory = sqlite3.Row
return rv
def get_db():
"""Opens a new database connection if there is none yet for the
current application context.
"""
if not hasattr(g, 'sqlite_db'):
g.sqlite_db = connect_db()
return g.sqlite_db
@app.teardown_appcontext
def close_db(error):
"""Closes the database again at the end of the request."""
if hasattr(g, 'sqlite_db'):
g.sqlite_db.close()
def init_db():
db = get_db()
with app.open_resource(os.path.join(app.root_path, 'schema.sql'), mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
@app.cli.command('initdb')
def initdb_command():
"""Initializes the database."""
init_db()
print 'Initialized the database.'
@app.route('/')
def show_entries():
db = get_db()
cur = db.execute('select * from entries order by id desc')
entries = cur.fetchall()
return render_template('show_entries.html', entries=entries)
@app.route('/add', methods=['POST'])
def add_entry():
db = get_db()
db.execute('insert into entries (user, cwid, pin, q0, q1, q2, q3, q4, q5, q6, q7, q8, q9) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[request.form['user'],
request.form['cwid'],
request.form['pin'],
request.form['q0'],
request.form['q1'],
request.form['q2'],
request.form['q3'],
request.form['q4'],
request.form['q5'],
request.form['q6'],
request.form['q7'],
request.form['q8'],
request.form['q9']])
db.commit()
flash('Submission Successfully Sent')
return redirect(url_for('show_entries'))
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != app.config['USERNAME']:
error = 'Invalid username'
elif request.form['password'] != app.config['PASSWORD']:
error = 'Invalid password'
else:
session['logged_in'] = True
flash('You were logged in')
return redirect(url_for('show_entries'))
return render_template('login.html', error=error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('show_entries'))