def getCoursesByUser(token, userId): """ <span class="card-title">This Call will return an array of all Courses of a certain User</span> <br> <b>Route Parameters</b><br> - seToken: 'seToken'<br> - userId: 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: otherUser = User.get_by_id(int(userId)) except Exception as e: return bad_request("Bad id format") otherUser = User.get_by_id(int(userId)) if otherUser is None: return bad_request("Bad user Id") arr = [] for i in otherUser.courses_id_list: print i course = Course.get_by_id(int(i)) 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 deleteCourse(token, courseId): """ <span class="card-title">This Call will delete a specific Course</span> <br> <b>Route Parameters</b><br> - seToken: 'seToken'<br> - courseid: 'courseid' <br> <br> <b>Payload</b><br> - NONE <br> <br> <br> <b>Response</b> <br> 202 - Deleted Course <br> ....<br> {<br> ...<br> }req<br> ]<br> 400 - no such Course <br> 403 - Invalid token or not a lecturer or lecturer is not owner of Course!<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: c = Course.get_by_id(int(courseId)) except Exception as e: return bad_request("Bad id format") if c is None: return bad_request("no such course") if c.master_id == user.key().id(): db.delete(c) db.save return accepted("course deleted") return forbidden("lecturer is not owner of course")
def joinCourse(token, courseId): """ <span class="card-title">This call will add the user (by token) to a specific course</span> <br> <b>Route Parameters</b><br> - seToken: 'seToken'<br> - courseId: 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 """ user = get_user_by_token(token) if user is None: return bad_request("Wrong user Token") try: course = Course.get_by_id(int(courseId)) except Exception as e: return bad_request("Bad id format") if course is None: return bad_request("No such course") if str(user.key().id()) in course.membersId: return bad_request("User is already member in Course") 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")
def getCoursesByID(token, courseId): """ <span class="card-title">This Call will return A Course By ID</span> <br> <b>Route Parameters</b><br> - seToken: 'seToken'<br> - courseId: 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> 403 - Invalid Token<br> """ user = get_user_by_token(token) if user is None: return bad_request("Bad user Token") try: course = Course.get_by_id(int(courseId)) except Exception as e: return bad_request("Bad id format") if course is None: return bad_request("Bad Course Id") return Response(response=course.to_JSON(), status=200, mimetype="application/json")
def to_JSON(self): user = User.get_by_id(self.master_id) if self.isProject: group = Project.get_by_id(self.groupId) else: group = Course.get_by_id(self.groupId) data = { 'groupId' : self.groupId, 'message' : self.message, 'date' : { 'year': self.msgDate.year, 'month': self.msgDate.month, 'day': self.msgDate.day, 'hour': self.msgDate.hour, 'minute': self.msgDate.minute }, 'id' : self.key().id(), 'master_id' : self.master_id, 'isProject' : self.isProject, 'user': json.loads(user.to_JSON()), 'group': json.loads(group.to_JSON()) } return json.dumps(data)
def removeUserFromCourse(token, userId, courseId): """ <span class="card-title">>This Call will remove a user from a course</span> <br> <b>Route Parameters</b><br> - seToken: 'seToken'<br> - userId: 0987654321,<br> - 'courseId': 1234567890<br> <br> <br> <b>Payload</b><br> - NONE {<br> }<br> <br> <b>Response</b> <br> 200 - User updated <br> 400 - Bad Request """ requestingUser = get_user_by_token(token) if requestingUser is None: return bad_request("Bad User Token") try: userToRemove = User.get_by_id(int(userId)) except Exception as e: return bad_request("Bad id format") if userToRemove is None: return bad_request("No such user to remove") #check Course Exists course = Course.get_by_id(int(courseId)) if course is None: return bad_request("No such Course!") #check if user is owner of Course if requestingUser.key().id() != course.master_id: # check if user want to delete itself if requestingUser.key().id() != userToRemove.key().id(): return forbidden("No permission to delete user") try: userToRemove.courses_id_list.remove(courseId) course.membersId.remove(userToRemove.key().id()) except Exception as e: return bad_request("user is not listed to this course") #remove user from all projects in course projects = Project.all().filter("courseId =", course.key().id()) for p in projects: if p.key().id() in userToRemove.projects_id_list: userToRemove.projects_id_list.remove(p.key().id()) p.membersId.remove(userToRemove.key().id()) db.put(p) db.put(userToRemove) db.put(course) db.save return Response(response=userToRemove.to_JSON(), status=200, mimetype="application/json") # Real response!
def getUsersByCourse(token, courseId): """ <span class="card-title">>This Call will return all users in Course</span> <br> <b>Route Parameters</b><br> - seToken: 'seToken'<br> - courseId: 123456789 <br> <br> <b>Payload</b><br> - NONE <br> <br> <b>Response</b> <br> 200 - JSON Example:<br> <code> {<br> 'username': '******',<br> 'name': 'Darth Vader',<br> 'email': '[email protected],<br> 'isLecturer': 'True',<br> 'seToken': 'xxxxxx-xxxxx-xxxxx-xxxxxx',<br> 'avatar_url': 'http://location.git.com/somthing'<br> 'isFirstLogin': False,<br> 'campuses_id_list': [1243567,7583584904],<br> 'courses_id_list': [543543,54353453,543543534]<br> } </code> <br> 403 - No User Found """ if token is None: return no_content("Token Is Empty, No User Found") user = get_user_by_token(token) if user is None: return bad_request("Bad User Token") try: course = Course.get_by_id(int(courseId)) except Exception as e: return bad_request("Bad id format") if course is None: return bad_request("No such Course") arr = [] for u in enumerate(course.membersId): u = json.loads(User.get_by_id(int(u)).to_JSON()) arr.append(dict(u)) if len(arr) != 0: return Response(response=json.dumps(arr), status=200, mimetype="application/json") else: return Response(response=[], status=200, mimetype="application/json")