forked from mitodl/mit_lti_flask_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mit_lti_flask_sample.py
126 lines (95 loc) · 3.18 KB
/
mit_lti_flask_sample.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
import os
from flask import Flask
from flask import render_template
from flask.ext.wtf import Form
from wtforms import IntegerField, BooleanField
from random import randint
from pylti.flask import lti
VERSION = '0.0.1'
app = Flask(__name__)
app.config.from_object('config')
class AddForm(Form):
""" Add data from Form
:param Form:
"""
p1 = IntegerField('p1')
p2 = IntegerField('p2')
result = IntegerField('result')
correct = BooleanField('correct')
def error(exception=None):
""" render error page
:param exception: optional exception
:return: the error.html template rendered
"""
return render_template('error.html')
@app.route('/is_up', methods=['GET'])
def hello_world(lti=lti):
""" Indicate the app is working. Provided for debugging purposes.
:param lti: the `lti` object from `pylti`
:return: simple page that indicates the request was processed by the lti
provider
"""
return render_template('up.html', lti=lti)
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET'])
@app.route('/lti/', methods=['GET', 'POST'])
@lti(request='initial', error=error, app=app)
def index(lti=lti):
""" initial access page to the lti provider. This page provides
authorization for the user.
:param lti: the `lti` object from `pylti`
:return: index page for lti provider
"""
return render_template('index.html', lti=lti)
@app.route('/index_staff', methods=['GET', 'POST'])
@lti(request='session', error=error, role='staff', app=app)
def index_staff(lti=lti):
""" render the contents of the staff.html template
:param lti: the `lti` object from `pylti`
:return: the staff.html template rendered
"""
return render_template('staff.html', lti=lti)
@app.route('/add', methods=['GET'])
@lti(request='session', error=error, app=app)
def add_form(lti=lti):
""" initial access page for lti consumer
:param lti: the `lti` object from `pylti`
:return: index page for lti provider
"""
form = AddForm()
form.p1.data = randint(1, 9)
form.p2.data = randint(1, 9)
return render_template('add.html', form=form)
@app.route('/grade', methods=['POST'])
@lti(request='session', error=error, app=app)
def grade(lti=lti):
""" post grade
:param lti: the `lti` object from `pylti`
:return: grade rendered by grade.html template
"""
form = AddForm()
correct = ((form.p1.data + form.p2.data) == form.result.data)
form.correct.data = correct
lti.post_grade(1 if correct else 0)
return render_template('grade.html', form=form)
def set_debugging():
""" enable debug logging
"""
import logging
import sys
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(name)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
set_debugging()
if __name__ == '__main__':
"""
For if you want to run the flask development server
directly
"""
port = int(os.environ.get("FLASK_LTI_PORT", 5000))
host = os.environ.get("FLASK_LTI_HOST", "localhost")
app.run(debug=True, host=host, port=port)