-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
103 lines (93 loc) · 3.19 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
#!/usr/bin/env python
from flask import Flask, url_for, render_template, abort, Response
from flask import request
import gc
from pymongo import Connection
import pymongo
import re
try:
import simplejson as json
except ImportError:
import json
app = Flask(__name__)
# mongoDB connection
connection = Connection('localhost', 27017) # the default is 27017
db = connection.ModuleDatabase
errorjson = json.dumps({"message": "Not Found"})
MODULE_CODE_REGEX = "[A-Z]{2,3}[0-9]{3,4}[A-Z]{0,2}"
def getModuleCode(lst):
moduleList = []
if type(lst) == list:
for item in lst:
submods = getModuleCode(item)
moduleList.extend(submods)
else:
if re.match(MODULE_CODE_REGEX, str(lst)):
moduleList.append(lst)
return moduleList
@app.route('/')
def get_home():
return render_template('home.html')
@app.route('/modules/', methods=['GET'])
def get_all_modules():
"""Returns all the modules.
"""
entities = db['modules'].find()
if not entities:
return Response(errorjson, mimetype='application/json')
allModules = []
desc = []
for e in entities:
module = {}
module['code'] = e['code']
module['req'] = e['prereq']
module['exc'] = e['preexc']
if module['code']:
allModules.append(module['code'])
allModules.extend(getModuleCode(module['req']))
allModules.extend(getModuleCode(module['exc']))
desc.append(module)
allModuleList = list(set(allModules))
response = {
'listModule': allModuleList,
'desc': desc
}
gc.collect()
return Response(json.dumps(response), mimetype='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 Response(errorjson, mimetype='application/json')
del entity['_id'] # the _id object isn't JSON serializable
gc.collect()
return Response(json.dumps(entity), mimetype='application/json')
@app.route('/search', methods=['GET'])
def search_modules():
"""Returns details for all modules that match given regex
:params string regex
"""
regex = request.args.get('term', '')
print regex
entities1 = db['modules'].find({'code': re.compile('%s'%regex, re.I)}) \
.sort('code', pymongo.ASCENDING).limit(10)
entities2 = db['modules'].find({'name': re.compile('%s'%regex, re.I)}) \
.sort('code', pymongo.ASCENDING).limit(10)
ls = []
appeared = []
for entities in [entities1, entities2]:
for e in entities:
if e['code'] not in appeared and len(ls) < 10:
appeared.append(e['code'])
title = e['code'] + ': ' + e['name']
ls.append({
'label': title,
'value': e['code']
})
gc.collect()
return Response(json.dumps(ls), mimetype='application/json')
if __name__ == '__main__':
app.run(debug=True)