-
Notifications
You must be signed in to change notification settings - Fork 1
/
poll.py
executable file
·159 lines (123 loc) · 3.81 KB
/
poll.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from flask import Flask, render_template, request, Response, redirect, url_for, session, abort
from flask.ext.login import LoginManager, UserMixin, \
login_required, login_user, logout_user
import os
import lookup
app = Flask(__name__)
ANS_ENUM = ['No', 'Unfamiliar with Drink', 'Yes']
response_data = ['You should try bacardi and coke', 'You should try vodka martini', 'You should try mimosa']
# config
app.config.update(
DEBUG = True,
SECRET_KEY = 'secret_xxx'
)
# flask-login
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
# silly user model
class User(UserMixin):
def __init__(self, id):
self.id = id
self.name = "user" + str(id)
self.password = self.name + "_secret"
def __repr__(self):
return "%d/%s/%s" % (self.id, self.name, self.password)
# create some users with ids 1 to 20
users = [User(id) for id in range(1, 21)]
poll_data =[
{'question' : 'Do you like Whiskey and Coke?',
'fields' : ['Yes', 'No', 'Unfamiliar with Drink'], 'val' : 'q0'},
{'question' : 'Do you like Long Island?',
'fields' : ['Yes', 'No', 'Unfamiliar with Drink'], 'val' : 'q1'},
{'question' : 'Do you like Gin and Tonic?',
'fields' : ['Yes', 'No', 'Unfamiliar with Drink'], 'val' : 'q2'}]
filename = 'data.txt'
def response(val0, val1, val2):
tup = (val0, val1, val2)
return lookup.ginWhiskeyTeaTable[tup]
@app.route('/')
@login_required
def root():
return render_template('poll.html', data=poll_data)
@app.route('/poll')
@login_required
def poll():
vote0 = request.args.get('q0')
vote1 = request.args.get('q1')
vote2 = request.args.get('q2')
vote_val0 = -2
vote_val1 = -2
vote_val2 = -2
print(vote0)
for val in range(0, 3):
if vote0 == ANS_ENUM[val]:
vote_val0 = val-1
print(vote_val0)
if vote_val0 == -1:
print("Error")
for val in range(0, 3):
if vote1 == ANS_ENUM[val]:
vote_val1 = val-1
print(vote_val1)
if vote_val1 == -1:
print("Error")
for val in range(0, 3):
if vote2 == ANS_ENUM[val]:
vote_val2 = val-1
print(vote_val2)
if vote_val2 == -1:
print("Error")
str_response = response(vote_val0, vote_val1, vote_val2)
out = open(filename, 'a')
out.write( str(vote_val0) + '\n' )
out.close()
return render_template('thankyou.html', data=str_response)
@app.route('/results')
@login_required
def show_results():
votes = {}
for f in poll_data['fields']:
votes[f] = 0
f = open(filename, 'r')
for line in f:
vote = line.rstrip("\n")
votes[vote] += 1
return render_template('results.html', data=poll_data, votes=votes)
# somewhere to login
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if password == username + "_secret":
id = username.split('user')[1]
user = User(id)
login_user(user)
return redirect(request.args.get("next"))
else:
return abort(401)
else:
return Response('''
<form action="" method="post">
<p><input type=text name=username>
<p><input type=password name=password>
<p><input type=submit value=Login>
</form>
''')
# somewhere to logout
@app.route("/logout")
@login_required
def logout():
logout_user()
return Response('<p>Logged out</p>')
# handle login failed
@app.errorhandler(401)
def page_not_found(e):
return Response('<p>Login failed</p>')
# callback to reload the user object
@login_manager.user_loader
def load_user(userid):
return User(userid)
if __name__ == "__main__":
app.run(debug=True)