-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
188 lines (163 loc) · 8.27 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
# Import python libraries:
import logging, os
import json
import random
# Import third-party libraries:
from google.appengine.api import users
from flask import Flask,redirect,request,render_template
# Import our own files:
from profile import UserProfile
from nextpuzzle import progress
# make the flask app:
app = Flask(__name__)
# Set up debug messages, when not in "real world" production mode
production_environment = os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')
if not production_environment:
app.debug = True
logging.info('debugging!')
# When user goes to home page, redirect to static welcome page
@app.route('/')
def home():
return redirect('/resources/welcome.html')
# when user goes to /next,
# figure out the next logical step for this user and redirect there.
@app.route('/next')
def nextStep():
print request.remote_addr
profile = UserProfile.get_by_user(request.remote_addr)
print profile
return redirect(profile.current_puzzle)
@app.route('/dummyprofile')
def renderDummyProfile():
username = 'Fake User'
current_puzzle = 'fake/url.html'
total_puzzles = 8
completed_puzzles = ['autopass/hello-world.html','resources/doctypequiz.html']
num_completed = 2
return render_template('profile.html',
user=username,
curr=current_puzzle,
total=total_puzzles,
completed_count=num_completed,
completed_urls=completed_puzzles)
@app.route('/dummyheadertest')
def renderDummyPuzzle():
puzzlename = 'Fake Puzzle'
total_puzzles = 8
num_completed = 2
prev_puzzle = 'fake/url.html'
return render_template('autopass/hello-world-inherit.html',
puzzle_name=puzzlename,
completed_count=num_completed,
total=total_puzzles)
# check the doc type quiz question
@app.route('/doctypeanswer')
def checkDocType():
answer = request.args.get('struct') # get what was submitted in the struct field
if answer == 'opt2': # compare to correct answer
# if correct, then use the progress() function to progress from this puzzle
return progress('resources/doctypequiz.html') # progress() takes in the name of the current puzzle, and returns a link to the next one
# progress() also marks the current puzzle as solved for this user.
else:
# wrong answer - return a short snippet of HTML to send them back to the same quiz.
return 'Sorry, that\'s wrong! <a href="/resources/doctypequiz.html">Try again?</a>'
# check the doc type quiz question
@app.route('/quizanswer')
def imagetag():
answer = request.args.get('structure') # get what was submitted in the struct field
if answer == 'opt3': # compare to correct answer
# if correct, then use the progress() function to progress from this puzzle
return progress('resources/quizquestion.html') # progress() takes in the name of the current puzzle, and returns a link to the next one
# progress() also marks the current puzzle as solved for this user.
else:
# wrong answer - return a short snippet of HTML to send them back to the same quiz.
return 'Sorry, that\'s wrong! <a href="/resources/quizquestion.html">Try again?</a>'
@app.route('/viccorrect')
def checkVicCorrect():
answer = request.args.get('choice') # get what was submitted in the struct field
if answer == 'Dogs': # compare to correct answer
# if correct, then use the progress() function to progress from this puzzle
return progress('resources/VicsQuiz.html') # progress() takes in the name of the current puzzle, and returns a link to the next one
# progress() also marks the current puzzle as solved for this user.
else:
# wrong answer - return a short snippet of HTML to send them back to the same quiz.
return 'Sorry, that\'s wrong! <a href="/resources/VicsQuiz.html">Try again?</a>'
@app.route('/kennedyanswer')
def checkForm():
answer = request.args.get('form') # get what was submitted in the struct field
if answer == '<form>': # compare to correct answer
# if correct, then use the progress() function to progress from this puzzle
return progress('resources/selecttheform.html') # progress() takes in the name of the current puzzle, and returns a link to the next one
# progress() also marks the current puzzle as solved for this user.
else:
# wrong answer - return a short snippet of HTML to send them back to the same quiz.
return 'Sorry, that\'s wrong! <a href="/resources/selecttheform.html">Try again?</a>'
@app.route('/youranswer')
def headertype():
answer = request.args.get('heading') # get what was submitted in the struct field
if answer == 'opt1': # compare to correct answer
# if correct, then use the progress() function to progress from this puzzle
return progress('resources/daizhaquiz.html') # progress() takes in the name of the current puzzle, and returns a link to the next one
# progress() also marks the current puzzle as solved for this user.
else:
# wrong answer - return a short snippet of HTML to send them back to the same quiz.
return 'Sorry, that\'s wrong! <a href="/resources/daizhaquiz.html">Try again?</a>'
@app.route('/daletypeanswer')
def daleDocType():
answer = request.args.get('struct') # get what was submitted in the struct field
if answer == 'opt1': # compare to correct answer
# if correct, then use the progress() function to progress from this puzzle
return progress('resources/dalequiz.html') # progress() takes in the name of the current puzzle, and returns a link to the next one
# progress() also marks the current puzzle as solved for this user.
else:
# wrong answer - return a short snippet of HTML to send them back to the same quiz.
return 'Sorry, that\'s wrong! <a href="/resources/dalequiz.html">Try again?</a>'
# check the doc type quiz question
@app.route('/quizanswer')
def quiz():
answer = request.args.get('question') # get what was submitted in the struct field
if answer == '1': # compare to correct answer
# if correct, then use the progress() function to progress from this puzzle
return progress('resources/newquiz.html') # progress() takes in the name of the current puzzle, and returns a link to the next one
# progress() also marks the current puzzle as solved for this user.
else:
# wrong answer - return a short snippet of HTML to send them back to the same quiz.
return 'Sorry, that\'s wrong! <a href="/resources/newquiz.html">Try again?</a>'
# when user navigates to an autopass puzzle, either display the puzzle,
# or (if this is a correct solution) move on to the next puzzle
@app.route('/autopass/<puzzle>')
def render_autopass_puzzle(puzzle):
# get passphrase that was submitted with this request, if any:
submitted = request.args.get('pass')
# get current user's passphrase:
profile = UserProfile.get_by_user(request.remote_addr)
# see if they submitted the correct one:
if submitted and (submitted == profile.current_passphrase):
profile.solved_puzzles.append('autopass/'+puzzle)
profile.put()
value = 'correct! '
value += progress('autopass/'+puzzle)
return value
# fallthrough logic - incorrect or no passphrase submitted:
# TODO: add extra output on pass submitted, but incorrect?
# (but then it'd be outside of the manual HTML structure)
# generate a new passphrase:
passphrase = 'default'
with app.open_resource('data/passphrases.json') as f:
passphrases = json.load(f)
passphrase = random.choice(passphrases)
# store it in user's profile:
profile.current_passphrase = passphrase
profile.put()
return render_template('autopass/'+puzzle, passphrase=passphrase)
@app.route('/answer')
def CheckForm():
answer = request.args.get('pass') # get what was submitted in the struct field
if answer == 'pass=banana': # compare to correct answer
# if correct, then use the progress() function to progress from this puzzle
return progress('resources/quizquestionissue.html') # progress() takes in the name of the current puzzle, and returns a link to the next one
# progress() also marks the current puzzle as solved for this user.
else:
# wrong answer - return a short snippet of HTML to send them back to the same quiz.
return 'Sorry, that\'s wrong! <a href="/resources/quizquestionissue.html">Try again?</a>'