diff --git a/SE_API/CampusRoutes.py b/SE_API/CampusRoutes.py index 02d460e..2d5e4c0 100644 --- a/SE_API/CampusRoutes.py +++ b/SE_API/CampusRoutes.py @@ -76,9 +76,10 @@ def create_campus(token): print e user = get_user_by_token(token) - + arr = [] + arr.append(str(user.key().id())) try: - campus = Campus(title=payload['title'], email_ending=payload['email_ending'], master_user_id=user.key().id(), avatar_url=payload['avatar_url']) + campus = Campus(title=payload['title'], email_ending=payload['email_ending'], master_user_id=user.key().id(), avatar_url=payload['avatar_url'], membersId=arr) except Exception: return bad_request() @@ -94,6 +95,53 @@ def create_campus(token): # PUT #---------------------------------------------------------- +@campus_routes.route('/api/campuses/joinCampus//', methods=["PUT"]) +@auto.doc() +def joinCampus(token, campusId): + """ + This call will add the user (by token) to a specific campus +
+ Route Parameters
+ - seToken: 'seToken'
+ - campusId: 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") + + campus = Campus.get_by_id(int(campusId)) + if campus is None: + return bad_request("No such course") + + if user.key().id() in campus.membersId: + return no_content("User is already member in Project") + + campus.membersId.append(str(user.key().id())) + user.courses_id_list.append(str(campus.key().id())) + + db.put(campus) + db.put(user) + db.save + + return Response(response=campus.to_JSON(), + status=202, + mimetype="application/json") + + + #---------------------------------------------------------- # GET #---------------------------------------------------------- diff --git a/models/Campus.py b/models/Campus.py index dd910de..343fbda 100644 --- a/models/Campus.py +++ b/models/Campus.py @@ -9,12 +9,15 @@ class Campus(db.Model): avatar_url = db.StringProperty(required=True) email_ending = db.StringProperty(required=True) master_user_id = db.IntegerProperty(required=True) + membersId = db.StringListProperty(required=True, default=[]) + def to_JSON(self): data = {'title': self.title, 'email_ending': self.email_ending, 'master_user_id': self.master_user_id, 'avatar_url': self.avatar_url, + 'membersId': self.membersId, 'id' : self.key().id() } return json.dumps(data)