From af98c69723c9d56bfe2023a2c6faaf246619001c Mon Sep 17 00:00:00 2001 From: aranzaiger Date: Wed, 24 Jun 2015 20:09:13 +0300 Subject: [PATCH] added getProjectsByUser and joinProject function --- SE_API/ProjectRoutes.py | 117 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 5 deletions(-) diff --git a/SE_API/ProjectRoutes.py b/SE_API/ProjectRoutes.py index 5c12708..f1848db 100644 --- a/SE_API/ProjectRoutes.py +++ b/SE_API/ProjectRoutes.py @@ -58,7 +58,10 @@ def create_project(token): """ if not request.data: return bad_request() - payload = json.loads(request.data) + try: + payload = json.loads(request.data) + except Exception as e: + return bad_request("invalid JSON format") #if not is_lecturer(token): #todo: change to lecturer id # return forbidden("Invalid token or not a lecturer!") @@ -68,6 +71,7 @@ def create_project(token): #todo: check legality + try: project = Project(projectName=payload['projectName'], courseName=payload['courseName'], master_id=user.key().id(), gitRepository=payload['gitRepository'], membersId=[token]) except Exception as e: @@ -75,6 +79,11 @@ def create_project(token): return bad_request() db.put(project) + + #update user projects list + user.projects_id_list.append(str(project.key().id())) + + db.put(user) db.save return Response(response=project.to_JSON(), status=200, @@ -84,6 +93,50 @@ def create_project(token): # PUT #---------------------------------------------------------- +@project_routes.route('/api/projects/joinProject//', methods=["PUT"]) +@auto.doc() +def joinProject(token, projectId): + """ + This call will add the user (by token) to a specific project +
+ Route Parameters
+ - seToken: 'seToken'
+ - projectId: 123456789 +
+
+ Payload
+ - None
+
+ Response +
+ 202 - Accepted +
+ 400 - Bad Request +
+ 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") + + #---------------------------------------------------------- # GET @@ -138,21 +191,73 @@ def getProjectsByCourseName(name): +@project_routes.route('/api/projects/getProjectsByUser/', methods=["GET"]) +@auto.doc() +def getProjectsByUser(token): + """ + >This Call will return an array of all projects in a given course +
+ Route Parameters
+ - token: 'SEToken' +
+
+ Payload
+ - NONE +
+
+ Response +
+ 200 - JSON Example:
+ + {
+ 'projectName': 'Advance Math',
+ 'courseName': 'JCE',
+ 'grade': 98,
+ 'logo_url': 'http://location.domain.com/image.jpg',
+ 'gitRepository': 'http://location.git.com/somthing',
+ 'membersId': ['bob', 'dylan', 'quentin', 'terentino'],
+ 'id' : 1234567890
+ } +
+
+ """ + + 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") + + + #---------------------------------------------------------- # DELETE #---------------------------------------------------------- -@project_routes.route('/api/projects/deleteProject//', methods=['DELETE']) +@project_routes.route('/api/projects/deleteProject//', methods=['DELETE']) @auto.doc() -def deleteProject(token,projectid): +def deleteProject(token,projectId): """ This Call will delete a specific Project
Route Parameters
- seToken: 'seToken' - - courseid: 'projectid' + - projectId: 'projectid'

Payload
@@ -178,7 +283,9 @@ def deleteProject(token,projectid): # return forbidden("Invalid token or not a lecturer!") user = get_user_by_token(token) - p = Project.get_by_id(int(projectid)) + if user is None: + return bad_request("Bad user Token") + p = Project.get_by_id(int(projectId)) if p is None: return bad_request("no such Project")