-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·152 lines (110 loc) · 4.52 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
#!/usr/bin/env python3
import jwt
import logging
from prokit import smartclient
from flask import Flask, redirect, session, request, render_template, jsonify
from fhirclient import client
from fhirclient.models import practitioner
app = Flask(__name__, template_folder='app/templates')
app_secret = "applicaitonsecret123"
app.secret_key = app_secret
app.debug = True
app.user_practitioner = None
smart_settings = {
'app_id' : 'pro_app2',
'redirect_uri' : 'http://127.0.0.1:8000/callback',
'scope' : 'openid fhirUser launch launch/patient',
'app_secret' : app_secret,
'patient_id' : '39234650-0229-4aee-975b-c8ee68bab40b'
}
def _save_state(state):
session['state'] = state
session.modified = True
def _smart_client():
state = session.get('state')
if state:
return smartclient.PROClient(state=state, save_func=_save_state)
else:
return smartclient.PROClient(settings=smart_settings, save_func=_save_state)
@app.route('/launch.html')
def app_launch():
smart_settings.update({'api_base': request.args.get('iss', '')})
smart = _smart_client()
if smart.ready and smart.patient is not None:
return redirect('/index.html')
else:
auth_url = smart.authorize_url
if auth_url:
return redirect(auth_url)
else:
return msg('error authorizing')
@app.route('/requests/new')
def request_pgd():
smart = _smart_client()
instrument_type = request.args.get('instrumenttype')
instrument_id = request.args.get('instrumentid')
schedule_type = request.args.get('sch_type', 'instant')
start_date = request.args.get('start', None)
end_date = request.args.get('end', None)
if schedule_type != 'instant':
if not start_date or not end_date:
return jsonify(result={'result': 'fail', 'message': 'Please select a start and an end date'})
result = smart.dispatch_request(instrumenttype=instrument_type, instrumentidentifier=instrument_id, practitioner_resource=app.user_practitioner, schedule_type=schedule_type, start_date=start_date, end_date=end_date)
return jsonify(result={'result':'success', 'request_id': result.identifier, 'request_title': result.title})
# request_schedule = None
# if schedule_type != 'instant':
# if start_date is None or end_date is None:
# return jsonify(result={'result': 'fail', 'message': 'Please select a start and an end date'})
# request_schedule = PROSchedule()
# if instr is None:
# return msg('No')
# success = smart.dispatchRequest(selected_instrument=instr, practitioner_resource=app.user_practitioner, selected_schedule=None)
# if success:
# return jsonify(result={'result':'success', 'request_id': success.identifier, 'request_title': success.title})
# else:
# return jsonify(result={'result': 'fail'})
@app.route('/index.html')
def app_main():
smart = _smart_client()
print(session['state'])
if app.user_practitioner is None:
idtoken = session['state']['launch_context']['id_token']
if idtoken is None:
return msg('id-token not found')
jwtDecoded = jwt.decode(idtoken, verify=False)
practitioner_id = jwtDecoded['fhirUser'].split('/')[1]
app.user_practitioner = smart.user_practitioner(practitioner_id)
return render_template('index.html',
page='request',
proclient=smart,
observations=smart.getobservations(),
requests=smart.getrequests(),
questionnaires=smart.instrumentlist_questionnaires,
promis=smart.instrumentlist_promis,
activetasks=smart.instrumentlist_activetasks,
activityinstruments=smart.instrumentList_activity,
clinicalrecords=smart.instrumentlist_clinicalrecords,
devices=smart.instrumentlist_devices
)
@app.route('/reset')
def reset():
_reset()
return msg('Deleted session')
def _reset():
if 'state' in session:
del session['state']
@app.route('/callback')
def auth_callback():
smart = _smart_client()
try:
smart.handle_callback(request.url)
except Exception as e:
return """<h1>Authorization Error</h1>{0}""".format(e)
return redirect('/index.html')
def msg(test):
return f'{test}'
def log(msg):
logging.debug(msg)
if '__main__' == __name__:
logging.basicConfig(level=logging.DEBUG)
app.run(debug=True, port=8000)