def getUserCoursesByCampus(token, campusId): """ <span class="card-title">This Call will return an array of all Courses of a certain User in a specific Campus</span> <br> <b>Route Parameters</b><br> - seToken: 'seToken'<br> - campusId: 1234354543<br> <br> <br> <b>Payload</b><br> - NONE <br> <br> <br> <b>Response</b> <br> 200 - JSON Array, Example:<br> [<br> { 'title': 'JCE',<br> 'email_ending': '@post.jce.ac.il',<br> 'master_user_id': 123453433341, (User that created the campus)<br> 'avatar_url': 'http://some.domain.com/imagefile.jpg',<br> 'id' : 1234567890<br> },<br> ....<br> {<br> ...<br> }req<br> ]<br> <br> 403 - Invalid Token<br> """ user = get_user_by_token(token) if user is None: return bad_request("Bad user Token") try: campus = Campus.get_by_id(int(campusId)) except Exception as e: return bad_request("Bad id format") 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.campusId == campus.key().id(): 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")
def getCampusesByUserID(token, id): """ <span class="card-title">This Call will return an array of all Campuses of a certain User By ID</span> <br> <b>Route Parameters</b><br> - token: 'seToken' of requesting user - The ID of <b>Wanted</b> User Campuses <br> <br> <b>Payload</b><br> - NONE <br> <br> <br> <b>Response</b> <br> 200 - JSON Array, Example:<br> [<br> { 'title': 'JCE',<br> 'email_ending': '@post.jce.ac.il',<br> 'master_user_id': 123453433341, (User that created the campus)<br> 'avatar_url': 'http://some.domain.com/imagefile.jpg',<br> 'id' : 1234567890<br> },<br> ....<br> {<br> ...<br> }req<br> ]<br> <br> 403 - Invalid Token<br> """ user = get_user_by_token(token) if user is None: return forbidden("Invalid Token") try: user = get_user_by_id(int(id)) except Exception as e: return bad_request("Bad id format") if user is None: return no_content("No User") 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")
def joinCampus(token, campusId): """ <span class="card-title">This call will add the user (by token) to a specific campus</span> <br> <b>Route Parameters</b><br> - seToken: 'seToken'<br> - campusId: 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 """ if not is_lecturer(token): return forbidden("Invalid token or not a lecturer!") user = get_user_by_token(token) try: campus = Campus.get_by_id(int(campusId)) except Exception as e: return bad_request("Bad id format") if campus is None: return bad_request("No such course") if str(user.key().id()) in campus.membersId: return bad_request("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")
def deleteCampus(token,campusId): """ <span class="card-title">This Call will delete a specific campus</span> <br> <b>Route Parameters</b><br> - seToken: 'seToken' - campusId: 1234567890 <br> <br> <b>Payload</b><br> - NONE <br> <br> <br> <b>Response</b> <br> 202 - Deleted campus <br> ....<br> {<br> ...<br> }req<br> ]<br> 400 - no such campus <br> 403 - Invalid token or not a lecturer or lecturer is not owner of campus!<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) try: camp = Campus.get_by_id(int(campusId)) except Exception as e: return bad_request("Bad id format") if camp is None: return bad_request("no such campus") if camp.master_user_id == user.key().id(): db.delete(camp) db.save return accepted("campus deleted") return forbidden("lecturer is not owner of campus")