def API_GET(self): input_data = web.input() course_id = get_mandatory_parameter(input_data, 'course_id') task_id = get_mandatory_parameter(input_data, 'task_id') notebook_name = get_mandatory_parameter(input_data, 'notebook_name') try: course = self.course_factory.get_course(course_id) except (CourseNotFoundException, InvalidNameException, CourseUnreadableException): raise APIError( 400, { "error": "The course does not exists or the user does not have permissions" }) try: task = course.get_task(task_id) except Exception: raise APIError(400, {"error": "The task does not exist in the course"}) try: with open(os.path.join(task.get_fs().prefix, notebook_name), 'r') as notebook_file: return 200, notebook_file.read() except Exception: return 200, ""
def POST_AUTH(self, course_id, task_id, submission_id): """ POST request """ try: self.update_manual_comment_and_grade(submission_id) except ManualScoringError as error: api_error = APIError(400, {"error": error.get_message()}) api_error.send() return except APIError as error: error.send() return return 200, {"status": "success", "text": "Update successfully"}
def API_POST(self): """ Set a unlocked user's hint in the database as 'unlocked', and set it's own penalty and total penalty """ input_data = web.input() course_id = get_mandatory_parameter(input_data, 'course_id') task_id = get_mandatory_parameter(input_data, 'task_id') username = self.user_manager.session_username() hint_id = get_mandatory_parameter(input_data, 'hint_id') try: course = self.course_factory.get_course(course_id) except (InvalidNameException, CourseNotFoundException): raise APIError( 400, { "error": _("The course does not exist or the user does not have access." ) }) try: task = self.task_factory.get_task(course, task_id) except Exception: raise APIError( 400, {"error": _("The task does not exist in the course.")}) if not self.user_manager.course_is_user_registered(course, username): raise APIError( 400, {"error": _("The user is not registered in this course.")}) task_hints = self.get_task_hints(task) try: self.user_hint_manager.unlock_hint(task, username, hint_id, task_hints) except Exception: return 200, { "status": "error", "message": _("An error occurred while updating status of the hint. The hint does not exist in the database." ) } return 200, { "status": "success", "message": _("Hint unlocked successfully.") }
def API_POST(self, courseid, taskid): # pylint: disable=arguments-differ """ Creates a new submissions. Takes as (POST) input the key of the subproblems, with the value assigned each time. Returns - an error 400 Bad Request if all the input is not (correctly) given, - an error 403 Forbidden if you are not allowed to create a new submission for this task - an error 404 Not found if the course/task id not found - an error 500 Internal server error if the grader is not available, - 200 Ok, with {"submissionid": "the submission id"} as output. """ try: course = self.course_factory.get_course(courseid) except: raise APINotFound("Course not found") username = self.user_manager.session_username() if not self.user_manager.course_is_open_to_user(course, username, False): raise APIForbidden("You are not registered to this course") try: task = course.get_task(taskid) except: raise APINotFound("Task not found") self.user_manager.user_saw_task(username, courseid, taskid) # Verify rights if not self.user_manager.task_can_user_submit(task, username, False): raise APIForbidden("You are not allowed to submit for this task") user_input = flask.request.form.copy() for problem in task.get_problems(): pid = problem.get_id() if problem.input_type() == list: user_input[pid] = flask.request.form.getlist(pid) elif problem.input_type() == dict: user_input[pid] = flask.request.files.get(pid) else: user_input[pid] = flask.request.form.get(pid) user_input = task.adapt_input_for_backend(user_input) if not task.input_is_consistent(user_input, self.default_allowed_file_extensions, self.default_max_file_size): raise APIInvalidArguments() # Get debug info if the current user is an admin debug = self.user_manager.has_admin_rights_on_course(course, username) # Start the submission try: submissionid, _ = self.submission_manager.add_job(task, user_input, debug) return 200, {"submissionid": str(submissionid)} except Exception as ex: raise APIError(500, str(ex))
def API_GET(self): """ For all the hints that the user has unlocked, get the left necessary content by each hint to show them in task view (Content in RST format, applied penalty, and unlocked hint status). """ input_data = web.input() course_id = get_mandatory_parameter(input_data, 'course_id') task_id = get_mandatory_parameter(input_data, 'task_id') username = self.user_manager.session_username() try: course = self.course_factory.get_course(course_id) except (InvalidNameException, CourseNotFoundException): raise APIError( 400, { "error": _("The course does not exist or the user does not have access." ) }) try: task = self.task_factory.get_task(course, task_id) except Exception: raise APIError( 400, {"error": _("The task does not exist in the course.")}) if not self.user_manager.course_is_user_registered(course, username): raise APIError( 400, {"error": _("The user is not registered in this course.")}) hints_data = {} try: hints_data = self.user_hint_manager.get_hint_content_by_status( task, username, self.get_task_hints(task)) except Exception: raise APIError(400, { "message": _("An error occurred while getting the user's hints.") }) return 200, {"status": "success", "data": hints_data}
def API_GET(self): # Validate parameters username = self.user_manager.session_username() input_data = web.input() course_id = get_mandatory_parameter(input_data, "course_id") task_id = get_mandatory_parameter(input_data, "task_id") language = get_mandatory_parameter(input_data, "language") try: course = self.course_factory.get_course(course_id) except (CourseNotFoundException, InvalidNameException, CourseUnreadableException): raise APIError( 400, { "error": "The course does not exists or the user does not have permissions" }) if not self.user_manager.course_is_user_registered(course, username): raise APIError( 400, { "error": "The course does not exists or the user does not have permissions" }) try: task = course.get_task(task_id) except Exception: raise APIError(400, {"error": "The task does not exist in the course"}) try: with open(os.path.join(task.get_fs().prefix, 'task.yaml'), 'r') as task_yaml: try: data = yaml.safe_load(task_yaml) filename = data['code_preview_pairs'][language] with open(os.path.join(task.get_fs().prefix, filename), 'r') as file_preview: return 200, file_preview.read() except yaml.YAMLError as exc: return 200, "" except Exception: return 200, ""
def API_GET(self): """ Get task submission mode before it's saved """ input_data = web.input() course_id = get_mandatory_parameter(input_data, 'course_id') task_id = get_mandatory_parameter(input_data, 'task_id') course = self.get_course_and_check_rights(course_id) use_temporal_file = False task = None try: task = self.task_factory.get_task(course, task_id) task_data = task._data except: use_temporal_file = True if use_temporal_file: try: task = self.get_temporal_task_hints_file_content( course, task_id) task_data = task.get('data', {}) except InvalidNameException: raise APIError(400, {"error": _("Invalid task name")}) if task is None: raise APIError( 400, { "error": _("Task does not exist, or task has not been created yet.") }) task_submission_mode = task_data.get('groups', False) return 200, task_submission_mode
def API_POST(self): """ Change user hints documents of the task if submission mode has been changed """ input_data = web.input() course_id = get_mandatory_parameter(input_data, 'course_id') task_id = get_mandatory_parameter(input_data, 'task_id') last_submission_mode = get_mandatory_parameter(input_data, 'last_submission_mode') course = self.get_course_and_check_rights(course_id) use_temporal_file = False task = None try: task = self.task_factory.get_task(course, task_id) task_data = task._data except: use_temporal_file = True if use_temporal_file: try: task = self.get_temporal_task_hints_file_content( course, task_id) task_data = task.get('data', {}) except InvalidNameException: raise APIError(400, {"error": _("Invalid task name")}) if task is None: raise APIError( 400, { "error": _("Task does not exist, or task has not been created yet.") }) task_submission_mode = task_data.get('groups', False) task_hints = task_data.get('task_hints', {}) self.task_factory.delete_temporal_task_file(course, task_id) try: last_submission_mode = ('true' == last_submission_mode) if task_submission_mode != last_submission_mode: self.user_hint_manager.on_change_task_submission_mode( course_id, task_id, task_submission_mode, task_hints) except Exception: raise APIError( 400, { "error": _("There was a problem to update submission mode on users hints." ) }) return 200, task_submission_mode
def get_submission(self, submission_id): """ get a submission by id """ submission = self.submission_manager.get_submission(submission_id) if submission is None: raise APIError(404, "Submission no found") return submission
def _check_string(content): """ Check if the content is a string """ if not isinstance(content, str): raise APIError(400, "The content isn't a string")