Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from nushackers/master
Pull darora's changes
- Loading branch information
Showing
1 changed file
with
71 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,72 @@ | ||
#!/usr/bin/env python | ||
"""Flask webapp. Exposes a RESTful API. | ||
""" | ||
from flask import Flask, url_for, render_template, abort, Response | ||
from pymongo import Connection | ||
try: | ||
import simplejson as json | ||
except ImportError: | ||
import json | ||
|
||
app = Flask(__name__) | ||
|
||
# mongoDB connection | ||
connection = Connection('localhost', 34006) # the default is 27017 | ||
db = connection.corsdatabase | ||
|
||
@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 Response(json.dumps({"message": "Not Found"}), mimetype='application/json') | ||
ls = [] | ||
for e in entities: | ||
del e['_id'] | ||
ls.append(e) | ||
return Response(json.dumps(ls), 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(json.dumps({"message": "Not Found"}), mimetype='application/json') | ||
del entity['_id'] # the _id object isn't JSON serializable | ||
return Response(json.dumps(entity), mimetype='application/json') | ||
|
||
@app.route('/timetable/<modulecode>', methods=['GET']) | ||
def get_module_time(modulecode): | ||
"""Returns lecture and tutorial timetables | ||
""" | ||
entity = db['modules'].find_one({'code': modulecode}) | ||
if not entity: | ||
return Response(json.dumps({"message": "Not Found"}), mimetype='application/json') | ||
return Response(json.dumps({'lecture_time_table': entity['lecture_time_table'], | ||
'tutorial_time_table': entity['tutorial_time_table']}), | ||
mimetype='application/json') | ||
|
||
if __name__ == '__main__': | ||
#!/usr/bin/env python | ||
"""Flask webapp. Exposes a RESTful API. | ||
""" | ||
from flask import Flask, url_for, render_template, abort, Response | ||
from pymongo import Connection | ||
import re | ||
try: | ||
import simplejson as json | ||
except ImportError: | ||
import json | ||
|
||
app = Flask(__name__) | ||
|
||
# mongoDB connection | ||
connection = Connection('localhost', 34006) # the default is 27017 | ||
db = connection.corsdatabase | ||
|
||
@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 Response(json.dumps({"message": "Not Found"}), mimetype='application/json') | ||
ls = [] | ||
for e in entities: | ||
del e['_id'] | ||
ls.append(e) | ||
return Response(json.dumps(ls), 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(json.dumps({"message": "Not Found"}), mimetype='application/json') | ||
del entity['_id'] # the _id object isn't JSON serializable | ||
return Response(json.dumps(entity), mimetype='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) }) | ||
if not entities: | ||
return Response(json.dumps({"message": "No matches"}), mimetype='application/json') | ||
ls = [] | ||
for e in entities: | ||
del e['_id'] | ||
ls.append(e) | ||
return Response(json.dumps(ls), mimetype='application/json') | ||
|
||
@app.route('/timetable/<modulecode>', methods=['GET']) | ||
def get_module_time(modulecode): | ||
"""Returns lecture and tutorial timetables | ||
""" | ||
entity = db['modules'].find_one({'code': modulecode}) | ||
if not entity: | ||
return Response(json.dumps({"message": "Not Found"}), mimetype='application/json') | ||
return Response(json.dumps({'lecture_time_table': entity['lecture_time_table'], | ||
'tutorial_time_table': entity['tutorial_time_table']}), | ||
mimetype='application/json') | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |