forked from rebeccastandig/TheMATCProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
521 lines (442 loc) · 15.9 KB
/
app.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
from flask import Flask, render_template, request, flash, redirect, url_for, session, Markup, Response
import model
import os
import keen
app = Flask(__name__)
app.secret_key = os.environ.get('flask_secretkey')
#### Basic Functionality ####
@app.route("/")
def index():
return redirect(url_for('about_tt'))
@app.route("/register", methods=['GET', 'POST'])
def register():
if request.method == 'POST':
name = request.form.get('name')
pw = request.form.get('pw')
verify_pw = request.form.get('verify_pw')
if len(name) > 0 and len(pw) > 0 and len(verify_pw) > 0:
name_is_alphanum = model.check_alphanum(name)
pw_is_alphanum = model.check_alphanum(pw)
if name_is_alphanum == False:
flash('Invalid user name. Alphanumeric characters only (A-Z, a-z, 0-9).')
return render_template("register.html")
elif pw_is_alphanum == False:
flash('Invalid password. Alphanumeric characters only (A-Z, a-z, 0-9).')
return render_template("register.html")
elif 2 > len(name) or 15 < len(name):
flash('Invalid user name. User name must be between 2 and 15 characters.')
return render_template("register.html")
elif 6 > len(pw) or 15 < len(pw):
flash('Invalid password. Password must be between 6 and 15 characters.')
return render_template("register.html")
elif pw != verify_pw:
flash('Passwords must match.')
return render_template("register.html")
elif (pw == verify_pw) and name:
if model.check_if_user(name) == True:
flash('You\'re already registered. Please sign in.')
return redirect(url_for('signin'))
else:
model.set_user(name)
model.set_user_pw(name, pw)
keen.add_event('register', {
'username':name
})
flash('Thanks for registering! You\'re now signed in.')
session['user'] = name
return redirect(url_for('index'))
else:
flash('All fields are required.')
return render_template("register.html")
elif not session:
return render_template("register.html")
else:
# if there's a session
flash('You\'re already signed in.')
return redirect(url_for('index'))
@app.route("/signin", methods=['GET', 'POST'])
def signin():
if request.method =='POST':
name = request.form.get('name')
pw = request.form.get('pw')
if name and pw:
if len(name) > 0 and len(pw) > 0:
name_is_alphanum = model.check_alphanum(name)
pw_is_alphanum = model.check_alphanum(pw)
if name_is_alphanum == True and pw_is_alphanum == True:
if model.auth_login(name, pw) == True:
keen.add_event('signins', {
"username":name
})
session['user'] = name
return redirect(url_for('index'))
else:
flash('Your user name and/or password didn\'t match our records. Please try signing in again.')
return render_template('signin.html')
else:
flash('Your user name and/or password didn\'t match our records. Please try signing in again.')
return render_template('signin.html')
elif not session:
return render_template('signin.html')
else:
# if there's a session
flash('You\'re already signed in.')
return redirect(url_for('index'))
@app.route("/logout")
def logout():
session.clear()
return redirect(url_for('index'))
@app.route("/about")
def about_tt():
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('about.html', logged_in=logged_in, not_you=not_you)
else:
return render_template('about.html')
@app.route("/about/tt")
def abt_tt():
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('about_tt.html', logged_in=logged_in, not_you=not_you)
else:
return render_template('about_tt.html')
@app.route("/about/matc")
def abt_matc():
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('about_matc.html', logged_in=logged_in, not_you=not_you)
else:
return render_template('about_matc.html')
@app.route("/about/ttud")
def abt_ttud():
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('about_ttud.html', logged_in=logged_in, not_you=not_you)
else:
return render_template('about_ttud.html')
@app.route("/about/faqs")
def abt_faqs():
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('about_faqs.html', logged_in=logged_in, not_you=not_you)
else:
return render_template('about_faqs.html')
@app.route("/corpus")
def corpus():
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('corpus.html', logged_in=logged_in, not_you=not_you)
else:
return render_template('corpus.html')
@app.route("/contact")
def contact_me():
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('contact_me.html', logged_in=logged_in, not_you=not_you)
else:
return render_template('contact_me.html')
@app.route("/linguists")
def linguist_info():
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('info_for_linguists.html', logged_in=logged_in, not_you=not_you)
else:
return render_template('info_for_linguists.html')
@app.route("/guide")
def guide():
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('pos_guide.html', logged_in=logged_in, not_you=not_you)
else:
return render_template('pos_guide.html')
#### End Basic Functionality ####
#### Corpus Functionality ####
## None of these will work until people tag in the game ##
# check if i need to redo any of these fns since i re-ordered pos tag thing?
@app.route("/corpus/download")
def corpus_download():
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('corpus_download_main.html', logged_in=logged_in, not_you=not_you)
else:
return render_template('corpus_download_main.html')
@app.route("/corpus/download/pos")
def corpus_download_pos():
keen.add_event("corpus_dl", {
"type":"pos"
})
corpus = model.get_corpus_pos()
return Response(corpus, mimetype='text/csv')
@app.route("/corpus/download/words")
def corpus_download_words():
keen.add_event("corpus_dl", {
"type":"words"
})
corpus = model.get_corpus_words()
return Response(corpus, mimetype='text/csv')
@app.route("/corpus/browse")
def corpus_browse():
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('browse.html', logged_in=logged_in, not_you=not_you)
else:
return render_template('browse.html')
@app.route("/corpus/browse/pos")
def corpus_pos_list():
tag_list = model.get_pos()
first_half = tag_list[:len(tag_list)/2]
second_half = tag_list[len(tag_list)/2:]
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('browse_list_pos.html', logged_in=logged_in, corpus_list=tag_list, not_you=not_you, first_half=first_half, second_half=second_half)
else:
return render_template('browse_list_pos.html', corpus_list=tag_list, first_half=first_half, second_half=second_half)
@app.route("/corpus/browse/words")
def corpus_words_list():
word_list = model.get_words()
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('browse_list_words.html', logged_in=logged_in, corpus_list=word_list, not_you=not_you)
else:
return render_template('browse_list_words.html', corpus_list=word_list)
@app.route("/corpus/browse/pos/<tag>")
def corpus_pos(tag):
tag_pos = 'tag_%s'%tag
word_list = model.get_words_by_tag(tag_pos)
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('browse_words_by_pos.html', logged_in=logged_in, word_list=word_list, tag=tag, not_you=not_you)
else:
return render_template('browse_words_by_pos.html', word_list=word_list, tag=tag)
@app.route("/corpus/browse/words/<word>")
def corpus_word(word):
tag_list = model.get_tags_by_word(word)
if session:
logged_in = 'Logged in as: %s.'%session['user']
not_you = 'Not %s?'%session['user']
return render_template('browse_pos_by_word.html', logged_in=logged_in, tag_list=tag_list, word=word, not_you=not_you)
else:
return render_template('browse_pos_by_word.html', tag_list=tag_list, word=word)
#### End Corpus Functionality ####
#### Game Functionality ####
@app.route("/game")
def game():
if not session:
return render_template('login_tt.html')
if session['user']:
game_info = model.get_words_tweets_game()
word_for_game = game_info[0]
tweet_for_game = game_info[1]
tweet_list = game_info[2]
user = session['user']
user_name_pts = "user_%s_pts"%user
user_points = model.get_string_num(user_name_pts)
tweet_for_game = unicode(tweet_for_game, 'utf-8', errors='replace')
for tweet in tweet_list:
tweet = unicode(tweet, 'utf-8', errors='replace')
pos_sents_tags = model.break_pos_sents(model.get_pos_sentences())
first_half = pos_sents_tags[:len(pos_sents_tags)/2]
second_half = pos_sents_tags[len(pos_sents_tags)/2:]
logged_in = 'Logged in as: %s.'%user
not_you = 'Not %s?'%session['user']
num_tagged = model.get_num_tagged(user)
num_final_tagged = model.get_num_final_tagged(user)
keen.add_event('game_play', {
"user":user,
"points":user_points,
"num_tagged":num_tagged,
"num_final_tagged":num_final_tagged
})
return render_template('game.html', word = word_for_game, tweet = Markup(tweet_for_game).unescape(), user_points=user_points, tweet_list=tweet_list, first_half=first_half, second_half=second_half, logged_in=logged_in, user=user, not_you=not_you)
@app.route("/game", methods=['POST'])
def play_game():
tag = request.form.get('tag')
word = request.form.get('word')
tweet = request.form.get('tweet')
user = session['user']
num_tagged = model.get_num_tagged(user)
num_final_tagged = model.get_num_final_tagged(user)
if tag and word:
# set the tag for the user and the tweet
model.tag_word_game(word, tag, user, tweet)
# add points and set final tag if needed
model.add_pts_game(word, tag, user)
# retrieve how many points user has
user_name_pts = "user_%s_pts"%user
num_points = model.get_string_num(user_name_pts)
keen.add_event("button_press", {
"tag":tag,
"word":word,
"user":user,
"num_tagged":num_tagged,
"num_final_tagged":num_final_tagged,
"points":num_points
})
return str(num_points)
@app.route("/game/more_tweets", methods=['POST'])
def new_tweet():
word = request.form.get('word')
tweet = request.form.get('tweet')
user = session['user']
user_name_pts = "user_%s_pts"%user
points = model.get_string_num(user_name_pts)
num_tagged = model.get_num_tagged(user)
num_final_tagged = model.get_num_final_tagged(user)
keen.add_event('more_tweets', {
"user":user,
'word':word,
'original tweet':tweet,
'num_tagged':num_tagged,
'num_final_tagged':num_final_tagged,
'points':points
})
tweet_list = model.get_another_tweet(word, tweet)
tweets_gotten = request.form.get('tweets_gotten')
if tweets_gotten > 0:
if len(tweet_list) >= tweets_gotten:
new_tweet = tweet_list[-tweets_gotten]
if new_tweet == tweet:
return "There are no more tweets with \"%s\" in them."%word
else:
new_tweet = unicode(new_tweet, 'utf-8', errors='replace')
return render_template('moretweets.html', tweet=Markup(new_tweet).unescape())
else:
new_tweet = "There are no more tweets with \"%s\" in them."%word
return render_template('moretweets.html', tweet=new_tweet)
else:
new_tweet = "There are no more tweets with \"%s\" in them."%word
return render_template('moretweets.html', tweet=new_tweet)
@app.route("/game/howto")
def howto():
return render_template("tutorial.html")
@app.route("/game/posreference")
def photoref():
return render_template("photoreference.html")
@app.route("/game/more_tweets/new")
def more_tweets(tweet):
return render_template('moretweets.html', tweet=tweet)
@app.route("/game/points")
def show_points():
if not session:
scores = model.get_top_scores()
return render_template("top_points.html", scores=scores )
if session['user']:
user = session['user']
scores = model.get_top_scores()
user_name_pts = "user_%s_pts"%user
user_score = model.get_string_num(user_name_pts)
logged_in = 'Logged in as: %s.'%user
not_you = 'Not %s?'%session['user']
return render_template("user_and_top_points.html", scores=scores, user_score=user_score, user=user, logged_in=logged_in, not_you=not_you)
#### End Game Functionality ####
#### Visualization Functionality ####
@app.route("/vis")
def vis():
# lists all pos, you can choose which one you want to visualize
return render_template("vis.html")
@app.route("/vis/N")
def vis_n():
words = model.final_tags_pos("N")
return render_template("vis_pos.html", words=words, pos='N')
@app.route("/vis/O")
def vis_o():
words = model.final_tags_pos("O")
return render_template("vis_pos.html", words=words, pos='O')
@app.route("/vis/P")
def vis_p():
words = model.final_tags_pos("P")
return render_template("vis_pos.html", words=words, pos='P')
@app.route("/vis/M")
def vis_m():
words = model.final_tags_pos("M")
return render_template("vis_pos.html", words=words, pos='M')
@app.route("/vis/PV")
def vis_pv():
words = model.final_tags_pos("PV")
return render_template("vis_pos.html", words=words, pos='PV')
@app.route("/vis/NV")
def vis_nv():
words = model.final_tags_pos("NV")
return render_template("vis_pos.html", words=words, pos='NV')
@app.route("/vis/Z")
def vis_z():
words = model.final_tags_pos("Z")
return render_template("vis_pos.html", words=words, pos='Z')
@app.route("/vis/OP")
def vis_op():
words = model.final_tags_pos("OP")
return render_template("vis_pos.html", words=words, pos='OP')
@app.route("/vis/J")
def vis_j():
words = model.final_tags_pos("J")
return render_template("vis_pos.html", words=words, pos='J')
@app.route("/vis/A")
def vis_a():
words = model.final_tags_pos("A")
return render_template("vis_pos.html", words=words, pos='A')
@app.route("/vis/S")
def vis_s():
words = model.final_tags_pos("S")
return render_template("vis_pos.html", words=words, pos='S')
@app.route("/vis/I")
def vis_i():
words = model.final_tags_pos("I")
return render_template("vis_pos.html", words=words, pos='I')
@app.route("/vis/D")
def vis_d():
words = model.final_tags_pos("D")
return render_template("vis_pos.html", words=words, pos='D')
@app.route("/vis/V")
def vis_v():
words = model.final_tags_pos("V")
return render_template("vis_pos.html", words=words, pos='V')
@app.route("/vis/C")
def vis_c():
words = model.final_tags_pos("C")
return render_template("vis_pos.html", words=words, pos='C')
@app.route("/vis/E")
def vis_e():
words = model.final_tags_pos("E")
return render_template("vis_pos.html", words=words, pos='E')
@app.route("/vis/PD")
def vis_pd():
words = model.final_tags_pos("PD")
return render_template("vis_pos.html", words=words, pos='PD')
@app.route("/vis/EV")
def vis_ev():
words = model.final_tags_pos("EV")
return render_template("vis_pos.html", words=words, pos='EV')
@app.route("/vis/SC")
def vis_sc():
words = model.final_tags_pos("SC")
return render_template("vis_pos.html", words=words, pos='SC')
#### End Visualization Functionality ####
#### Admin Dashboard Functionality ####
@app.route("/dashboard")
def dashboard():
if not session:
return redirect(url_for('index'))
else:
if session['user'] == 'rebecca':
more_tweets_word = keen.count("more_tweets", group_by="word")
players_last_hr = keen.count('game_play', group_by="user", timeframe="previous_60_minutes")
return render_template('dashboard.html', more_tweets_word=more_tweets_word, players_last_hr=players_last_hr)
else:
return redirect(url_for('index'))
#### End Admin Dashboard Functionality ####
if __name__ == "__main__":
app.run(host='0.0.0.0', port=os.environ.get('PORT'))