def register_user(): info = request.json email, password, name, role = info['email'], info['password'], info[ 'name'], info['role'] if auth.check_email_exists(email): # Make it a proper response? return jsonify({"id": -1, "status": "Nope, email taken"}), 401 if role not in ('teacher', 'student'): return jsonify({ "id": -1, "status": "Role has to be either teacher or student" }), 401 pwd_hash = generate_password_hash(password) student_id, teacher_id, user_id = models.create_user( email, pwd_hash, name, role) response = { "id": user_id, "student_id": student_id, "teacher_id": teacher_id, "status": "Worked" } if role == 'student': models.run_simple_query( "INSERT INTO classes_students (class_id, student_id) VALUES (1, ?)", (student_id, )) return jsonify(response)
def api_create_experiment(class_id): """ JSON format: {"questions": ["Text1", "Text2", "Text3"], "mins": [0, 1, 1], "maxs": [5, 1, 3], "types": ["sociometric", "sociometric", "scale"]} """ class_owner = models.run_simple_query( "SELECT teacher_id FROM classes WHERE id = ?", (class_id, ))[0][0] allowed_user = int(class_owner) authorized, message = auth.authorize_teacher(allowed_user) if not authorized: return jsonify(message), 401 info = request.json if not ('questions' in info and 'mins' in info and 'maxs' in info and 'type' in info): return "Malformed input", 400 # if info['type'] not in ('sociometric', 'scale'): # return "Wrong type", 400 experiment_id = models.create_questionnaire(info['questions'], info['mins'], info['maxs'], class_id, info['type']) models.push_questionnaire(experiment_id, class_id) return jsonify(info)
def api_class_experiments(class_id): class_owner = models.run_simple_query( "SELECT teacher_id FROM classes WHERE id = ?", (class_id, ))[0][0] allowed_user = int(class_owner) authorized, message = auth.authorize_teacher(allowed_user) if not authorized: return jsonify(message), 401 experiments = models.get_experiments(class_id) experiments_dict = {'experiments': []} for exp in experiments: experiments_dict['experiments'].append({ 'id': exp[0], 'info': models.json.loads(exp[1]), 'replies': models.json.loads(exp[2])['replies'], 'date_created': exp[4], 'finished': exp[5], }) return jsonify(experiments_dict)
def api_save_template(category_id): class_owner = models.run_simple_query( "SELECT teacher_id FROM template_categories WHERE id = ?", (category_id, ))[0][0] allowed_user = int(class_owner) authorized, message = auth.authorize_teacher(allowed_user) if not authorized: return jsonify(message), 401 content = json.dumps(request.json) # content = request.json models.save_template(category_id, content) return content, 201
def logout_user(): auth_token = request.headers.get('Authorization') if auth_token is None: response = { "status": "Failure", "message": "Need an Authorization header with the format: Bearer <token>" } return jsonify(response), 400 try: token = auth_token.split(" ")[1] except IndexError: response = { "status": "Failure", "message": "Malformed auth token. Use the format: Bearer <token>" } return jsonify(response), 400 user_id = auth.decode_auth_token(token) if isinstance(user_id, str): response = {"status": "Failure", "message": "Wrong token"} return jsonify(response), 400 models.run_simple_query( "INSERT INTO blacklist (token, blacklisted_on) VALUES (?, ?)", (token, time.time())) response = { "status": "Success", "message": "Logged out", "user_id": user_id } return jsonify(response)
def api_list_experiments(class_id): class_owner = models.run_simple_query( "SELECT teacher_id FROM classes WHERE id = ?", (class_id, ))[0][0] allowed_user = int(class_owner) authorized, message = auth.authorize_teacher(allowed_user) if not authorized: return jsonify(message), 401 experiments = models.list_experiments(class_id) # print(experiments) experiments_dict = utils.query_to_dict(experiments, "experiments", [(0, 'id'), (1, 'date_created')]) return jsonify(experiments_dict)
def api_students(class_id): class_owner = models.run_simple_query( "SELECT teacher_id FROM classes WHERE id = ?", (class_id, ))[0][0] allowed_user = int(class_owner) authorized, message = auth.authorize_teacher(allowed_user) if not authorized: return jsonify(message), 401 students = models.get_students_in_class(class_id) students_dict = utils.query_to_dict(students, 'students', [(0, 'id'), (1, 'name')]) return jsonify(students_dict)
def api_experiment_replies(experiment_id): class_owner = models.run_simple_query( "SELECT c.teacher_id FROM classes c " "JOIN experiments e on c.id = e.class_id WHERE e.id = ?", (experiment_id, ))[0][0] allowed_user = int(class_owner) authorized, message = auth.authorize_teacher(allowed_user) if not authorized: return jsonify(message), 401 replies = models.get_teacher_experiment_replies(experiment_id) replies_dict = json.loads(replies) return jsonify(replies_dict)
def api_load_templates(category_id): class_owner = models.run_simple_query( "SELECT teacher_id FROM template_categories WHERE id = ?", (category_id, ))[0][0] allowed_user = int(class_owner) authorized, message = auth.authorize_teacher(allowed_user) if not authorized: return jsonify(message), 401 templates = models.load_templates(category_id) res_dict = {"templates": []} for row in templates: part_dict = {"id": row[0], "template": json.loads(row[1])} res_dict["templates"].append(part_dict) return jsonify(res_dict)
def graph_api(experiment_id): # Authentication class_owner = models.run_simple_query( "SELECT c.teacher_id FROM classes c " "JOIN experiments e on c.id = e.class_id WHERE e.id = ?", (experiment_id, ))[0][0] allowed_user = int(class_owner) authorized, message = auth.authorize_teacher(allowed_user) if not authorized: return jsonify(message), 401 # Get the question text questions = models.get_experiment_details(experiment_id) questions = json.loads(questions) # Get the class id and teacher's name teacher_query = """SELECT c.id, t.name FROM experiments e JOIN classes c on e.class_id = c.id JOIN teachers t on c.teacher_id = t.id WHERE e.id = ?""" class_id, teacher_name = models.run_simple_query(teacher_query, (experiment_id, ))[0] # Get the raw replies replies = models.get_experiment_replies(experiment_id) replies = json.loads(replies) # Get the list of students for nodes students = models.get_students_in_class(class_id) students = utils.query_to_dict(students, 'nodes', [(0, 'id'), (1, 'label'), (0, 'group')]) # for student in students: # student['shape'] = 'circularImage' # student['image'] = '' response = { 'teacher': teacher_name, 'questions': [], 'nodes': students['nodes'] } for i, question in enumerate(questions['questions']): question_dict = { 'text': question['text'], 'type': question['type'], 'edges': [] } # print(question_dict) response['questions'].append(question_dict) # Bit of spaghetti, but builds the edges array for reply_dict in replies['replies']: question_reply = reply_dict['response'][i] if isinstance(question_reply, list): for reply in question_reply: question_dict['edges'].append({ 'from': reply_dict['id'], 'to': reply }) return jsonify(response)