-
Notifications
You must be signed in to change notification settings - Fork 0
/
repertoaari_flask.py
75 lines (48 loc) · 2.58 KB
/
repertoaari_flask.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
from flask import Flask, request
from impl.ui.web.pages import *
from repertoaari import NoSuchDictionary, Repertoaari, FlashContext, InvalidRequest
flask_app = Flask(__name__)
repertoaari = Repertoaari()
@flask_app.route("/")
def hello():
return "Hello, World!"
@flask_app.errorhandler(404)
def on_page_not_found(error):
return ErrorPage('page_not_found.html').display_error('{0} {1}'.format(request.method, request.path)).render(), 404
@flask_app.errorhandler(500)
def on_server_error(error):
return ErrorPage('error.html').display_error(error.description).render(), 500
@flask_app.errorhandler(InvalidRequest)
def on_invalid_request(error):
return ErrorPage('error.html').display_error(error).render(), 400
@flask_app.errorhandler(NoSuchDictionary)
def on_invalid_dictionary(error):
return on_page_not_found(error)
@flask_app.route("/<dict_file>/list")
def show_dictionary_listing(dict_file):
return repertoaari.show_dictionary(TabularPage('listing.html'), dict_file).render()
@flask_app.route("/<dict_file>/exam/random-<int:words>", methods=['GET'])
def show_exam(dict_file, words):
return repertoaari.show_exam(TabularPage('exam.html'), dict_file, words).render()
@flask_app.route("/<dict_file>/exam/random-<int:words>", methods=['POST'])
def assess_exam(dict_file, words):
answers = []
for key in sorted(request.form.keys()):
if key.strip().startswith('answer_'):
elements = key.strip().split('_')
answers.append(Repertoaari.Answer(elements[2], elements[3], request.form[key]))
return repertoaari.assess_exam(TabularPage('exam.html'), dict_file, answers).render()
@flask_app.route("/<dict_file>/flash", methods=['GET', 'POST'])
def show_flash(dict_file):
page = FlashPage('flash.html').with_next('show_assess_flash', dict_file=dict_file).with_stats_button(dict_file)
context = FlashContext.from_str(request.form.get('context'))
return repertoaari.show_flash(page, dict_file, context).render()
@flask_app.route("/<dict_file>/assess_flash", methods=['POST'])
def show_assess_flash(dict_file):
page = FlashPage('flash.html').with_next('show_flash', dict_file=dict_file).with_stats_button(dict_file)
context = FlashContext.from_str(request.form.get('context'))
return repertoaari.assess_flash(page, dict_file, request.form['id'], request.form['answer'], context).render()
@flask_app.route("/<dict_file>/flash_stats", methods=['POST'])
def show_flash_stats(dict_file):
context = FlashContext.from_str(request.form.get('context'))
return repertoaari.show_flash_stats(StatsPage(), dict_file, context).render()