Example #1
0
    def _parse_and_validate_input(self):
        request_params = web.input()

        course_name = get_mandatory_parameter(request_params, "course_name")
        course_year = get_mandatory_parameter(request_params, "course_year")
        course_period = get_mandatory_parameter(request_params, "course_period")

        course_group = request_params.get("course_group", None)
        course_to_copy_id = request_params.get("course_to_copy_id", None)

        if not course_name:
            raise api.APIError(400, _("The course name cannot be empty."))

        if not course_year:
            raise api.APIError(400, _("The year field cannot be empty."))

        if not course_year.isnumeric():
            raise api.APIError(400, _("The year must be a number."))

        if not course_period:
            raise api.APIError(400, _("The period field cannot be empty."))

        data = {"name": course_name, "group": course_group, "year": course_year, "period": course_period}

        all_courses = set(self.course_factory.get_all_courses().keys())
        course_to_copy_default_value = "-1"
        if course_to_copy_id and course_to_copy_id != course_to_copy_default_value:
            if course_to_copy_id not in all_courses:
                raise api.APIError(400, _("The selected course to copy tasks from does not exist."))

            data["course_to_copy"] = course_to_copy_id

        return data
    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 API_GET(self):
        """
        Returns a list of files and directories as a JSON list.
        Each entry of the output is an object (representing a file or directory) with the following
        properties:
        - "level": Integer. Indicates the depth level of this entry.
        - "is_directory": Boolean. Indicates whether the current entry is a directory. If False, it
        is a file.
        - "name": The file or directory name.
        - "complete_name": The full path of the entry.
        """
        parameters = web.input()

        courseid = get_mandatory_parameter(parameters, "course_id")
        taskid = get_mandatory_parameter(parameters, "task_id")

        self.get_course_and_check_rights(courseid)

        file_list = CourseTaskFiles.get_task_filelist(self.task_factory,
                                                      courseid, taskid)
        result = [{
            "level":
            level,
            "is_directory":
            is_directory,
            "name":
            name,
            "complete_name":
            complete_name[1:]
            if complete_name.startswith("/") else complete_name
        } for level, is_directory, name, complete_name in file_list]

        return 200, result
Example #4
0
        def API_POST(self):
            request_params = web.input()

            courseid = get_mandatory_parameter(request_params, "courseid")
            course = self.course_factory.get_course(courseid)
            taskid = get_mandatory_parameter(request_params, "taskid")
            task = self.task_factory.get_task(course, taskid)

            try:
                init_var = {
                    problem.get_id(): problem.input_type()()
                    for problem in task.get_problems() if problem.input_type() in [dict, list]
                }
                userinput = task.adapt_input_for_backend(web.input(**init_var))
                for k in userinput:
                    userinput[k] = userinput[k].replace("\r", "")
                result, grade, problems, tests, custom, archive, stdout, stderr = self.add_unsaved_job(task, userinput)

                data = {
                    "status": ("done" if result[0] == "success" or result[0] == "failed" else "error"),
                    "result": result[0],
                    "text": ParsableText(result[1]).parse(),
                    "stdout": custom.get("custom_stdout", ""),
                    "stderr": custom.get("custom_stderr", "")
                }
                web.header('Content-Type', 'application/json')
                return 200, json.dumps(data)

            except Exception as ex:
                web.header('Content-Type', 'application/json')
                return 200, json.dumps({"status": "error", "text": str(ex)})
Example #5
0
    def API_POST(self):
        """
        Method receiving POST request, receiving the file and course to register students on UNCode and the course.
        """
        file = get_mandatory_parameter(web.input(), "file")
        course_id = get_mandatory_parameter(web.input(), "course")
        email_language = get_mandatory_parameter(web.input(), "language")

        if email_language not in self.app.available_languages:
            return 200, {
                "status": "error",
                "text": _("Language is not available.")
            }

        course = self._get_course_and_check_rights(course_id)
        if course is None:
            return 200, {
                "status":
                "error",
                "text":
                _("The course does not exist or the user does not have permissions."
                  )
            }

        try:
            text = file.decode("utf-8")
        except:
            return 200, {
                "status":
                "error",
                "text":
                _("The file is not coded in UTF-8. Please change the encoding."
                  )
            }

        parsed_file = self._parse_csv_file(text)

        if not self._file_well_formatted(parsed_file):
            return 200, {
                "status":
                "error",
                "text":
                _("""The file is not formatted as expected, check it is comma separated and 
                             emails are actual emails.""")
            }

        session_language = self.user_manager.session_language()
        self.user_manager.set_session_language(email_language)
        registered_on_course, registered_users = self.register_all_students(
            parsed_file, course, email_language)
        self.user_manager.set_session_language(session_language)

        total_to_register = len(parsed_file)
        message = _(
            """The process finished. Registered students on the course: {0!s}. 
            Registered students on UNCode: {1!s} out of {2!s}.""").format(
                registered_on_course, registered_users, total_to_register)

        return 200, {"status": "success", "text": message}
Example #6
0
def check_manual_scoring_data():
    """ check the three data related to manual scoring """
    manual_grade = get_mandatory_parameter(web.input(), "manual_grade")
    comment = get_mandatory_parameter(web.input(), "comment")
    rubric_status = get_mandatory_parameter(web.input(), "rubric")

    check_rubric_format(rubric_status)
    check_manual_grade_format(manual_grade)
    check_comment_format(comment)

    return manual_grade, comment, rubric_status
Example #7
0
    def API_GET(self):
        parameters = web.input()
        limit = int(get_mandatory_parameter(parameters, "limit"))
        page = int(get_mandatory_parameter(parameters, "page"))

        course_ids = set(bank["courseid"]
                         for bank in self.database.problem_banks.find())

        all_courses = self.course_factory.get_all_courses()
        for course_id, course in all_courses.items():
            if self.user_manager.has_admin_rights_on_course(course):
                course_ids.add(course_id)

        course_ids = list(course_ids)

        tasks = list(
            self.database.tasks_cache.aggregate([{
                "$match": {
                    "course_id": {
                        "$in": course_ids
                    }
                }
            }, {
                "$project": {
                    "course_id": 1,
                    "task_id": 1,
                    "task_name": 1,
                    "task_author": 1,
                    "task_context": 1,
                    "tags": 1,
                    "course_name": 1,
                    "_id": 0,
                }
            }, {
                "$sort": {
                    "task_name": 1
                }
            }]))

        left = limit * (page - 1)
        right = left + limit
        total_pages = len(tasks) // limit
        if len(tasks) % limit != 0 or total_pages == 0:
            total_pages += 1

        if right >= len(tasks):
            tasks = tasks[left:]
        else:
            tasks = tasks[left:right]

        response = {'total_pages': total_pages, "tasks": tasks}
        return 200, response
Example #8
0
    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.")
        }
Example #9
0
        def API_GET(self):
            request_params = web.input()

            custom_test_id = get_mandatory_parameter(request_params,
                                                     "custom_test_id")
            custom_test = self._custom_test_manager.get_custom_test(
                custom_test_id)

            if not custom_test:
                web.header("Content-Type", "application/json")
                return 404, json.dumps({
                    "status":
                    "error",
                    "text":
                    _("Custom test job not found. Try running the custom tests again."
                      )
                })
            elif self._custom_test_manager.is_done(custom_test_id):
                data = {
                    "status": custom_test["status"],
                    "result": custom_test["result"],
                    "text": ParsableText(custom_test["text"]).parse(),
                }
                web.header("Content-Type", "application/json")
                self._custom_test_manager.delete_custom_test(custom_test_id)
                return 200, json.dumps(data)
            elif self._custom_test_manager.is_waiting(custom_test_id):
                web.header("Content-Type", "application/json")
                return 200, json.dumps({
                    "status":
                    "waiting",
                    "text":
                    _("Custom tests are still running.")
                })
Example #10
0
    def API_GET(self):
        parameters = web.input()
        limit = int(get_mandatory_parameter(parameters, "limit"))
        page = int(get_mandatory_parameter(parameters, "page"))

        course_ids = set(bank["courseid"]
                         for bank in self.database.problem_banks.find())

        all_courses = self.course_factory.get_all_courses()
        for course_id, course in all_courses.items():
            if self.user_manager.has_admin_rights_on_course(course):
                course_ids.add(course_id)

        tasks = []
        course_ids = list(course_ids)

        search_tasks = self.database.tasks_cache.find(
            {"course_id": {
                "$in": course_ids
            }})
        for task in search_tasks:
            dict_task = {
                "course_id": task["course_id"],
                "task_id": task["task_id"],
                "task_name": task["task_name"],
                "task_author": task["task_author"],
                "task_context": task["task_context"],
                "tags": task["tags"],
                "course_name": task["course_name"]
            }
            tasks.append(dict_task)
        left = limit * (page - 1)
        right = left + limit
        tasks = list(
            sorted(tasks, key=lambda k: (k['course_name'], k['task_name'])))
        total_pages = len(tasks) // limit
        if len(tasks) % limit != 0 or total_pages == 0:
            total_pages += 1

        if right >= len(tasks):
            tasks = tasks[left:]
        else:
            tasks = tasks[left:right]

        response = {'total_pages': total_pages, "tasks": tasks}
        return 200, response
Example #11
0
    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}
Example #12
0
    def API_GET(self):

        parameters = web.input()
        course_id = get_mandatory_parameter(parameters, "course_id")
        task_id = get_mandatory_parameter(parameters, "task_id")
        user = self.user_manager.session_username()

        db_results = self.database.submissions.find(
            {
                "username": user,
                "courseid": course_id,
                "taskid": task_id,
                "status": "done"
            }, {
                "username": True,
                "_id": False,
                "taskid": True,
                "result": True,
                "submitted_on": True
            }).sort([("submitted_on", pymongo.ASCENDING)])

        success = 0
        fails = 0

        for submission in db_results:
            if submission['result'] == "success":
                success += 1
                #print(submission['result'] + " " + submission['taskid'] + " " + submission['username'][0])
            else:
                fails += 1
                #print(submission['result'] + " " + submission['taskid'] + " " + submission['username'][0])

        if success == 0 and fails == 0:
            response = 'nosubmission'
        else:
            if success > 0:
                response = 'true'
            else:
                response = 'false'

        return 200, {
            'programmer': response,
            'success': success,
            'fails': fails
        }
Example #13
0
 def API_POST(self):
     """ post request """
     try:
         content = get_mandatory_parameter(web.input(), "content")
     except APIError as error:
         error.send()
         return
     comment = _transform_rst_content(content)
     return 200, comment
Example #14
0
    def API_POST(self):
        """
        Method receiving POST request, receiving the file and course to register students on UNCode and the course.
        """
        file = get_mandatory_parameter(web.input(), "file")
        course_id = get_mandatory_parameter(web.input(), "course")

        course = self._get_course_and_check_rights(course_id)
        if course is None:
            return 200, {
                "status":
                "error",
                "text":
                "The course does not exist or the user does not have permissions."
            }

        try:
            text = file.decode("utf-8")
        except:
            return 200, {
                "status":
                "error",
                "text":
                "The file is not coded in UTF-8. Please change the encoding."
            }

        parsed_file = self._parse_csv_file(text)

        if not self._file_well_formatted(parsed_file):
            return 200, {
                "status":
                "error",
                "text":
                "The file is not formatted as expected, check it is comma separated"
                " and emails are actual emails."
            }

        registered_on_course, registered_users = self.register_all_students(
            parsed_file, course)

        message = "The process finished successfully. Registered students on the course: {0!s}. Registered students " \
                  "in UNCode: {1!s}.".format(registered_on_course, registered_users)

        return 200, {"status": "success", "text": message}
Example #15
0
    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, ""
Example #16
0
        def API_POST(self):
            request_params = web.input()

            courseid = get_mandatory_parameter(request_params, "courseid")
            course = self.course_factory.get_course(courseid)
            taskid = get_mandatory_parameter(request_params, "taskid")
            task = self.task_factory.get_task(course, taskid)

            try:
                init_var = {
                    problem.get_id(): problem.input_type()()
                    for problem in task.get_problems()
                    if problem.input_type() in [dict, list]
                }
                user_input = task.adapt_input_for_backend(
                    web.input(**init_var))
                task_tests = task._data.get("grader_test_cases", [])
                for problem_id in init_var.keys():
                    if not self._is_valid_input(problem_id, user_input):
                        web.header("Content-Type", "application/json")
                        return 200, json.dumps({
                            "status":
                            "error",
                            "text":
                            _("Please select at least 1 and up to 3 tests.")
                        })
                    user_input[problem_id +
                               "/input"] = self._parse_selected_tests(
                                   user_input, problem_id, task_tests)

                custom_test_id = self._add_custom_test_job(task, user_input)

                web.header("Content-Type", "application/json")
                return 200, json.dumps({
                    "status": "done",
                    "custom_test_id": custom_test_id,
                    "text": _("Custom tests are running.")
                })
            except Exception as exp:
                web.header("Content-Type", "application/json")
                return 200, json.dumps({"status": "error", "text": str(exp)})
Example #17
0
 def API_GET(self):
     """ Get request. returns a list of users matching the search parameter """
     self.check_superadmin_rights()
     user_string = get_mandatory_parameter(web.input(), "user")
     field_search = web.input().get("field", None)
     collection_manager = CollectionsManagerSingleton.get_instance()
     try:
         list_of_users = search_user(user_string, collection_manager,
                                     field_search)
     except pymongo.errors.OperationFailure:
         return 500, {"error": _("Mongo operation fail")}
     return 200, {"users": list_of_users}
Example #18
0
    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
Example #19
0
    def API_POST(self):

        parameters = web.input()
        task_query = get_mandatory_parameter(parameters, "task_query")
        course_ids = set(bank["courseid"]
                         for bank in self.database.problem_banks.find())

        for course_id, course in self.course_factory.get_all_courses().items():
            if self.user_manager.has_admin_rights_on_course(course):
                course_ids.add(course_id)

        tasks = []
        for course_id in course_ids:
            ids_tasks = self.database.tasks_cache.aggregate([{
                "$match": {
                    "$text": {
                        "$search": task_query
                    }
                }
            }, {
                "$match": {
                    "course_id": course_id
                }
            }, {
                "$unwind": {
                    "path": "$tags",
                    "preserveNullAndEmptyArrays": True
                }
            }, {
                "$group": {
                    "_id": "$_id"
                }
            }])

            for id_task in ids_tasks:

                task = self.database.tasks_cache.find_one(
                    {"_id": id_task["_id"]})

                dict_task = {
                    "course_id": task["course_id"],
                    "task_id": task["task_id"],
                    "task_name": task["task_name"],
                    "task_author": task["task_author"],
                    "task_context": task["task_context"],
                    "tags": task["tags"],
                    "course_name": task["course_name"]
                }
                tasks.append(dict_task)

        return 200, sorted(tasks, key=lambda k: (k['course_id'], k['task_id']))
Example #20
0
        def API_GET(self):
            request_params = web.input()
            submission_id = get_mandatory_parameter(request_params,
                                                    "submission")

            # A mongo request for the submission and his input
            # How do i do this?
            things = self.database.submissions.aggregate([{
                '$match': {
                    "_id": ObjectId(submission_id)
                }
            }, {
                "$project": {
                    "_id": 0,
                    "taskid": 1,
                    "courseid": 1
                }
            }])
            return 200, list(things)[0]
Example #21
0
    def API_POST(self):
        parameters = web.input()
        target_id = get_mandatory_parameter(parameters, "target_id")
        bank_id = get_mandatory_parameter(parameters, "bank_id")
        task_id = get_mandatory_parameter(parameters, "task_id")
        target_course = self.get_course_and_check_rights(target_id)
        target_course_tasks_ids = [key for key in target_course.get_tasks()]

        copy_id = str(uuid.uuid4())
        while copy_id in target_course_tasks_ids:
            copy_id = str(uuid.uuid4())

        try:
            bank_course = self.course_factory.get_course(bank_id)
        except (CourseNotFoundException, InvalidNameException,
                CourseUnreadableException):
            raise api.APIError(400, {"error": "Invalid bank"})

        if not self.is_a_bank(
                bank_id) and not self.user_manager.has_admin_rights_on_course(
                    bank_course):
            raise api.APIError(400, {"error": "Invalid bank"})

        try:
            task = bank_course.get_task(task_id)
        except (TaskNotFoundException, InvalidNameException):
            raise api.APIError(400, {"error": "Invalid task"})

        target_fs = self.course_factory.get_course_fs(target_id)

        try:
            target_fs.copy_to(task.get_fs().prefix, copy_id)

            if "tasks_cache" in self.database.collection_names():
                task_to_copy = self.database.tasks_cache.find_one({
                    "course_id":
                    bank_id,
                    "task_id":
                    task_id
                })

                self.database.tasks_cache.insert({
                    "course_id":
                    target_id,
                    "task_id":
                    copy_id,
                    "task_name":
                    task_to_copy["task_name"],
                    "tags":
                    task_to_copy["tags"],
                    "task_context":
                    task_to_copy["task_context"],
                    "task_author":
                    task_to_copy["task_author"],
                    "course_name":
                    target_course.get_name(
                        self.user_manager.session_language())
                })

        except NotFoundException:
            raise api.APIError(400,
                               {"error": "the copy_id made an invalid path"})

        return 200, {"message": "Copied successfully"}
Example #22
0
    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
Example #23
0
    def API_POST(self):
        """
        Method receiving POST request, receiving the file and course to register students on UNCode and the course.
        """
        file = get_mandatory_parameter(web.input(), "file")
        course_id = get_mandatory_parameter(web.input(), "course")
        email_language = get_mandatory_parameter(web.input(), "language")

        if email_language not in self.app.available_languages:
            return 200, {
                "status": "error",
                "text": _("Language is not available.")
            }

        course = self._get_course_and_check_rights(course_id)
        if course is None:
            return 200, {
                "status":
                "error",
                "text":
                _("The course does not exist or the user does not have permissions."
                  )
            }

        try:
            text = file.decode("utf-8")
        except:
            return 200, {
                "status":
                "error",
                "text":
                _("The file is not coded in UTF-8. Please change the encoding."
                  )
            }

        parsed_file = self._parse_csv_file(text)

        if not self._file_well_formatted(parsed_file):
            return 200, {
                "status":
                "error",
                "text":
                _("""The file is not formatted as expected, check it is comma separated and 
                             emails are actual emails.""")
            }

        session_language = self.user_manager.session_language()
        self.user_manager.set_session_language(email_language)
        registered_on_course, registered_users, users_failed = self.register_all_students(
            parsed_file, course, email_language)
        self.user_manager.set_session_language(session_language)

        total_to_register = len(parsed_file)
        message = _(
            """The process finished with {0!s} students registered on the course and {1!s} students registered 
            on UNCode out of {2!s} students listed in the file. The emails for the new students are being sent in the 
            background, wait a while and check again that all the students were registered."""
        ).format(registered_on_course, registered_users, total_to_register)

        if users_failed:
            html_users_failed = "".join(
                map(
                    lambda x: "<br>  - {} - {}".format(x["username"], x[
                        "email"]), users_failed))
            failed_students_message = _(
                """<br><br>The next students were not registered because of an unexpected error, probably the user 
                is already registered with another username, the username is already taken or the email address is already in use:
                {}""").format(html_users_failed)
            message += failed_students_message

        return 200, {"status": "success", "text": message}
 def get_course_id(self):
     parameters = web.input()
     course_id = get_mandatory_parameter(parameters, "course_id")
     return course_id
Example #25
0
    def API_POST(self):
        """
        Method receiving POST request, receiving the file and course to upload a custom rubric for the course.
        """
        file = get_mandatory_parameter(web.input(), "file")
        course_id = get_mandatory_parameter(web.input(), "course")

        course = self._get_course_and_check_rights(course_id)
        if course is None:
            return 200, {
                "status":
                "error",
                "text":
                _("The course does not exist or the user does not have permissions."
                  )
            }

        try:
            text = file.decode("utf-8")
        except:
            return 200, {
                "status":
                "error",
                "text":
                _("The file is not coded in UTF-8. Please change the encoding."
                  )
            }

        try:
            rubric = json.loads(text, object_pairs_hook=OrderedDict)
        except:
            return 200, {
                "status":
                "error",
                "text":
                _("The rubric is not well formatted. The JSON format is not correct, please check the file and upload it again."
                  )
            }

        try:
            parsed_rubric = self._parse_rubric(rubric)
        except api.APIError as e:
            return 200, {"status": "error", "text": e.return_value}
        except Exception:
            return 200, {
                "status":
                "error",
                "text":
                _("Something went wrong while saving the rubric. Please try again."
                  )
            }

        course_fs = self.course_factory.get_course_fs(course_id)
        course_fs.put(CUSTOM_RUBRIC_FILENAME, json.dumps(parsed_rubric))

        self._remove_feedback_previous_submissions(course_id)

        message = _(
            "The rubric was successfully uploaded. The page will reload once you close the modal."
        )

        return 200, {"status": "success", "text": message}