def on_get(self, req, resp): body = req.context.get("body") if not body: raise falcon.HTTPBadRequest(title="Body is empty") user_type = take_user_type(req.context["user"]["email"]) if user_type is None: raise falcon.HTTPBadRequest(title="No user") if user_type not in [user_types[1], user_types[0]]: raise falcon.HTTPBadRequest(title="Unauthorized user") if body.get("exam_id") is None: raise falcon.HTTPBadRequest(title="There is no exam_id key") file_service = connect_rpc(service=services["file_service"]) exam_uuid = file_service.root.split_file(body.get("exam_id")) resp_data = { "zip_file": open(os.path.join(data_path, exam_uuid, "result.zip"), "rb").read() } resp.context["result"] = resp_data
def on_get(self, req, resp): body = req.context.get("body") if not body: raise falcon.HTTPBadRequest(title="Body is empty") ops = ["section", "exam"] needed_args = { "section": ["section_id", ["instructor_id", "section_code", "course_id"]], "exam": ["exam_id", ["type"]] } user_type = take_user_type(req.context["user"]["email"]) if user_type not in ["admin_user", "instructor_user"]: raise falcon.HTTPBadRequest(title="No user") op = req.params.get("change") or False if not op: raise falcon.HTTPBadRequest(title="There is no 'change' params") if op not in ops: raise falcon.HTTPBadRequest(title="Unsupported operation") data = {"op": op} for arg in needed_args[op]: if isinstance(arg, list): count = 0 for nested_arg in arg: if body.get(nested_arg): data[nested_arg] = body.get(nested_arg) count += 1 if count != 1: if count == 0: raise falcon.HTTPBadRequest( title="Insufficient arguments") else: raise falcon.HTTPBadRequest(title="Too many arguments") else: if not body.get(arg): raise falcon.HTTPBadRequest(title="Insufficient arguments") data[arg] = body.get(arg) database_service = connect_rpc(service=services["database_service"]) response = database_service.root.update_handler(data) resp.context["result"] = response
def on_post(self, req, resp): body = req.context.get("body") if not body: raise falcon.HTTPBadRequest(title="Body is empty") if (body.get("email") is None or body.get("password") is None): raise falcon.HTTPBadRequest( title="email or password is missing on body") else: data = { "email": body.get("email"), "password": body.get("password") } conn = connect_rpc() response = conn.root.get_token(data) resp.context["result"] = response
def on_get(self, req, resp): body = req.context.get("body") if not body: raise falcon.HTTPBadRequest(title="Body is empty") admin_ops = ["university", "department", "faculty", "course"] inst_ops = ["section", "exam"] needed_args = { "university": ["name", "s_name"], "department": ["faculty_id", "name"], "faculty": ["university_id", "name"], "course": ["department_id", "name", "course_code"], "section": ["course_id", "instructor_id", "section_code"], "exam": ["section_ids", "type"] } op = req.params.get("new") user_type = take_user_type(req.context["user"]["email"]) if not op: raise falcon.HTTPBadRequest(title="There is no 'change' params") if user_type is None: raise falcon.HTTPBadRequest(title="No user") if op not in admin_ops + inst_ops: raise falcon.HTTPBadRequest(title="Unsupported operation") if op in admin_ops and user_type != "admin_user": raise falcon.HTTPBadRequest(title="Unauthorized user") if op in inst_ops and user_type != "instructor_user" and user_type != "admin_user": raise falcon.HTTPBadRequest(title="Unauthorized user") data = {"op": op} for needed_arg in needed_args[op]: if body.get(needed_arg) is None: raise falcon.HTTPBadRequest(title="Insufficient arguments") data[needed_arg] = body.get(needed_arg) user_service = connect_rpc(service=services["user_service"]) response = user_service.root.create_handler(data) resp.context["result"] = response
def on_get(self, req, resp): body = req.context.get("body") if not body: raise falcon.HTTPBadRequest(title="Body is empty") if body.get("email") is None or body.get( "password") is None or body.get("name") is None: raise falcon.HTTPNotAcceptable( description="email, password or name is missing on body") else: conn = connect_rpc() data = { "email": body.get("email"), "name": body.get("name"), "lastname": body.get("lastname"), "password": body.get("password"), "school_no": body.get("school_no") } response = conn.root.add_new_user(data) resp.context["result"] = response
def on_post(self, req, resp): body = req.context.get("body") if not body: raise falcon.HTTPBadRequest(title="Body is empty") user_type = take_user_type(req.context["user"]["email"]) if user_type is None: raise falcon.HTTPBadRequest(title="No user") if user_type not in [user_types[1], user_types[0]]: raise falcon.HTTPBadRequest(title="Unauthorized user") if body.get("exam_id") is None: raise falcon.HTTPBadRequest(title="There is no exam_id key") if body.get("exam_pdf") is None: raise falcon.HTTPBadRequest(title="There is no exam_pdf key") if len(body.get("exam_pdf")) == 0: raise falcon.HTTPBadRequest(title="exam_pdf is empty") data = {"exam_id": body.get("exam_id")} file_service = connect_rpc(service=services["file_service"]) exam_uuid = file_service.root.exam_file(data) if exam_uuid is None: raise falcon.HTTPBadRequest(title="Task failed") file_dir = os.path.join(data_path, str(exam_uuid)) os.makedirs(file_dir) file_dir = os.path.join(file_dir, "original_file") file = open(file_dir, "wb") file.write(body.get("exam_pdf")) file.close() resp.context["result"] = std_response( True, message="File transferred successfully")
def on_get(self, req, resp): body = req.context.get("body") if not body: raise falcon.HTTPBadRequest(title="Body is empty") assign_as = req.params.get("assign_as") user_type = take_user_type(req.context["user"]["email"]) if user_type is None: raise falcon.HTTPBadRequest( title="No user", description= "There is no user on system to execute assign process on it") if user_type != "admin_user": raise falcon.HTTPUnauthorized(title="Unauthorized user") op = ["instructor", "student", "admin"] if assign_as not in op: raise falcon.HTTPBadRequest(title="Unsupported operation") if body.get("email") is None: raise falcon.HTTPBadRequest(title="Email is needed") if assign_as == "instructor" or assign_as == "student": if body.get("department_id") is None: raise falcon.HTTPBadRequest(title="Department_id is needed") data = { "op": assign_as, "email": body.get("email"), "department_id": body.get("department_id") } user_service = connect_rpc(service=services["user_service"]) response = user_service.root.assign_handler(data) resp.context["result"] = response
def on_get(self, req, resp): conn = connect_rpc() response = conn.root.verify_token(req.get_header("token")) resp.context["result"] = response
def take_user_type(email): auth_service = connections.connect_rpc() user_type = auth_service.root.user_type(email) return user_type