def post(self, args): """ --- summary: Test creation description: Creates new test requestBody: required: true content: application/json: schema: TestsRegistrationSchema responses: 201: description: OK 400: description: Bad request content: application/json: schema: ErrorSchema example: message: [Wrong input data] """ test_name = args['test'].test_name test_time = args['test'].max_time if test_name is None or test_time is None: return fail_response(msg="Wrong input data", code=400) test_id = application.orm.add_test(test_name=test_name, max_time=test_time) for question in args['questions']: application.orm.add_question(question=question.question, question_type=question.question_type, answer=question.answer, manually_grading=question.manually_grading, points=question.points, test_id=int(test_id)) if test_id is None: return fail_response("Test creation failed", code=500) return generic_response(status='Created', msg="Test created", code=201)
def post(self, email, password): """ --- summary: Login description: Get JWT tokens requestBody: required: true content: application/json: schema: LoginSchema example: email: [email protected] password: 123456 responses: 200: description: OK content: application/json: schema: TokensSchema example: access_token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NTQzMDU1MjYsIm5iZiI6MTU1NDMwNTUyNiwianRpIjoiNzk3ZWZhYWQtZTI4ZS00ZTIyLWE2N2EtZTBmYjA4ZmI1NTY5IiwiZXhwIjoxNTU0MzA2NDI2LCJpZGVudGl0eSI6InN1cGVyQGlubm9wb2xpcy5ydSIsImZyZXNoIjpmYWxzZSwidHlwZSI6ImFjY2VzcyJ9.hvsxNM9SjBapbv1WzpGnn0G5T8N0amAANM9i2woP0kk refresh_token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NTQzMDU1MjYsIm5iZiI6MTU1NDMwNTUyNiwianRpIjoiNjA1Mzc3OTktY2NhMy00MjM3LTkwMmUtZmE3NTNjODM5M2RkIiwiZXhwIjoxNTU2ODk3NTI2LCJpZGVudGl0eSI6InN1cGVyQGlubm9wb2xpcy5ydSIsInR5cGUiOiJyZWZyZXNoIn0.MJQ5eOZQHz8_fq698uJ0Je8PhwX_ZE25dJtS_HNec3E 406: description: Invalid credentials content: application/json: schema: ErrorSchema example: message: [Bad email or password] """ ORM = application.orm user_auth = ORM.get_user_auth_by_email(email=email) if not user_auth: return fail_response('Bad email or password', code=406) try: if not argon2.verify(password, user_auth.password): return fail_response('Bad email or password', 406) except ValueError as excpt: print(f"Failed to verify hash: {excpt}") return fail_response('Bad password hash', 501) try: access_token = create_access_token(identity=email) refresh_token = create_refresh_token(identity=email) ORM.add_token(token=access_token, token_type=TokenType.ACCESS, user_id=user_auth.id) ORM.add_token(token=refresh_token, token_type=TokenType.REFRESH, user_id=user_auth.id) return TokensSchema().dump({ 'access_token': access_token, 'refresh_token': refresh_token, }) except Exception as expt: print(f"Auth error: {expt}") return fail_response(code=500)
def put(self, args, test_id): """ --- summary: Change test by id description: Changes test parameters: - in: path required: true name: test_id schema: type: int requestBody: required: true content: application/json: schema: TestsSchema responses: 201: description: OK 404: description: Not found content: application/json: schema: ErrorSchema example: message: [Test not found] 400: description: Bad request content: application/json: schema: ErrorSchema example: message: [Wrong input data] """ test_name = args['test'].test_name test_time = args['test'].max_time req_test_id = args['test'].id if test_name is None or test_time is None or req_test_id is None: return fail_response(msg="Wrong input data", code=400) test_id = application.orm.update_test(test_id=args['test'].id, test_name=args['test'].test_name, max_time=args['test'].max_time) if test_id is None: return fail_response("Test is not found", code=404) for question in args['questions']: if question.id is None: application.orm.add_question(question=question.question, question_type=question.question_type, answer=question.answer, manually_grading=question.manually_grading, points=question.points, test_id=int(test_id)) else: application.orm.update_question(question=question.question, question_type=question.question_type, answer=question.answer, manually_grading=question.manually_grading, points=question.points, test_id=int(test_id), question_id=question.id) return generic_response(status='Success', msg="Test changed", code=201)
def get(self): """ --- summary: Get all non-archived tests list description: All tests with links on them responses: 200: description: OK content: application/json: schema: type: array items: TestsSchema example: [ { "archived": false, "id": 4, "max_time": null, "questions_tests": [], "test_name": "4" }, { "archived": false, "id": 5, "max_time": null, "questions_tests": [], "test_name": "4" } ] 406: description: Forbidden content: application/json: schema: ErrorSchema example: message: [You are not allowed to create test] 404: description: Tests not found content: application/json: schema: ErrorSchema """ user: Users = get_current_user() if user.role == UsersRole.MANAGER or user.role == UsersRole.STAFF: return fail_response(msg="You are not allowed to create test", code=406) tests = application.orm.get_tests() if tests is None: return fail_response("Some problems with tests retrieving", code=404) test_schema = TestsSchema(many=True) res = test_schema.dump(tests) return jsonify(res.data)
def post(self, args): """ --- summary: Registration description: Creates new user profile requestBody: required: true content: application/json: schema: RegistrationSchema example: $ref: '#/components/examples/Registration' responses: 201: description: OK 406: description: Invalid data provided content: application/json: schema: ErrorSchema example: message: [Invalid email] 409: description: User already exists content: application/json: schema: ErrorSchema example: message: [A user with that email already exists.] """ ORM = application.orm # if not validate_email(args["email"], check_mx=True, verify=True): if not validate_email(args["email"]): return fail_response('Invalid email', code=406) existing_user = ORM.get_user_auth_by_email(email=args["email"]) if existing_user: return fail_response('User with such email already exists', code=409) user = ORM.add_user(first_name=args["name"], last_name=args["surname"]) ORM.add_candidates_authorization(u_id=user.id, email=args["email"], password=argon2.hash( args["password"])) ORM.add_candidates_documents(u_id=user.id) ORM.add_candidates_info(candidate_id=user.id) return generic_response(201)
def get(self, page=1): """ --- summary: Users list description: List of all users with pagination, filtering and sorting responses: 200: description: OK content: application/json: schema: ProfileInfoSchema many: true example: - $ref: '#/components/examples/ProfileFull' - $ref: '#/components/examples/ProfileFull' """ current_user = get_jwt_identity() users = application.orm.get_users(page_num=page, num_of_users=USERS_PER_PAGE) if users is None: return fail_response("Some problems with users retrieving", code=404) user_schema = UsersSchema(many=True) res = user_schema.dump(users) return jsonify(res.data)
def get(self, test_id): """ --- summary: Test submissions description: List of all submissions for given test parameters: - in: path required: true name: test_id schema: type: int responses: 200: description: OK content: application/json: schema: type: array items: TestsSubmissionsSchema 404: description: Not found content: application/json: schema: ErrorSchema example: message: [Submission not found] """ submissions = application.orm.get_submissions(test_id) if submissions is None: return fail_response("Submission not found", code=404) return TestsSubmissionsSchema(many=True).dump(submissions)
def post(self): """ --- summary: Logout description: Revokes JWT token responses: 202: description: OK """ jti = get_raw_jwt()['jti'] if not application.orm.revoke_token_by_jti(jti): return fail_response('Something went wrong', code=500) return success_response(_data={'jti': jti}, code=202)
def get_profile(u_id): user = application.orm.get_user(u_id) if user is None: return fail_response(msg="user not found", code=404) documents = application.orm.get_document(u_id) info = application.orm.get_info(u_id) status = application.orm.get_status(u_id) user_schema = UsersSchema() docs_schema = CandidatesDocumentsSchema() info_schema = CandidatesInfoSchema() status_schema = CandidatesStatusSchema() profile = dict() profile.update({'user': user_schema.dump(user).data}) profile.update({'document': docs_schema.dump(documents).data}) profile.update({'status': info_schema.dump(status).data}) profile.update({'info': status_schema.dump(info).data}) return jsonify(profile)
def delete(self, test_id): """ --- summary: Delete test by id description: Deletes test responses: 201: description: OK 404: description: Not found content: application/json: schema: ErrorSchema example: message: [Test not found] """ success = application.orm.delete_test(test_id) if success: return generic_response(status='success', code=201, msg="Deleted") return fail_response(msg="Can't delete test", code=406)
def get(self, submission_id): """ --- summary: Submission info description: Candidate test submission information parameters: - in: path required: true name: submission_id schema: type: int responses: 200: description: OK content: application/json: schema: type: array items: TestsSubmissionsSchema 404: description: Not found content: application/json: schema: ErrorSchema example: message: [Submission not found] """ submission = application.orm.get_submission(submission_id) submission_schema = TestsSubmissionsSchema() if submission is None: return fail_response("Submission is not found", code=404) res = submission_schema.dump(submission) answers = application.orm.get_answers(res.data['id']) json_answers = [] answers_schema = CandidatesAnswersSchema() for answer in answers: json_answers.append(answers_schema.dump(answer).data) res.data.update({'answers': json_answers}) return jsonify(res.data)
def post(self, test_id): """ --- summary: Test start description: Starts picked test for user and creates empty submission checkpoint parameters: - in: path required: true name: test_id schema: type: int responses: 201: description: OK """ user: Users = get_current_user() submission = application.orm.init_submission(candidate_id=user.id, test_id=test_id) if submission is None: return fail_response("Submission was not created", code=406) return success_response(msg=submission.id, code=201)
def post(self, submission_id): """ --- summary: Test submission description: Marks test as completed. After this action no checkpoints are allowed, so you have to firstly save answers and then complete test parameters: - in: path required: true name: submission_id schema: type: int requestBody: required: true content: application/json: schema: TestsSubmissionsSchema responses: 201: description: OK 404: description: Not found content: application/json: schema: ErrorSchema example: message: [Submission not found] """ found_submission_id = application.orm.get_submission(submission_id) if found_submission_id is None: return fail_response(msg="Submission not found", code=404) application.orm.finish_submission(submission_id=submission_id) return generic_response(status='Created', msg="Submission completed", code=201)
def get(self, test_id): """ --- summary: Get test by id description: All test questions with test metainfo parameters: - in: path name: user_id schema: type: integer required: true description: Numeric ID of the user to get responses: 200: description: OK content: application/json: schema: TestsSchema example: { "archived": false, "id": 8, "max_time": null, "questions": [ { "answer": "1", "id": 5, "manually_grading": true, "points": 0, "question": "2", "question_type": 0, "test": 5 }, { "answer": "1", "id": 9, "manually_grading": true, "points": 0, "question": "2", "question_type": 0, "test": 9 } ], "questions_tests": [ 5, 9 ], "test_name": "45" } 404: description: Not found content: application/json: schema: ErrorSchema example: message: [Test not found] """ test = application.orm.get_test(test_id) test_schema = TestsSchema() if test is None: return fail_response("Test is not found", code=404) res = test_schema.dump(test) questions = [] questions_schema = QuestionsSchema() for question_id in res.data['questions_tests']: obj = application.orm.get_question(question_id) questions.append(questions_schema.dump(obj).data) res.data.update({'questions': questions}) return jsonify(res.data)