diff --git a/SE_API/CourseRoutes.py b/SE_API/CourseRoutes.py index d50fb90..7075f36 100644 --- a/SE_API/CourseRoutes.py +++ b/SE_API/CourseRoutes.py @@ -99,6 +99,8 @@ def create_course(token): print e 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 @@ -169,6 +171,56 @@ def createMessage(token): +#---------------------------------------------------------- +# 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 #---------------------------------------------------------- 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)