Exemplo n.º 1
0
    def get_temporal_task_file_content(self, course, taskid):
        """
        :param course: a Course object
        :param taskid: the task id of the task
        :raise InvalidNameException, TaskUnreadableException
        :return: the content of the temporary task file
        """

        if not id_checker(taskid):
            raise InvalidNameException("Task with invalid name: " + taskid)

        task_file_manager = None

        task_fs = self.get_task_fs(course.get_id(), taskid)
        for file_extension, file_manager in self._task_file_managers.items():
            if task_fs.get("task_temp." + file_extension):
                task_file_manager = file_manager

        temporal_task_file_content = {}
        try:
            temporal_task_file_content["data"] = task_file_manager.load(
                task_fs.get("task_temp.yaml"))
        except Exception as e:
            raise TaskUnreadableException(str(e))

        return temporal_task_file_content
Exemplo n.º 2
0
    def _update_cache(self, course, taskid):
        """
        Updates the cache
        :param course: a Course object
        :param taskid: a (valid) task id
        :raise InvalidNameException, TaskNotFoundException, TaskUnreadableException
        """
        path_to_descriptor, descriptor_ext, descriptor_reader = self._get_task_descriptor_info(
            course.get_id(), taskid)
        try:
            with codecs.open(path_to_descriptor, 'r', 'utf-8') as fd:
                task_content = descriptor_reader.load(fd.read())
        except Exception as e:
            raise TaskUnreadableException(str(e))

        self._hook_manager.call_hook('modify_task_data',
                                     course=course,
                                     taskid=taskid,
                                     data=task_content)

        task = self._task_class(
            course, taskid, task_content,
            self.get_directory_path(course.get_id(), taskid))
        self._cache[(course.get_id(),
                     taskid)] = (task, os.stat(path_to_descriptor).st_mtime)
Exemplo n.º 3
0
    def _get_last_updates(self, course, taskid, task_fs, need_content=False):
        descriptor_name, descriptor_reader = self._get_task_descriptor_info(course.get_id(), taskid)
        last_update = {descriptor_name: task_fs.get_last_modification_time(descriptor_name)}
        translations_fs = task_fs.from_subfolder("$i18n")

        if not translations_fs.exists():
            translations_fs = task_fs.from_subfolder("student").from_subfolder("$i18n")
        if not translations_fs.exists():
            translations_fs = course.get_fs().from_subfolder("$common").from_subfolder("$i18n")
        if not translations_fs.exists():
            translations_fs = course.get_fs().from_subfolder("$common").from_subfolder("student").from_subfolder(
                "$i18n")

        if translations_fs.exists():
            for f in translations_fs.list(folders=False, files=True, recursive=False):
                lang = f[0:len(f) - 3]
                if translations_fs.exists(lang + ".mo"):
                    last_update["$i18n/" + lang + ".mo"] = translations_fs.get_last_modification_time(lang + ".mo")

        if need_content:
            try:
                task_content = descriptor_reader.load(task_fs.get(descriptor_name))
            except Exception as e:
                raise TaskUnreadableException(str(e))
            return last_update, translations_fs, task_content
        else:
            return last_update, translations_fs, None
Exemplo n.º 4
0
    def _update_cache(self, course, taskid):
        """
        Updates the cache
        :param course: a Course object
        :param taskid: a (valid) task id
        :raise InvalidNameException, TaskNotFoundException, TaskUnreadableException
        """
        if not id_checker(taskid):
            raise InvalidNameException("Task with invalid name: " + taskid)

        task_fs = self.get_task_fs(course.get_id(), taskid)
        descriptor_name, descriptor_reader = self._get_task_descriptor_info(course.get_id(), taskid)
        try:
            task_content = descriptor_reader.load(task_fs.get(descriptor_name))
        except Exception as e:
            raise TaskUnreadableException(str(e))

        last_modif = {descriptor_name: task_fs.get_last_modification_time(descriptor_name)}
        translations_fs = task_fs.from_subfolder("$i18n")
        if translations_fs.exists():
            for f in translations_fs.list(folders=False, files=True, recursive=False):
                lang = f[0:len(f) - 3]
                if translations_fs.exists(lang + ".mo"):
                    last_modif["$i18n/" + lang + ".mo"] = translations_fs.get_last_modification_time(lang + ".mo")

        self._cache[(course.get_id(), taskid)] = (
            self._task_class(course, taskid, task_content, task_fs, self._hook_manager, self._task_problem_types),
            last_modif
        )
Exemplo n.º 5
0
    def _get_last_updates(self, course, taskid, task_fs, need_content=False):
        descriptor_name, descriptor_reader = self._get_task_descriptor_info(course.get_id(), taskid)
        last_update = {descriptor_name: task_fs.get_last_modification_time(descriptor_name)}

        if need_content:
            try:
                task_content = descriptor_reader.load(task_fs.get(descriptor_name))
            except Exception as e:
                raise TaskUnreadableException(str(e))
            return last_update, task_content
        else:
            return last_update, None
Exemplo n.º 6
0
    def get_task_descriptor_content(self, courseid, taskid):
        """
        :param courseid: the course id of the course
        :param taskid: the task id of the task
        :raise InvalidNameException, TaskNotFoundException, TaskUnreadableException
        :return: the content of the task descriptor, as a dict
        """
        if not id_checker(courseid):
            raise InvalidNameException("Course with invalid name: " + courseid)
        if not id_checker(taskid):
            raise InvalidNameException("Task with invalid name: " + taskid)

        descriptor_path, descriptor_manager = self._get_task_descriptor_info(courseid, taskid)
        try:
            task_content = descriptor_manager.load(self.get_task_fs(courseid, taskid).get(descriptor_path))
        except Exception as e:
            raise TaskUnreadableException(str(e))
        return task_content
Exemplo n.º 7
0
 def get_task_descriptor_content(self, courseid, taskid):
     """
     :param courseid: the course id of the course
     :param taskid: the task id of the task
     :raise InvalidNameException, TaskNotFoundException, TaskUnreadableException
     :return: the content of the task descriptor, as a dict
     """
     if not id_checker(courseid):
         raise InvalidNameException("Course with invalid name: " + courseid)
     if not id_checker(taskid):
         raise InvalidNameException("Task with invalid name: " + taskid)
     path_to_descriptor, _, descriptor_manager = self._get_task_descriptor_info(courseid, taskid)
     try:
         with codecs.open(path_to_descriptor, 'r', 'utf-8') as fd:
             task_content = descriptor_manager.load(fd.read())
     except Exception as e:
         raise TaskUnreadableException(str(e))
     return task_content
Exemplo n.º 8
0
    def _update_cache(self, course, taskid):
        """
        Updates the cache
        :param course: a Course object
        :param taskid: a (valid) task id
        :raise InvalidNameException, TaskNotFoundException, TaskUnreadableException
        """
        if not id_checker(taskid):
            raise InvalidNameException("Task with invalid name: " + taskid)

        task_fs = self.get_task_fs(course.get_id(), taskid)
        descriptor_name, descriptor_reader = self._get_task_descriptor_info(
            course.get_id(), taskid)
        try:
            task_content = descriptor_reader.load(task_fs.get(descriptor_name))
        except Exception as e:
            raise TaskUnreadableException(str(e))

        self._cache[(course.get_id(), taskid)] = (
            self._task_class(course, taskid, task_content, task_fs,
                             self._hook_manager),
            task_fs.get_last_modification_time(descriptor_name))
Exemplo n.º 9
0
    def import_elements(self, course, data):
        imported_tasks, renamed_tasks = [], {}
        errors = []

        try:
            with tarfile.open(fileobj=data["archive_file"].file) as tar:
                elements = tar.getnames()

                if "import_inginious_file" in data:
                    # import tasks
                    for taskid in get_tasks_id(elements):
                        try:
                            # get files
                            task_files = get_task_files(taskid, elements)
                            tar_members = [
                                tar.getmember(task_file)
                                for task_file in task_files
                            ]

                            # avoid tasks with same id
                            new_taskid = make_id_unique(
                                taskid, course.get_tasks())
                            if taskid != new_taskid:
                                renamed_tasks[taskid] = new_taskid
                                for tar_member in tar_members:
                                    tar_member.name = new_taskid + tar_member.name.lstrip(
                                        taskid)

                            # extract files
                            tar.extractall(course.get_fs().prefix, tar_members)

                            # check task descriptor is in inginious format and remove tags
                            task_descriptor = self.task_factory.get_task_descriptor_content(
                                course.get_id(), new_taskid)
                            if not check_task_descriptor(task_descriptor):
                                raise TaskUnreadableException(
                                    "Invalid task config format")
                            task_descriptor["categories"] = []
                            self.task_factory.update_task_descriptor_content(
                                course.get_id(), taskid, task_descriptor)

                            imported_tasks.append(new_taskid)
                        except:
                            errors.append(
                                _("Invalid format for task ") + taskid)
                            if new_taskid in course.get_tasks():
                                self.task_factory.delete_task(
                                    course.get_id(), new_taskid)

                    # import sections
                    if "sections.yaml" in elements:
                        try:
                            from inginious.frontend.plugins.course_structure.webapp_course import \
                                get_sections_tasks_ids, replace_sections_tasks_ids, get_toc, update_toc_content

                            sections_content = load(
                                tar.extractfile("sections.yaml").read())
                            replace_sections_tasks_ids(sections_content,
                                                       renamed_tasks)

                            # if all tasks in the sections are imported, add sections to course structure and adapt rank
                            if all(elem in imported_tasks for elem in
                                   get_sections_tasks_ids(sections_content)):
                                toc = get_toc(course)
                                for section in sections_content:
                                    section["rank"] = len(toc)
                                    toc.append(section)

                                update_toc_content(self.course_factory,
                                                   course.get_id(), toc)
                            else:
                                errors.append(
                                    _("Some tasks in the structure are not included in the archive"
                                      ))
                        except:
                            errors.append(
                                _("Sections not supported for this instance"))
        except Exception as e:
            print()
            errors.append(_("Invalid archive format"))

        return self.page(course, errors,
                         None if errors else _("Import successful."))