diff --git a/SE_API/CampusRoutes.py b/SE_API/CampusRoutes.py index 6f73148..c49fb3e 100644 --- a/SE_API/CampusRoutes.py +++ b/SE_API/CampusRoutes.py @@ -153,20 +153,72 @@ def get_campuses(token): return forbidden("Invalid Token") +@campus_routes.route('/api/campuses/getCampusesByUser/', methods=['GET']) +@auto.doc() +def getCampusesByUser(token): + """ + This Call will return an array of all Campuses of a certain User +
+ Route Parameters
+ - seToken: 'seToken' +
+
+ Payload
+ - NONE
+
+
+ Response +
+ 200 - JSON Array, Example:
+ [
+ { + 'title': 'JCE',
+ 'email_ending': '@post.jce.ac.il',
+ 'master_user_id': 123453433341, (User that created the campus)
+ 'avatar_url': 'http://some.domain.com/imagefile.jpg',
+ 'id' : 1234567890
+ },
+ ....
+ {
+ ...
+ }req
+ ]
+
+ 403 - Invalid Token
+ """ + + user = get_user_by_token(token) + if user is None: + return bad_request("Bad user Token") + + arr = [] + for i in user['campuses_id_list']: + campus = Campus.get_by_id(int(i)) + arr.append(dict(json.loads(campus.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 #---------------------------------------------------------- -@campus_routes.route('/api/campuses/deleteCampus//', methods=['DELETE']) +@campus_routes.route('/api/campuses/deleteCampus//', methods=['DELETE']) @auto.doc() -def deleteCampus(token,campusid): +def deleteCampus(token,campusId): """ This Call will delete a specific campus
Route Parameters
- seToken: 'seToken' - - campusid: 'campusid' + - campusId: 1234567890

Payload
@@ -192,7 +244,7 @@ def deleteCampus(token,campusid): return forbidden("Invalid token or not a lecturer!") user = get_user_by_token(token) - camp = Campus.get_by_id(int(campusid)) + camp = Campus.get_by_id(int(campusId)) if camp is None: return bad_request("no such campus") diff --git a/SE_API/CourseRoutes.py b/SE_API/CourseRoutes.py index 972b047..7075f36 100644 --- a/SE_API/CourseRoutes.py +++ b/SE_API/CourseRoutes.py @@ -47,13 +47,12 @@ def create_course(token):
Payload
- JSON Object, Example:
- {
- 'courseName': 'Advance Math',
- 'campusName': 'JCE',
- 'startDate': {'year': 2015, 'month' : 4, 'day' : 3}
- 'endDate': {'year': 2016, 'month' : 5, 'day' : 14}
- 'taskFlag': false
- }
+ {
+ 'courseName': 'Advance Math',
+ 'campusName': 'JCE',
+ 'startDate': {'year': 2015, 'month' : 4, 'day' : 3},
+ 'endDate': {'year': 2016, 'month' : 5, 'day' : 14}
+ }


Response @@ -75,7 +74,7 @@ def create_course(token): try: payload = json.loads(request.data) except Exception as e: - return bad_request() + return bad_request("incorrect JSON format") try: start_date = datetime.date(payload['startDate']['year'],payload['startDate']['month'],payload['startDate']['day']) @@ -98,9 +97,16 @@ def create_course(token): except Exception as e: print e - return bad_request() + return bad_request(2) + #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, @@ -149,17 +155,72 @@ def createMessage(token): return bad_request("here") try: - msg = Message(courseName=payload['courseName'], message=payload['message'], msgDate=datetime.datetime.now()) + msg = Message(groupId=payload['groupId'], message=payload['message'], msgDate=datetime.datetime.now(), master_id=user.key().id()) except Exception as e: print e return bad_request("there") + try: + msg['isProject'] = payload['isProject'] + except Exception as e: + pass + db.save(msg) db.save return created() +#---------------------------------------------------------- +# PUT +#---------------------------------------------------------- + +@course_routes.route('/api/courses/joinCourse//', methods=["PUT"]) +@auto.doc() +def joinCourse(token, courseId): + """ + This call will add the user (by token) to a specific course +
+ Route Parameters
+ - seToken: 'seToken'
+ - courseId: 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") + + 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") + + #---------------------------------------------------------- # GET #---------------------------------------------------------- @@ -211,6 +272,66 @@ def getCourseByCampusName(name): status=200, mimetype="application/json") +@course_routes.route('/api/courses/getCoursesByUser//', methods=['GET']) +@auto.doc() +def getCampusesByUser(token,campusId): + """ + This Call will return an array of all Campuses of a certain User +
+ Route Parameters
+ - seToken: 'seToken'
+ - campusId: 1234354543
+
+
+ Payload
+ - NONE
+
+
+ Response +
+ 200 - JSON Array, Example:
+ [
+ { + 'title': 'JCE',
+ 'email_ending': '@post.jce.ac.il',
+ 'master_user_id': 123453433341, (User that created the campus)
+ 'avatar_url': 'http://some.domain.com/imagefile.jpg',
+ 'id' : 1234567890
+ },
+ ....
+ {
+ ...
+ }req
+ ]
+
+ 403 - Invalid Token
+ """ + + 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.courseName == campus.title: + 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") + + @course_routes.route('/api/courses/getMessagesByCourseName/', methods=["GET"]) @auto.doc() def getMessagesByCourseName(name): @@ -286,7 +407,7 @@ def deleteCourse(token,courseid): This Call will delete a specific Course
Route Parameters
- - seToken: 'seToken' + - seToken: 'seToken'
- courseid: 'courseid'

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") diff --git a/SE_API/UserRoutes.py b/SE_API/UserRoutes.py index 56f05e4..a448ae6 100644 --- a/SE_API/UserRoutes.py +++ b/SE_API/UserRoutes.py @@ -269,9 +269,6 @@ def getUserByToken(token): mimetype="application/json") # Real response! - - - return no_content("No User Found") diff --git a/models/Course.py b/models/Course.py index e2a6b7c..a96503a 100644 --- a/models/Course.py +++ b/models/Course.py @@ -10,6 +10,7 @@ class Course(db.Model): # projects = db.StringListProperty(required=True,default=[]) startDate = db.DateProperty(required=True) endDate = db.DateProperty(required=True) + membersId = db.StringListProperty(required=True) def to_JSON(self): data = {'courseName' : self.courseName, @@ -26,6 +27,7 @@ class Course(db.Model): 'month': self.endDate.month, 'day': self.endDate.day, }, + 'membersId' : self.membersId, 'id' : self.key().id() } return json.dumps(data) diff --git a/models/Message.py b/models/Message.py index a5dc29f..23019e9 100644 --- a/models/Message.py +++ b/models/Message.py @@ -4,13 +4,15 @@ __author__ = 'Aran' from google.appengine.ext import db class Message(db.Model): - courseName = db.StringProperty(required=True) + groupId = db.IntegerProperty(required=True) message = db.StringProperty(required=True) msgDate = db.DateTimeProperty(required=True) + master_id = db.IntegerProperty(required=True) + isProject = db.BooleanProperty(default=False) def to_JSON(self): data = { - 'courseName' : self.courseName, + 'groupId' : self.groupId, 'message' : self.message, 'date' : { 'year': self.msgDate.year, @@ -19,6 +21,8 @@ class Message(db.Model): 'hour': self.msgDate.hour, 'minute': self.msgDate.minute }, - 'id' : self.key().id() + 'id' : self.key().id(), + 'master_id' : self.master_id, + 'isProject' : self.isProject } return json.dumps(data) diff --git a/models/User.py b/models/User.py index 3aaa1cf..d3bb545 100644 --- a/models/User.py +++ b/models/User.py @@ -15,6 +15,7 @@ class User(db.Model): campusName = db.StringProperty(required=True, default=" ") campuses_id_list = db.StringListProperty(default=[]) courses_id_list = db.StringListProperty(default=[]) + projects_id_list = db.StringListProperty(default=[]) def to_JSON(self): data = {'username' : self.username, @@ -25,6 +26,7 @@ class User(db.Model): 'isFirstLogin' : self.isFirstLogin, 'campusName': self.campusName, 'campuses_id_list': self.campuses_id_list, - 'courses_id_list': self.courses_id_list + 'courses_id_list': self.courses_id_list, + 'projects_id_list': self.projects_id_list } return json.dumps(data)