forked from ejamesc/cors-api
-
Notifications
You must be signed in to change notification settings - Fork 2
/
corsapi.py
92 lines (81 loc) · 2.6 KB
/
corsapi.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
#!/usr/bin/env python
"""Flask webapp. Exposes a RESTful API.
"""
from flask import Flask, url_for, render_template, abort, Response, request
import gc
from pymongo import Connection
import re
try:
import simplejson as json
except ImportError:
import json
app = Flask(__name__)
def js_response_helper(data_json, mime):
"""Assumes that callback is last arg.
"""
res = ""
url = request.url
cb_reg = re.compile('callback=([^&]*)')
cb = cb_reg.search(url)
if cb:
res = cb.expand("\g<1>")
res = res + "(" + data_json + ");"
return Response(res, mimetype='application/javascript')
else:
res = data_json
return Response(res, mimetype=mime)
# mongoDB connection
connection = Connection('localhost', 34006) # the default is 27017
db = connection.corsdatabase
errorjson = json.dumps({"message": "Not Found"})
@app.route('/')
def main():
return render_template('front.html')
@app.route('/modules/', methods=['GET'])
def get_all_modules():
"""Returns all the modules.
"""
entities = db['modules'].find()
if not entities:
return js_response_helper(errorjson, 'application/json')
ls = []
for e in entities:
del e['_id']
ls.append(e)
return js_response_helper(json.dumps(ls), 'application/json')
@app.route('/module/<modulecode>', methods=['GET'])
def get_module(modulecode):
"""Returns details for a specific module
:params string modulecode
"""
entity = db['modules'].find_one({'code': modulecode})
if not entity:
return js_response_helper(errorjson, 'application/json')
del entity['_id'] # the _id object isn't JSON serializable
return js_response_helper(json.dumps(entity), 'application/json')
@app.route('/modules/search/<regex>', methods=['GET'])
def search_modules(regex):
"""Returns details for all modules that match given regex
:params string regex
"""
entities = db['modules'].find({'code': re.compile('%s'%regex) })
ls = []
for e in entities:
del e['_id']
ls.append(e)
if not ls:
return js_response_helper(errorjson, 'application/json')
return js_response_helper(json.dumps(ls), 'application/json')
@app.route('/timetable/<modulecode>', methods=['GET'])
def get_module_time(modulecode):
"""Returns lecture and tutorial timetables
:params string modulecode
"""
entity = db['modules'].find_one({'code': modulecode})
if not entity:
return js_response_helper(errorjson, 'application/json')
return js_response_helper(json.dumps({'lecture_time_table': entity['lecture_time_table'],
'tutorial_time_table': entity['tutorial_time_table']}),
'application/json')
if __name__ == '__main__':
app.run(debug=True)