-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
628 lines (496 loc) · 18.4 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import json
import logging
import markdown
import os
import threading
import time
import traceback
import random
import urllib
from flask import Flask, redirect, render_template, request, Response, session, url_for, send_from_directory
from flask_caching import Cache
from ccl_scratch_tools import Parser
from ccl_scratch_tools import Scraper
from ccl_scratch_tools import Visualizer
from werkzeug.exceptions import default_exceptions, HTTPException, InternalServerError, NotFound
from lib import common
from lib import errors
from lib import schema
from lib import scrape
from lib import tasks
from lib import authentication
from lib import admin
from lib import display
from lib import certificate
from lib import summary
from lib.authentication import admin_required, login_required
from lib.reports import reporting
from lib.settings import CACHE_DIRECTORY, CLRY, PROJECT_CACHE_LENGTH, PROJECT_DIRECTORY, REDIRECT_PAGES, SITE
app = Flask(__name__)
app.register_blueprint(reporting)
try:
celery = tasks.make_celery(CLRY["name"],
CLRY["result_backend"],
CLRY["broker_url"],
app)
except:
logging.warn("Couldn't load celery.")
parser = Parser()
app.jinja_env.filters["twodec"] = common.twodec
app.jinja_env.filters["indexOf"] = common.indexOf
app.jinja_env.filters["pluralize"] = common.pluralize
app.jinja_env.filters["human_block"] = common.human_block
app.jinja_env.filters["get_selected"] = common.get_selected
app.secret_key = os.urandom(24)
app.url_map.strict_slashes = False
app.config["CACHE_TYPE"] = "lib.cache.MongoCache"
app.config["CACHE_DEFAULT_TIMEOUT"] = 1200
cache = Cache(app)
# Pass things to all templates
@app.context_processor
def inject_vars():
return dict(user=authentication.get_login_info(),
valid_admin_pages=admin.VALID_ADMIN_PAGES,
SITE=SITE)
# Helper routes
@app.route("/redirect", methods=["GET"])
def redirect_to():
if (request.args.get("username") is not None
and request.args.get("username") != ""): # yapf: disable
return redirect("/user/{0}".format(
urllib.parse.quote(request.args.get("username"))))
else:
return render_template("index.html",
message="Sorry! I wasn't able to do that.")
# Authentication
@app.route("/login", methods=["GET", "POST"])
def login():
session.clear()
if request.method == "GET":
return render_template("login.html")
else:
# yapf: disable
if (request.form["username"] is None
or request.form["username"] == ""
or request.form["password"] is None
or request.form["password"] == ""):
return render_template("login.html",
message="All fields are required!")
# yapf: enable
res = authentication.login_user(request.form["username"],
request.form["password"])
if res:
return redirect("/admin")
else:
return render_template(
"login.html",
message="Couldn't log in with that username/password combination!"
)
@app.route("/logout", methods=["GET"])
def logout():
session.clear()
return render_template("login.html", message="Successfully logged out.")
@app.route("/register", methods=["POST"])
def register():
res = authentication.register_user(request.form["username"],
request.form["email"],
request.form["first_name"],
request.form["last_name"],
request.form["password"],
request.form["user_role"])
if type(res) == bool and res:
return redirect("/login")
else:
return render_template(
"index.html",
message="One or several of your inputs were invalid.")
# For when the site is brand new
@app.route("/setup", methods=["GET"])
def setup():
common.connect_db()
if len(authentication.User.objects()) == 0:
session["user"] = {"role": "site_admin"}
return render_template("setup.html")
else:
return redirect("/")
# Admin pages
@app.route("/admin")
@admin_required
def admin_index():
return render_template("admin/index.html")
@app.route("/admin/<page>", methods=["GET", "POST"])
@admin_required
def admin_page(page):
if page in admin.VALID_ADMIN_PAGES:
if request.method == "GET":
info = admin.get_info(page)
return render_template("admin/{0}.html".format(page), info=info)
else:
if request.is_json:
form = request.get_json()
else:
form = request.form
result = admin.set_info(page, form)
if "redirect" in request.form:
if request.form["redirect"] in admin.VALID_REDIRECTS:
return redirect(request.form["redirect"])
return json.dumps(result)
else:
return redirect("/admin")
@app.route("/admin/cache/clear")
@admin_required
def clear_cache():
cache.clear()
return redirect("/admin")
@app.route("/admin/error/<eid>")
@admin_required
def error_page(eid):
error = errors.get_error(eid)
if not error:
return redirect("/admin/errors")
else:
issue = {
"title":
"{} error when loading {}"
.format(error["error_code"],
urllib.parse.urlparse(error["url"]).path),
"body":
"**[Replicate here]({})**\n\nWhen accessing `{}`, there's a {} error. The traceback says:\n\n```python\n{}\n```"
.format(error["url"],
urllib.parse.urlparse(error["url"]).path,
error["error_code"],
error["traceback"])
}
return render_template("admin/error.html", error=error, issue=issue)
def schema_editor(id):
data = {
"min_instructions_length": 0,
"min_description_length": 0,
"min_comments_made": 0,
"min_blockify": {
"comments": 0,
"costumes": 0,
"sounds": 0,
"sprites": 0,
"variables": 0
},
"required_text": [],
"required_block_categories": {},
"required_blocks": [],
"stats": [],
"text": {},
"comparison_basis": {
"basis": "__none__",
"priority": None
}
}
if id != "__new__":
common.connect_db()
try:
data = schema.Challenge.objects(id=id).first().to_mongo()
except AttributeError:
raise NotFound()
blocks = parser.block_data
block_list = list()
block_dict = dict()
for cat in blocks:
block_list += blocks[cat].keys()
for block in blocks[cat]:
block_dict[blocks[cat][block].lower().replace(" ", "")] = block
return render_template("admin/edit_schema.html",
blocks=blocks,
block_dict=block_dict,
block_list=block_list,
categories=list(blocks.keys()),
data=data,
schema_id=id,
stats=scrape.get_default_studio_stats())
@app.route("/admin/schemas/edit", methods=["GET"])
@admin_required
def add_schema():
return schema_editor("__new__")
@app.route("/admin/schemas/edit/<id>", methods=["GET"])
@admin_required
def edit_schema(id):
return schema_editor(id)
# Studios, projects, users, challenges
@app.route("/certificate/generate")
@admin_required
def generate_certificate():
common.connect_db()
authors = list(set(scrape.Project.objects().values_list("author")))
certificate.generate_certs.delay(authors)
return redirect("/admin/utilities")
@app.route("/participation")
def index():
return render_template("index.html")
@app.route("/md", methods=["POST"])
def md():
text = request.form["text"]
if text is not None:
ret = {"html": common.md(text), "js": "/static/js/sb.js"}
return json.dumps(ret)
return "False"
@app.route("/project/d", methods=["POST"])
def project_download():
if request.form["sid"] is None or request.form["pid"] is None:
return "False"
sid = request.form["sid"]
pid = request.form["pid"]
scraper = Scraper()
try:
pid = int(pid)
except:
return "False"
if pid in scraper.get_projects_in_studio(sid):
return str(scrape.add_project(pid, sid, CACHE_DIRECTORY))
else:
return "False"
@app.route("/project/f/<pid>", methods=["POST"])
def project_feedback(pid):
if ("_gu_uid" in request.cookies and "feelings" in request.json
and "minutes" in request.json): # yapf: disable
try:
common.connect_db()
reflection = scrape.ProjectReflection(
project_id=pid,
gu_uid=request.cookies.get("_gu_uid"),
minutes=int(request.json["minutes"]),
feelings=request.json["feelings"])
reflection.save()
return "True"
except:
return "False"
else:
return "False"
@app.route("/project/o/<pid>")
def feedback_owner(pid):
try:
common.connect_db()
reflection = scrape.ProjectReflection.objects(
project_id=pid).order_by("-timestamp").first()
return reflection["gu_uid"]
except:
return ""
@app.route("/project/r/<pid>")
def reload_project(pid):
try:
pid = int(pid)
except:
pid = 0
scrape.set_reload_page(pid)
return redirect("/project/{}".format(pid))
@app.route("/project/<pid>/view", methods=["GET"])
@cache.cached(timeout=PROJECT_CACHE_LENGTH,
forced_update=scrape.get_reload_project,
unless=authentication.session_active)
def project__id(pid):
return display.get_project_page(pid, CACHE_DIRECTORY)
@app.route("/project/<pid>", methods=["GET"])
def project_id(pid):
return render_template("project_loader.html")
@app.route("/studio", methods=["GET", "POST"])
@admin_required
def studio():
if request.method == "GET":
common.connect_db()
return render_template("studio.html",
schemas=list(schema.Challenge.objects().order_by("-modified"))) # yapf: disable
else:
scraper = Scraper()
sid = scraper.get_id(request.form["studio"])
s = None
if request.form["schema"] != "__none__":
s = request.form["schema"]
if request.form["studio"] == "__all__":
scrape.rescrape_all.delay(cache_directory=CACHE_DIRECTORY)
return "Started"
elif sid is not None:
scrape.add_studio.delay(sid,
schema=s,
show=("show" in request.form),
cache_directory=CACHE_DIRECTORY)
return redirect("/studio/{0}".format(sid))
else:
return render_template(
"studio.html",
message="Please enter a valid studio ID or URL.")
@app.route("/studio/list/<sid>")
def studio_list(sid):
if sid == "":
return "Must include a studio ID.", 400
common.connect_db()
studio = scrape.Studio.objects(studio_id=sid).first()
if studio is None:
return "Studio does not exist.", 404
limit = 8
page = 0
order = "author"
try:
if "page" in request.args:
page = int(request.args["page"])
if "order" in request.args:
if request.args["order"] in {"author", "title", "id", "project_id"}:
order = request.args["order"]
if "limit" in request.args:
if int(request.args["limit"]) <= 100:
limit = int(request.args["limit"])
except:
return "Invalid arguments", 400
skip = page * limit
projects = scrape.Project.objects(
studio_id=sid).order_by(order).skip(skip).limit(limit)
info = {"projects": list()}
for i, project in enumerate(projects):
info["projects"].append({
"project_id": project["project_id"],
"title": project["title"],
"author": project["author"],
"image": (project["image"] if "image" in project else ""),
"modified": project["history"]["modified"]
})
return Response(json.dumps(info), mimetype="application/json")
@app.route("/studio/<sid>")
def studio_id(sid):
if sid == "":
return redirect("/prompts")
common.connect_db()
studio = scrape.Studio.objects(studio_id=sid).first()
if studio is None or (not (studio["public_show"]
or authentication.session_active())):
return redirect("/prompts")
projects = list(scrape.Project.objects(studio_id=sid).order_by("author"))
info = {"authors": list(), "project_ids": list(), "titles": list()}
for project in projects:
info["authors"].append(project["author"].lower())
info["project_ids"].append(project["project_id"])
info["titles"].append(project["title"].lower())
message = None
if studio["status"] == "in_progress" or studio["status"] is None:
message = "This studio is currently in the process of being downloaded and analyzed. <a href=''>Refresh page.</a>"
return render_template("studio_id.html",
info=info,
projects=projects,
studio=studio,
message=message)
@app.route("/user/<username>", methods=["GET", "POST"])
def user_id(username):
if request.method == "POST":
return send_from_directory(f"{CACHE_DIRECTORY}/certificates",
filename="{}.pdf".format(username.lower()))
else:
common.connect_db()
projects = list(scrape.Project.objects(author=username.lower()))
studios = dict()
keep_projects = list()
for i, project in enumerate(projects):
if project["studio_id"] not in studios:
studio = scrape.Studio.objects(
studio_id=project["studio_id"]).first()
if studio is not None:
studios[project["studio_id"]] = studio
keep_projects.append(project)
else:
keep_projects.append(project)
return render_template("username.html",
projects=keep_projects,
studios=studios,
username=username)
@app.route("/prompts", methods=["GET"])
@cache.cached(timeout=600, unless=authentication.session_active)
def prompts():
common.connect_db()
studios = list(scrape.Studio.objects(public_show=True))
schema_ids = set()
for studio in studios:
if "challenge_id" not in studio:
studios.remove(studio)
break
schema_ids.add(studio["challenge_id"])
schemas = schema.Challenge.objects(id__in=schema_ids).order_by("short_label", "title") # yapf: disable
id_order = list(schemas.values_list("id"))
for i in range(len(id_order)):
id_order[i] = str(id_order[i])
schemas = schemas.as_pymongo()
new_schemas = dict()
for sc in schemas:
new_schemas[str(sc["_id"])] = sc
# Order the studios
ordered_studios = [None] * len(studios)
for studio in studios:
studio["challenge_id"] = str(studio["challenge_id"])
try:
ordered_studios[id_order.index(studio["challenge_id"])] = studio
except ValueError:
pass
return render_template("prompts.html",
challenges=ordered_studios,
schemas=new_schemas)
@app.route("/summary", methods=["GET", "POST"])
def summarize():
if request.method == "GET":
with open("{}/lib/data/summary.json".format(PROJECT_DIRECTORY)) as f:
data = json.load(f)
for i, item in enumerate(data["content"]):
data["content"][i] = common.md(item) if isinstance(item, str) else item # yapf: disable
return render_template("summary.html", data=data)
else:
with open("{}/data/summary.json".format(CACHE_DIRECTORY)) as f:
return Response(f.read(), mimetype="application/json")
@app.route("/summary/image")
@cache.cached()
def summary_image():
try:
with open("{}/cache/data/projects.jpg".format(PROJECT_DIRECTORY), "rb") as f: # yapf: disable
return f.read()
except:
return "Not found", 404
@app.route("/summary/generate")
@admin_required
def generate_summary():
summary.generate_summary_page.delay()
return redirect("/admin/utilities")
# Static pages -- About, Strategies, Signup, Research
@app.route("/")
@cache.cached(unless=authentication.session_active)
def homepage():
return render_template("home.html", section="home")
@app.route("/about", methods=["GET"])
@cache.cached(unless=authentication.session_active)
def about():
return render_template("about.html")
@app.route("/strategies", methods=["GET"])
@cache.cached(unless=authentication.session_active)
def strategies():
return render_template("strategies.html")
@app.route("/signup", methods=["GET", "POST"])
@cache.cached(unless=authentication.session_active)
def signup():
return render_template("signup.html")
@app.route("/research", methods=["GET"])
@cache.cached(unless=authentication.session_active)
def research():
return render_template("research.html")
# Error pages
@app.route("/ie")
@cache.cached(unless=authentication.session_active)
def ie():
return render_template("ie.html")
def error(e):
"""Handle errors."""
if e.code == 404 and request.path in REDIRECT_PAGES:
return redirect(REDIRECT_PAGES[request.path], code=301)
status = "closed" if e.code == 404 else "open"
saved = errors.add_error(e.code,
request.url,
traceback.format_exc(),
status)
if not isinstance(e, HTTPException):
e = InternalServerError()
scratch = "when i receive [error {} v]\n say [Oh no!]\nswitch costume to (sad :\( v)".format(e.code) # yapf: disable
return render_template("error.html", error=e, scratch=scratch, saved=saved)
# Listen for errors
for code in default_exceptions:
app.errorhandler(code)(error)
if __name__ == "__main__":
app.run()