se-hub/SE_API/CourseRoutes.py

153 lines
3.9 KiB
Python
Raw Normal View History

__author__ = 'Aran'
from flask import Blueprint
import json
from GithubAPI.GithubAPI import GitHubAPI_Keys
from google.appengine.ext import db
import requests
import datetime
from flask import Flask, request, render_template, redirect, abort, Response
from flask.ext.github import GitHub
from flask.ext.cors import CORS, cross_origin
from flask.ext.autodoc import Autodoc
# DB Models
from models.Course import Course
#Validation Utils Libs
from SE_API.Validation_Utils import *
from SE_API.Respones_Utils import *
course_routes = Blueprint("course_routes", __name__)
auto = Autodoc()
@course_routes.route('/api/courses/create/<string:token>', methods=['POST'])
@auto.doc()
def create_course(token):
"""
2015-06-15 15:58:03 +00:00
<span class="card-title">This call will create a new course in the DB</span>
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'
<br>
<br>
<b>Payload</b><br>
- JSON Object, Example: <br>
{<br>
'courseName': 'Advance Math',<br>
'campusName': 'JCE',<br>
'startDate': '2015-14-3'<br>
'endDate': '2015-29-6'<br>
'taskFlag': 'False'<br>
}<br>
<br>
<br>
<b>Response</b>
<br>
201 - Created
<br>
400 - Bad Request
<br>
403 - Invalid token or not a lecturer
"""
if not request.data:
return bad_request()
if not is_lecturer(token): #todo: change to lecturer id
return forbidden("Invalid token or not a lecturer!")
user = get_user_by_token(token)
2015-06-15 15:35:47 +00:00
#try to parse payload
try:
payload = json.loads(request.data)
except Exception as e:
return bad_request(e)
try:
start_date = datetime.date(payload['startDate']['year'],payload['startDate']['month'],payload['startDate']['day'])
end_date = datetime.date(payload['endDate']['year'],payload['endDate']['month'],payload['endDate']['day'])
2015-06-15 15:58:03 +00:00
if end_date <= start_date:
return bad_request("end date cant be before (or same day) start date")
course = Course(courseName=payload['courseName'], campusName=payload['campusName'],
startDate=start_date, endDate=end_date)
2015-06-15 15:35:47 +00:00
#check if name already exists
try:
2015-06-15 15:35:47 +00:00
query = Course.all()
query.filter("courseName = ", payload['courseName'])
for c in query.run(limit=1):
return forbidden("Campus with same name already exists")
except Exception as e:
print e
except Exception:
return bad_request()
db.put(course)
db.save
return Response(response=course.to_JSON(),
status=201,
mimetype="application/json")
2015-06-15 13:54:12 +00:00
@course_routes.route('/api/courses/getCourseByCampusName/<string:name>', methods=["GET"])
@auto.doc()
def getCourseByCampusName(name):
2015-06-15 15:58:03 +00:00
"""
<span class="card-title">>This Call will return an array of all courses in a given campus</span>
2015-06-15 13:54:12 +00:00
<br>
<b>Route Parameters</b><br>
2015-06-15 15:58:03 +00:00
- name: 'campus name'
2015-06-15 13:54:12 +00:00
<br>
<br>
<b>Payload</b><br>
- NONE
<br>
<br>
<b>Response</b>
<br>
200 - JSON Example:<br>
<code>
{<br>
2015-06-15 15:58:03 +00:00
'courseName': 'Advance Math',<br>
'campusName': 'JCE',<br>
'startDate': '2015-14-3'<br>
'endDate': '2015-29-6'<br>
'taskFlag': 'False'<br>
2015-06-15 13:54:12 +00:00
}
</code>
<br>
2015-06-15 15:58:03 +00:00
"""
2015-06-15 15:35:47 +00:00
arr = []
2015-06-15 13:54:12 +00:00
query = Course.all()
2015-06-15 15:35:47 +00:00
query.filter("campusName = ", name)
2015-06-15 13:54:12 +00:00
2015-06-15 15:35:47 +00:00
for c in query.run():
arr.append(dict(json.loads(c.to_JSON())))
print arr
if len(arr) != 0:
return Response(response=json.dumps(arr),
status=200,
mimetype="application/json")
else:
return Response(response=[],
2015-06-15 13:54:12 +00:00
status=200,
2015-06-15 15:35:47 +00:00
mimetype="application/json")
2015-06-15 13:54:12 +00:00
@course_routes.route('/api/courses/help')
def documentation():
return auto.html()