def post(self) -> Response: """ POST response method for endorsing a course. :return: JSON object """ data = request.get_json() user_id=get_jwt_identity() course_id = data["courseId"] authorized: bool = Users.objects.get(id=user_id).roles.organization if authorized: # don't let an organization endorse the same course twice if Courses.objects(id=course_id, endorsedBy=ObjectId(user_id)): return conflict("You already endorsed this course!") # add the organization the course's endorsedBy list try: endorse = Courses.objects(id=course_id).update(push__endorsedBy=ObjectId(user_id)) if endorse == 0: return not_found("404 Error: The requested course does not exist") except InvalidId as e: print(e.__class__.__name__) print(dir(e)) return bad_request(str(e)) except ValidationError as e: return bad_request(e.message) output = {'id': user_id} return jsonify(output) else: return forbidden()
def post(self) -> Response: """ POST response method for enrolling in a course. :return: JSON object """ data = request.get_json() user_id=get_jwt_identity() if Courses.objects(id=data["courseId"], students=ObjectId(user_id)): return conflict("You cannot enrol in the same course twice!") try: enroll = Courses.objects(id=data["courseId"]).update(push__students=ObjectId(user_id)) if enroll == 0: return not_found("404 Error: The requested course does not exist") except InvalidId as e: print(e.__class__.__name__) print(dir(e)) return bad_request(str(e)) except ValidationError as e: return bad_request(e.message) output = {'id': user_id} return jsonify(output)
def d_c(*args, **kwargs): try: res = function(*args, **kwargs) return res except Exception as e: print(e) return bad_request()
def delete(self, course_id: str) -> Response: """ DELETE response method for disenrolling in a course. :return: JSON object """ user_id=get_jwt_identity() try: disenroll = Courses.objects(id=course_id).update(pull__students=ObjectId(user_id)) if disenroll == 0: return not_found("404 Error: The requested course does not exist") except InvalidId as e: print(e.__class__.__name__) print(dir(e)) return bad_request(str(e)) except ValidationError as e: return bad_request(e.message) output = {'id': user_id} return jsonify(output)
def put(self, course_id: str) -> Response: """ PUT response method for updating a course. JSON Web Token is required. Authorization is required: Access(admin=true) """ data = request.get_json() try: res = Courses.objects.get(id=course_id).update(**data) except ValidationError as e: return bad_request(e.message) return jsonify(res)
def post(self) -> Response: """ POST response method for creating a quiz submission. JSON Web Token is required. Student must be in the course. Student may only have one submission per quiz. """ user = get_jwt_identity() data = request.get_json() quiz_id = data['quiz'] # already a submission from this user if len(Submissions.objects(quiz=quiz_id, user=user)) != 0: return forbidden() # quiz doesn't exist try: quiz = Quizzes.objects.get(id=quiz_id) course = Courses.objects.get(id=quiz.course.id) except DoesNotExist as e: return not_found() # student must be enrolled in the course of the quiz courses = Courses.objects(students=user) if course not in courses: return forbidden() # student is in course and has not submitted # generate the answer key answer_key = {} for question in quiz.quizQuestions: answer_key[question.index] = question.answer # compare answer key to student responses student_responses = data['answers'] grade = 0 for response in student_responses: index = response['question'] if index in answer_key and answer_key[index] == response['answer']: grade += 1 try: data['user'] = user data['grade'] = grade submission = Submissions(**data).save() except ValidationError as e: return bad_request(e.to_dict()) output = {'id': str(submission.id)} return jsonify(output)
def post() -> Response: """ POST response method for creating user. :return: JSON object """ data = request.get_json() try: user = create_user(data) output = {'id': str(user.id)} return jsonify(output) except NotUniqueError as e: # the only unique field for a User is the email address # so if this error occurs, we know the given email is in use return conflict("Given email is already in use!") except ValidationError as e: return bad_request(e.to_dict())
def get(self, course_id: str) -> Response: """ GET response method for single documents in course collection. :return: JSON object """ # check if the given course id is valid or not try: course = Courses.objects.get(id=course_id) except DoesNotExist as e: return not_found() except ValidationError as e: return bad_request() user = get_jwt_identity() try: # check if the user is enrolled in the course queryCourse = Courses.objects.get(id=course_id, students=ObjectId(user)) student = True except DoesNotExist as e: student = False # check if the user is the instructor of the course or if they are an admin inst = str(course.instructor.id) == user admin = Users.objects.get(id=user).roles.admin authorized = student or inst or admin if authorized: if inst or admin: query = Quizzes.objects(course=course) elif student: query = Quizzes.objects(course=course, published=True) fields = { 'id', 'name', 'quizQuestions', 'published', } embedded = {'course': {'id': 'course'}} converted = convert_embedded_query(query, fields, embedded) return jsonify(converted) else: return forbidden()
def delete(self, op_id: str) -> Response: """ DELETE response method for deleting single opportunity. JSON Web Token is required. """ authorized: bool = Users.objects.get(id=get_jwt_identity()).roles.organization or \ Users.objects.get(id=get_jwt_identity()).roles.admin if authorized: try: output = Opportunity.objects.get(id=op_id).delete() except ValidationError as e: return bad_request(e.message) return jsonify(output) else: return forbidden()
def put(self, op_id: str) -> Response: """ PUT response method for updating an opportunity. JSON Web Token is required. """ data = request.get_json() authorized: bool = Users.objects.get(id=get_jwt_identity()).roles.organization or \ Users.objects.get(id=get_jwt_identity()).roles.admin if authorized: try: res = Opportunity.objects.get(id=op_id).update(**data) except ValidationError as e: return bad_request(e.message) return jsonify(res) else: return forbidden()
def post(self) -> Response: """ POST response method for creating a course. JSON Web Token is required. Authorization is required: Access(admin=true) """ authorized: bool = True #Users.objects.get(id=get_jwt_identity()).access.admin if authorized: data = request.get_json() # get the instructor id based off of jwt token identity data['instructor'] = get_jwt_identity() try: course = Courses(**data).save() except ValidationError as e: return bad_request(e.to_dict()) output = {'id': str(course.id)} return jsonify(output) else: return forbidden()
def put(self, page_name) -> Response: # given a page_name that represents some page on the frontend that we've decided to track # either register the page or update the view count if it's already been registered # for now we plan to only track pages of the format ~courses~<course_id>, if the page # is not in this format, 400 error is returned def valid_page_name(page_name): # given a page_name, determine whether the page should be tracked or not prefix = '~courses~' valid = False if page_name.startswith('~courses~'): c_id = page_name[len(prefix):] # check the given id is for a published course try: course = Courses.objects.get(id=c_id) # make sure the course is published valid = course.published # catch the exceptions for either the course not existing, or the given id has an invalid format except (DoesNotExist, ValidationError) as e: valid = False return valid # get the page with the given name (might not be registered yet) try: page = Pages.objects.get(name=page_name) page.views += 1 page.save() except DoesNotExist: # check that the page is of the correct format if valid_page_name(page_name): page = {'name': page_name, 'views': 1} page = Pages(**page).save() else: return bad_request( "the page name given does not correspond with a course page on uimpactify.com" ) output = {'views': page.views} return jsonify(output)
def post(self) -> Response: """ POST response method for creating an opportunity. JSON Web Token is required. """ data = request.get_json() authorized: bool = Users.objects.get(id=get_jwt_identity()).roles.organization or \ Users.objects.get(id=get_jwt_identity()).roles.admin if authorized: data['organization'] = get_jwt_identity() try: opportunity = Opportunity(**data).save() except ValidationError as e: return bad_request(e.to_dict()) output = {'id': str(opportunity.id)} return jsonify(output) else: return forbidden()
def post(self) -> Response: authorized: bool = True #Users.objects.get(id=get_jwt_identity()).access.admin if authorized: data = request.get_json() # make sure the course exists try: course = Courses.objects.get(id=data["course"]) except DoesNotExist: return not_found() # get the user id based off of jwt token identity data['user'] = get_jwt_identity() try: feedback = Feedback(**data).save() except ValidationError as e: return bad_request(e.to_dict()) output = {'id': str(feedback.id)} return jsonify(output) else: return forbidden()
def post(self) -> Response: """ POST response method for creating a quiz. JSON Web Token is required. Authorization is required: role must be instructor """ user = get_jwt_identity() data = request.get_json() oid = ObjectId(user) authorized: bool = Courses.objects.get( id=data['course'])['instructor']['id'] == oid authorized = authorized or Users.objects.get(id=user).roles.admin if authorized: try: quiz = Quizzes(**data).save() except ValidationError as e: return bad_request(e.to_dict()) output = {'id': str(quiz.id)} return jsonify(output) else: return forbidden()
def put(self, quiz_id: str) -> Response: """ PUT response method for updating a quiz. JSON Web Token is required. """ query = Quizzes.objects.get(id=quiz_id) user = get_jwt_identity() # only the course instructor can delete their quizzes authorized: bool = query.course.instructor.id == ObjectId(user) # or an admin authorized = authorized or Users.objects.get(id=user).roles.admin if not authorized: return forbidden() data = request.get_json() if ('course' in data): data['course'] = ObjectId(data['course']) try: res = query.update(**data) except ValidationError as e: return bad_request(e.message) return jsonify(res)