se-hub/SE_API/CourseRoutes.py

467 lines
12 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
2015-06-21 19:18:48 +00:00
from operator import itemgetter
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
2015-06-21 19:18:48 +00:00
from models.Campus import Campus
from models.Message import Message
#Validation Utils Libs
from SE_API.Validation_Utils import *
from SE_API.Respones_Utils import *
course_routes = Blueprint("course_routes", __name__)
auto = Autodoc()
2015-06-21 19:18:48 +00:00
#----------------------------------------------------------
# POST
#----------------------------------------------------------
@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>
'campusId': 1234567890,<br>
'startDate': {'year': 2015, 'month' : 4, 'day' : 3},<br>
'endDate': {'year': 2016, 'month' : 5, 'day' : 14}<br>
}<br>
2015-06-15 15:58:03 +00:00
<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("no data")
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("incorrect JSON format")
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'], campusId=payload['campusId'], master_id=user.key().id(),
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()
2015-06-28 14:30:22 +00:00
query.filter('campusId = ', payload['campusId'])
2015-06-15 15:35:47 +00:00
query.filter("courseName = ", payload['courseName'])
for c in query.run(limit=1):
return forbidden("Course with same name already exists")
2015-06-15 15:35:47 +00:00
except Exception as e:
print e
except Exception as e:
print e
return bad_request(2)
2015-06-24 17:23:50 +00:00
#add user to course membersId list
course.membersId.append(str(user.key().id()))
db.put(course)
#add course to user course list
user.courses_id_list.append(str(course.key().id()))
db.put(user)
db.save
return Response(response=course.to_JSON(),
status=201,
mimetype="application/json")
2015-06-24 17:23:50 +00:00
#----------------------------------------------------------
# PUT
#----------------------------------------------------------
@course_routes.route('/api/courses/joinCourse/<string:token>/<string:courseId>', methods=["PUT"])
@auto.doc()
def joinCourse(token, courseId):
"""
<span class="card-title">This call will add the user (by token) to a specific course</span>
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'<br>
- courseId: 123456789
<br>
<br>
<b>Payload</b><br>
- None <br>
<br>
<b>Response</b>
<br>
202 - Accepted
<br>
400 - Bad Request
<br>
403 - Invalid token or not a lecturer
"""
user = get_user_by_token(token)
if user is None:
return bad_request("Wrong user Token")
course = Course.get_by_id(int(courseId))
if course is None:
return bad_request("No such course")
if user.key().id() in course.membersId:
return no_content("User is already member in Project")
course.membersId.append(str(user.key().id()))
user.courses_id_list.append(str(course.key().id()))
db.put(course)
db.put(user)
db.save
return Response(response=course.to_JSON(),
status=202,
mimetype="application/json")
2015-06-21 19:18:48 +00:00
#----------------------------------------------------------
# GET
#----------------------------------------------------------
2015-06-29 09:57:37 +00:00
@course_routes.route('/api/courses/getAllCoursesByCampus/<string:token>/<string:campusId>', methods=["GET"])
2015-06-15 13:54:12 +00:00
@auto.doc()
2015-06-29 09:57:37 +00:00
def getAllCoursesByCampus(token, campusId):
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>
- campusId: 1234567890
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>
'campusId': 1234567890,<br>
2015-06-15 15:58:03 +00:00
'startDate': '2015-14-3'<br>
'endDate': '2015-29-6'<br>
'taskFlag': 'False'<br>
2015-06-21 22:38:14 +00:00
'id' : 1234567890<br>
2015-06-15 13:54:12 +00:00
}
</code>
<br>
2015-06-15 15:58:03 +00:00
"""
2015-06-29 09:57:37 +00:00
if get_user_by_token(token) is None:
return bad_request("Bad User Token")
2015-06-15 15:35:47 +00:00
arr = []
2015-06-15 13:54:12 +00:00
query = Course.all()
query.filter("campusId = ", int(campusId))
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
2015-06-29 09:57:37 +00:00
@course_routes.route('/api/courses/getUserCoursesByCampus/<string:token>/<string:campusId>', methods=['GET'])
@auto.doc()
2015-06-29 09:57:37 +00:00
def getUserCoursesByCampus(token, campusId):
"""
2015-06-29 09:57:37 +00:00
<span class="card-title">This Call will return an array of all Courses of a certain User in a specific Campus</span>
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'<br>
- campusId: 1234354543<br>
<br>
<br>
<b>Payload</b><br>
- NONE <br>
<br>
<br>
<b>Response</b>
<br>
200 - JSON Array, Example:<br>
[<br>
{
'title': 'JCE',<br>
'email_ending': '@post.jce.ac.il',<br>
'master_user_id': 123453433341, (User that created the campus)<br>
'avatar_url': 'http://some.domain.com/imagefile.jpg',<br>
'id' : 1234567890<br>
},<br>
....<br>
{<br>
...<br>
}req<br>
]<br>
<br>
403 - Invalid Token<br>
"""
user = get_user_by_token(token)
if user is None:
return bad_request("Bad user Token")
campus = Campus.get_by_id(int(campusId))
if campus is None:
return bad_request("No such Campus")
arr = []
for i in user.courses_id_list:
course = Course.get_by_id(int(i))
if course.campusId == campus.key().id():
arr.append(dict(json.loads(course.to_JSON())))
if len(arr) != 0:
return Response(response=json.dumps(arr),
status=200,
mimetype="application/json")
else:
return Response(response='[]',
status=200,
mimetype="application/json")
2015-06-29 09:57:37 +00:00
@course_routes.route('/api/courses/getCoursesByUser/<string:token>/<string:userId>', methods=['GET'])
@auto.doc()
def getCoursesByUser(token, userId):
"""
<span class="card-title">This Call will return an array of all Courses of a certain User</span>
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'<br>
- userId: 1234354543<br>
<br>
<br>
<b>Payload</b><br>
- NONE <br>
<br>
<br>
<b>Response</b>
<br>
200 - JSON Array, Example:<br>
[<br>
{
'title': 'JCE',<br>
'email_ending': '@post.jce.ac.il',<br>
'master_user_id': 123453433341, (User that created the campus)<br>
'avatar_url': 'http://some.domain.com/imagefile.jpg',<br>
'id' : 1234567890<br>
},<br>
....<br>
{<br>
...<br>
}req<br>
]<br>
<br>
403 - Invalid Token<br>
"""
user = get_user_by_token(token)
if user is None:
return bad_request("Bad user Token")
otherUser = User.get_by_id(int(userId))
if otherUser is None:
return bad_request("Bad user Id")
arr = []
for i in otherUser.courses_id_list:
print i
course = Course.get_by_id(int(i))
arr.append(dict(json.loads(course.to_JSON())))
if len(arr) != 0:
return Response(response=json.dumps(arr),
status=200,
mimetype="application/json")
else:
return Response(response='[]',
status=200,
mimetype="application/json")
2015-06-21 19:18:48 +00:00
#----------------------------------------------------------
# PUT
#----------------------------------------------------------
#----------------------------------------------------------
# DELETE
#----------------------------------------------------------
@course_routes.route('/api/courses/deleteCourse/<string:token>/<string:courseid>', methods=['DELETE'])
2015-06-21 19:18:48 +00:00
@auto.doc()
def deleteCourse(token, courseid):
2015-06-21 19:18:48 +00:00
"""
<span class="card-title">This Call will delete a specific Course</span>
2015-06-21 19:18:48 +00:00
<br>
<b>Route Parameters</b><br>
- seToken: 'seToken'<br>
- courseid: 'courseid'
2015-06-21 19:18:48 +00:00
<br>
<br>
<b>Payload</b><br>
- NONE <br>
<br>
<br>
<b>Response</b>
<br>
202 - Deleted Course
2015-06-21 19:18:48 +00:00
<br>
....<br>
{<br>
...<br>
}req<br>
]<br>
400 - no such Course
2015-06-21 19:18:48 +00:00
<br>
403 - Invalid token or not a lecturer or lecturer is not owner of Course!<br>
2015-06-21 19:18:48 +00:00
"""
if not is_lecturer(token): #todo: change to lecturer id
return forbidden("Invalid token or not a lecturer!")
user = get_user_by_token(token)
c = Course.get_by_id(int(courseid))
2015-06-21 19:18:48 +00:00
if c is None:
return bad_request("no such course")
2015-06-21 19:18:48 +00:00
if c.master_id == user.key().id():
2015-06-21 19:18:48 +00:00
db.delete(c)
db.save
return accepted("course deleted")
return forbidden("lecturer is not owner of course")
2015-06-21 19:18:48 +00:00
# @course_routes.route('/api/courses/deleteCoursesByCampus/<string:token>/<string:campusid>', methods=['DELETE'])
# @auto.doc()
# def deleteCoursesByCampus(token,campusName):
# """
# <span class="card-title">This Call will delete a specific campus's courses</span>
# <br>
# <b>Route Parameters</b><br>
# - seToken: 'seToken'
# - title: 'campusName'
# <br>
# <br>
# <b>Payload</b><br>
# - NONE <br>
# <br>
# <br>
# <b>Response</b>
# <br>
# 202 - Deleted campus
# <br>
# 204 - No Matching Campus Found
# <br>
# ....<br>
# {<br>
# ...<br>
# }req<br>
#
# ]<br>
# 400 - Bad Request
# <br>
# 403 - Invalid token or not a lecturer!<br>
# """
#
# if not is_lecturer(token): #todo: change to lecturer id
# return forbidden("Invalid token or not a lecturer!")
#
#
# user = get_user_by_token(token)
# campus = get_campus_by_campusName(campusName)
# if campus is None:
# return bad_request("Not a campus!")
#
# #check user is owner of campus
# if campus.master_user_id != user.key().id():
# return forbidden("lecturer is not owner of campus!")
#
# query = Course.all()
#
# try:
# query.filter('campusName =', campusName)
# except Exception as e:
# print e
# return bad_request("invalid course title attribute")
#
# for c in query.run():
# db.delete(c)
# db.save
#
# return no_content()
2015-06-21 19:18:48 +00:00
2015-06-15 13:54:12 +00:00
2015-06-23 21:50:59 +00:00
#----------------------------------------------------------
# DOCUMENTATION
#----------------------------------------------------------
@course_routes.route('/api/courses/help')
def documentation():
return auto.html()