-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
118 lines (87 loc) · 3.7 KB
/
server.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
import sys
sys.path.insert(1, './castor/api')
from user import get_users, get_user
from study import get_studies, get_study, get_study_user
from export_data import export_study_data
from institute import get_study_institutes, get_study_institute
from record import get_study_records, get_study_record
from step import get_study_step
from flask import Flask, render_template, request
from json import dumps, loads
from datetime import datetime as dt
import os
f = open('devel.env', 'r')
env = loads(f.read())
for key in env.keys():
os.environ[key] = env[key]
app = Flask(__name__)
#
# FILTER TEMPLATES
#
@app.template_filter('dumpjson')
def dumpjson(jsonObject):
return dumps(jsonObject, indent=2)
@app.template_filter('date')
def date(datetimeString):
dateTime = dt.strptime(datetimeString, '%Y-%m-%d %H:%M:%S.%f')
return '{:%Y-%m-%d %H:%M}'.format(dateTime)
#
# ROUTES
#
@app.route('/')
@app.route('/index')
def index():
usersData = get_users()
studiesData = get_studies()
return render_template('index.html', users=usersData['_embedded']['user'], studies=studiesData['_embedded']['study'])
@app.route('/study/<string:studyID>/institutes')
def study_institutes(studyID):
domain=request.args.get('domain')
institutesData = get_study_institutes(studyID, domain)
institutes = institutesData['_embedded']['institutes']
attributes = institutes[0].keys()
attributes.remove('_links')
attributes.remove('id')
attributes.remove('institute_id')
return render_template('study_institutes.html', studyID=studyID, data=institutes, keys=sorted(attributes), size=len(institutes), domain=domain)
@app.route('/study/<string:studyID>/records')
def study_records(studyID):
domain=request.args.get('domain')
recordsData = get_study_records(studyID, domain)
records = recordsData['_embedded']['records']
attributes = records[0].keys()
attributes.remove('_embedded')
attributes.remove('_links')
attributes.remove('id')
attributes.remove('record_id')
return render_template('study_records.html', studyID=studyID, data=records, keys=sorted(attributes), size=len(records), domain=domain)
@app.route('/api/user/<string:userID>')
def user(userID):
userData = get_user(userID, domain=request.args.get('domain'))
return render_template('json_viewer.html', data=userData)
@app.route('/api/study/<string:studyID>')
def study(studyID):
studyData = get_study(studyID, domain=request.args.get('domain'))
return render_template('json_viewer.html', data=studyData)
@app.route('/api/study/<string:studyID>/user/<string:userID>')
def study_user(studyID, userID):
studyUserData = get_study_user(studyID, userID, domain=request.args.get('domain'))
return render_template('json_viewer.html', data=studyUserData)
@app.route('/api/study/<string:studyID>/step/<string:stepID>')
def study_step(studyID, stepID):
studyStepData = get_study_step(studyID, stepID, domain=request.args.get('domain'))
return render_template('json_viewer.html', data=studyStepData)
@app.route('/api/study/<string:studyID>/record/<string:recordID>')
def record(studyID, recordID):
recordData = get_study_record(recordID, studyID, domain=request.args.get('domain'))
return render_template('json_viewer.html', data=recordData)
@app.route('/api/study/<string:studyID>/institute/<string:instituteID>')
def institute(studyID, instituteID):
instituteData = get_study_institute(instituteID, studyID, domain=request.args.get('domain'))
return render_template('json_viewer.html', data=instituteData)
@app.route('/api/study/<string:studyID>/export/data')
def export_data(studyID):
exportData = export_study_data(studyID, domain=request.args.get('domain'))
return render_template('json_viewer.html', data=exportData)
if __name__ == '__main__':
app.run()