2015-06-14 22:38:46 +00:00
|
|
|
__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.Project import Project
|
|
|
|
|
|
|
|
#Validation Utils Libs
|
|
|
|
from SE_API.Validation_Utils import *
|
|
|
|
from SE_API.Respones_Utils import *
|
|
|
|
|
|
|
|
project_routes = Blueprint("project_routes", __name__)
|
|
|
|
auto = Autodoc()
|
|
|
|
|
2015-06-23 21:50:59 +00:00
|
|
|
#----------------------------------------------------------
|
|
|
|
# POST
|
|
|
|
#----------------------------------------------------------
|
|
|
|
|
2015-06-15 15:37:08 +00:00
|
|
|
@project_routes.route('/api/projects/create/<string:token>', methods=['POST'])
|
2015-06-14 22:38:46 +00:00
|
|
|
@auto.doc()
|
2015-06-15 15:37:08 +00:00
|
|
|
def create_project(token):
|
2015-06-14 22:38:46 +00:00
|
|
|
"""
|
|
|
|
<span class="card-title">This call will create a new project in the DB</span>
|
|
|
|
<br>
|
|
|
|
<b>Route Parameters</b><br>
|
|
|
|
- seToken: 'seToken'
|
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
<b>Payload</b><br>
|
|
|
|
- JSON Object, Example: <br>
|
|
|
|
{<br>
|
2015-06-15 15:58:03 +00:00
|
|
|
'projectName': 'Advance Math',<br>
|
2015-06-28 13:48:01 +00:00
|
|
|
'courseId': 1234567890,<br>
|
2015-06-21 19:22:13 +00:00
|
|
|
'logo_url': 'http://location.domain.com/image.jpg',<br>
|
2015-06-15 15:58:03 +00:00
|
|
|
'gitRepository': 'http://location.git.com/somthing'<br>
|
2015-06-14 22:38:46 +00:00
|
|
|
}<br>
|
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
<b>Response</b>
|
|
|
|
<br>
|
|
|
|
201 - Created
|
|
|
|
<br>
|
2015-06-15 15:58:03 +00:00
|
|
|
400 - Bad Request
|
|
|
|
<br>
|
|
|
|
403 - Invalid token or not a lecturer
|
2015-06-14 22:38:46 +00:00
|
|
|
"""
|
|
|
|
if not request.data:
|
|
|
|
return bad_request()
|
2015-06-24 17:09:13 +00:00
|
|
|
try:
|
|
|
|
payload = json.loads(request.data)
|
|
|
|
except Exception as e:
|
|
|
|
return bad_request("invalid JSON format")
|
2015-06-14 22:38:46 +00:00
|
|
|
|
|
|
|
user = get_user_by_token(token)
|
2015-06-21 19:22:13 +00:00
|
|
|
if user is None:
|
|
|
|
return bad_request("Wrong user Token")
|
2015-06-14 22:38:46 +00:00
|
|
|
|
2015-06-24 17:09:13 +00:00
|
|
|
|
2015-06-14 22:38:46 +00:00
|
|
|
try:
|
2015-06-28 13:48:01 +00:00
|
|
|
project = Project(projectName=payload['projectName'], courseId=payload['courseId'], master_id=user.key().id(), gitRepository=payload['gitRepository'], membersId=[token])
|
2015-06-14 22:38:46 +00:00
|
|
|
except Exception as e:
|
|
|
|
print e
|
|
|
|
return bad_request()
|
|
|
|
|
|
|
|
db.put(project)
|
2015-06-24 17:09:13 +00:00
|
|
|
|
|
|
|
#update user projects list
|
|
|
|
user.projects_id_list.append(str(project.key().id()))
|
|
|
|
|
|
|
|
db.put(user)
|
2015-06-14 22:38:46 +00:00
|
|
|
db.save
|
|
|
|
return Response(response=project.to_JSON(),
|
2015-06-23 22:20:53 +00:00
|
|
|
status=200,
|
2015-06-14 22:38:46 +00:00
|
|
|
mimetype="application/json")
|
|
|
|
|
2015-06-23 21:50:59 +00:00
|
|
|
#----------------------------------------------------------
|
|
|
|
# PUT
|
|
|
|
#----------------------------------------------------------
|
2015-06-14 22:38:46 +00:00
|
|
|
|
2015-06-24 17:09:13 +00:00
|
|
|
@project_routes.route('/api/projects/joinProject/<string:token>/<string:projectId>', methods=["PUT"])
|
|
|
|
@auto.doc()
|
|
|
|
def joinProject(token, projectId):
|
|
|
|
"""
|
|
|
|
<span class="card-title">This call will add the user (by token) to a specific project</span>
|
|
|
|
<br>
|
|
|
|
<b>Route Parameters</b><br>
|
|
|
|
- seToken: 'seToken'<br>
|
|
|
|
- projectId: 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")
|
|
|
|
|
|
|
|
project = Project.get_by_id(int(projectId))
|
|
|
|
if project is None:
|
|
|
|
return bad_request("No such Project")
|
|
|
|
|
|
|
|
if user.key().id() in project.membersId:
|
|
|
|
return no_content("User is already member in Project")
|
|
|
|
|
|
|
|
project.membersId.append(str(user.key().id()))
|
|
|
|
|
|
|
|
db.put(project)
|
|
|
|
db.save
|
|
|
|
|
|
|
|
return Response(response=project.to_JSON(),
|
|
|
|
status=202,
|
|
|
|
mimetype="application/json")
|
|
|
|
|
|
|
|
|
2015-06-14 22:38:46 +00:00
|
|
|
|
2015-06-23 21:50:59 +00:00
|
|
|
#----------------------------------------------------------
|
|
|
|
# GET
|
|
|
|
#----------------------------------------------------------
|
2015-06-14 22:38:46 +00:00
|
|
|
|
2015-06-29 09:57:37 +00:00
|
|
|
@project_routes.route('/api/projects/getProjectsByCourse/<string:token>/<string:courseId>', methods=["GET"])
|
2015-06-14 22:38:46 +00:00
|
|
|
@auto.doc()
|
2015-06-29 09:57:37 +00:00
|
|
|
def getProjectsByCourse(token, courseId):
|
2015-06-15 15:58:03 +00:00
|
|
|
"""
|
|
|
|
<span class="card-title">>This Call will return an array of all projects in a given course</span>
|
2015-06-14 22:38:46 +00:00
|
|
|
<br>
|
|
|
|
<b>Route Parameters</b><br>
|
2015-06-29 09:57:37 +00:00
|
|
|
- seToken: token<br>
|
2015-06-28 13:48:01 +00:00
|
|
|
- courseId: 1234567890
|
2015-06-14 22:38:46 +00:00
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
<b>Payload</b><br>
|
2015-06-15 15:37:08 +00:00
|
|
|
- NONE
|
2015-06-14 22:38:46 +00:00
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
<b>Response</b>
|
|
|
|
<br>
|
2015-06-15 15:37:08 +00:00
|
|
|
200 - JSON Example:<br>
|
|
|
|
<code>
|
|
|
|
{<br>
|
2015-06-15 15:58:03 +00:00
|
|
|
'projectName': 'Advance Math',<br>
|
2015-06-28 13:48:01 +00:00
|
|
|
'courseId': 123456789,<br>
|
2015-06-15 15:58:03 +00:00
|
|
|
'grade': 98,<br>
|
|
|
|
'logo_url': 'http://location.domain.com/image.jpg',<br>
|
|
|
|
'gitRepository': 'http://location.git.com/somthing',<br>
|
2015-06-21 22:38:14 +00:00
|
|
|
'membersId': ['bob', 'dylan', 'quentin', 'terentino'],<br>
|
|
|
|
'id' : 1234567890<br>
|
2015-06-15 15:37:08 +00:00
|
|
|
}
|
|
|
|
</code>
|
2015-06-14 22:38:46 +00:00
|
|
|
<br>
|
2015-06-15 15:58:03 +00:00
|
|
|
"""
|
2015-06-15 15:37:08 +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:37:08 +00:00
|
|
|
arr = []
|
|
|
|
query = Project.all()
|
2015-06-28 13:48:01 +00:00
|
|
|
query.filter("courseId = ", int(courseId))
|
2015-06-15 15:37:08 +00:00
|
|
|
|
|
|
|
for p in query.run():
|
|
|
|
arr.append(dict(json.loads(p.to_JSON())))
|
|
|
|
print arr
|
|
|
|
if len(arr) != 0:
|
|
|
|
return Response(response=json.dumps(arr),
|
|
|
|
status=200,
|
|
|
|
mimetype="application/json")
|
2015-06-14 22:38:46 +00:00
|
|
|
else:
|
2015-06-15 15:37:08 +00:00
|
|
|
return Response(response=[],
|
|
|
|
status=200,
|
|
|
|
mimetype="application/json")
|
2015-06-14 22:41:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-24 17:09:13 +00:00
|
|
|
@project_routes.route('/api/projects/getProjectsByUser/<string:token>', methods=["GET"])
|
|
|
|
@auto.doc()
|
|
|
|
def getProjectsByUser(token):
|
|
|
|
"""
|
|
|
|
<span class="card-title">>This Call will return an array of all projects in a given course</span>
|
|
|
|
<br>
|
|
|
|
<b>Route Parameters</b><br>
|
|
|
|
- token: 'SEToken'
|
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
<b>Payload</b><br>
|
|
|
|
- NONE
|
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
<b>Response</b>
|
|
|
|
<br>
|
|
|
|
200 - JSON Example:<br>
|
|
|
|
<code>
|
|
|
|
{<br>
|
|
|
|
'projectName': 'Advance Math',<br>
|
|
|
|
'courseName': 'JCE',<br>
|
|
|
|
'grade': 98,<br>
|
|
|
|
'logo_url': 'http://location.domain.com/image.jpg',<br>
|
|
|
|
'gitRepository': 'http://location.git.com/somthing',<br>
|
|
|
|
'membersId': ['bob', 'dylan', 'quentin', 'terentino'],<br>
|
|
|
|
'id' : 1234567890<br>
|
|
|
|
}
|
|
|
|
</code>
|
|
|
|
<br>
|
|
|
|
"""
|
|
|
|
|
|
|
|
user = get_user_by_token(token)
|
|
|
|
if user is None:
|
|
|
|
return bad_request("Bad Token")
|
|
|
|
|
|
|
|
|
|
|
|
arr = []
|
|
|
|
for p in user['projects_id_list']:
|
|
|
|
project = Project.get_by_id(int(p))
|
|
|
|
arr.append(dict(json.loads(project.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-23 21:50:59 +00:00
|
|
|
#----------------------------------------------------------
|
|
|
|
# DELETE
|
|
|
|
#----------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-24 17:09:13 +00:00
|
|
|
@project_routes.route('/api/projects/deleteProject/<string:token>/<string:projectId>', methods=['DELETE'])
|
2015-06-21 22:26:23 +00:00
|
|
|
@auto.doc()
|
2015-06-24 17:09:13 +00:00
|
|
|
def deleteProject(token,projectId):
|
2015-06-21 22:26:23 +00:00
|
|
|
"""
|
|
|
|
<span class="card-title">This Call will delete a specific Project</span>
|
|
|
|
<br>
|
|
|
|
<b>Route Parameters</b><br>
|
|
|
|
- seToken: 'seToken'
|
2015-06-24 17:09:13 +00:00
|
|
|
- projectId: 'projectid'
|
2015-06-21 22:26:23 +00:00
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
<b>Payload</b><br>
|
|
|
|
- NONE <br>
|
|
|
|
<br>
|
|
|
|
<br>
|
|
|
|
<b>Response</b>
|
|
|
|
<br>
|
|
|
|
202 - Deleted Project
|
|
|
|
<br>
|
|
|
|
....<br>
|
|
|
|
{<br>
|
|
|
|
...<br>
|
|
|
|
}req<br>
|
|
|
|
|
|
|
|
]<br>
|
|
|
|
400 - no such Project
|
|
|
|
<br>
|
|
|
|
403 - Invalid token or not the owner of Project!<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)
|
2015-06-24 17:09:13 +00:00
|
|
|
if user is None:
|
|
|
|
return bad_request("Bad user Token")
|
|
|
|
p = Project.get_by_id(int(projectId))
|
2015-06-21 22:26:23 +00:00
|
|
|
|
|
|
|
if p is None:
|
|
|
|
return bad_request("no such Project")
|
|
|
|
|
|
|
|
if p.master_id == user.key().id():
|
|
|
|
db.delete(p)
|
|
|
|
db.save
|
|
|
|
return accepted("Project deleted")
|
|
|
|
|
|
|
|
return forbidden("user is not owner of Project")
|
|
|
|
|
|
|
|
|
2015-06-15 13:32:59 +00:00
|
|
|
|
2015-06-23 21:50:59 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------
|
|
|
|
# DOCUMENTATION
|
|
|
|
#----------------------------------------------------------
|
|
|
|
|
2015-06-14 22:41:53 +00:00
|
|
|
@project_routes.route('/api/projects/help')
|
|
|
|
def documentation():
|
|
|
|
return auto.html()
|