-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathviews.py
86 lines (67 loc) · 2.4 KB
/
views.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
from flask import Flask, render_template, session, request, Response
from pylti.flask import lti
import settings
import logging
import json
from logging.handlers import RotatingFileHandler
app = Flask(__name__)
app.secret_key = settings.secret_key
app.config.from_object(settings.configClass)
# ============================================
# Logging
# ============================================
formatter = logging.Formatter(settings.LOG_FORMAT)
handler = RotatingFileHandler(
settings.LOG_FILE,
maxBytes=settings.LOG_MAX_BYTES,
backupCount=settings.LOG_BACKUP_COUNT
)
handler.setLevel(logging.getLevelName(settings.LOG_LEVEL))
handler.setFormatter(formatter)
app.logger.addHandler(handler)
# ============================================
# Utility Functions
# ============================================
def return_error(msg):
return render_template('error.html', msg=msg)
def error(exception=None):
app.logger.error("PyLTI error: {}".format(exception))
return return_error('''Authentication error,
please refresh and try again. If this error persists,
please contact support.''')
# ============================================
# Web Views / Routes
# ============================================
# LTI Launch
@app.route('/launch', methods=['POST', 'GET'])
@lti(error=error, request='initial', role='any', app=app)
def launch(lti=lti):
"""
Returns the launch page
request.form will contain all the lti params
"""
# example of getting lti data from the request
# let's just store it in our session
session['lis_person_name_full'] = request.form.get('lis_person_name_full')
# Write the lti params to the console
app.logger.info(json.dumps(request.form, indent=2))
return render_template('launch.html', lis_person_name_full=session['lis_person_name_full'])
# Home page
@app.route('/', methods=['GET'])
def index(lti=lti):
return render_template('index.html')
# LTI XML Configuration
@app.route("/xml/", methods=['GET'])
def xml():
"""
Returns the lti.xml file for the app.
XML can be built at https://www.eduappcenter.com/
"""
try:
return Response(render_template(
'lti.xml'), mimetype='application/xml'
)
except:
app.logger.error("Error with XML.")
return return_error('''Error with XML. Please refresh and try again. If this error persists,
please contact support.''')