Exemplo n.º 1
0
def import_course(course, new_courseid, username, course_factory):
    if not id_checker(new_courseid):
        raise ImportCourseException("Course with invalid name: " + new_courseid)
    course_fs = course_factory.get_course_fs(new_courseid)

    if course_fs.exists("course.yaml") or course_fs.exists("course.json"):
        raise ImportCourseException("Course with id " + new_courseid + " already exists.")

    try:
        git.clone(course.get_link(), course_fs.prefix)
    except:
        raise ImportCourseException(_("Couldn't clone course into your instance"))

    try:
        old_descriptor = course_factory.get_course_descriptor_content(new_courseid)
    except:
        old_descriptor ={}

    try:
        new_descriptor = {"description": old_descriptor.get("description", ""),
                          'admins': [username],
                          "accessible": False,
                          "tags": old_descriptor.get("tags", {})}
        if "name" in old_descriptor:
            new_descriptor["name"] = old_descriptor["name"] + " - " + new_courseid
        else:
            new_descriptor["name"] = new_courseid
        if "toc" in old_descriptor:
            new_descriptor["toc"] = old_descriptor["toc"]
        course_factory.update_course_descriptor_content(new_courseid, new_descriptor)
    except:
        course_factory.delete_course(new_courseid)
        raise ImportCourseException(_("An error occur while editing the course description"))

    get_course_logger(new_courseid).info("Course %s cloned from the marketplace.", new_courseid)
Exemplo n.º 2
0
    def create_course(self, courseid, init_content):
        """
        :param courseid: the course id of the course
        :param init_content: initial descriptor content
        :raise InvalidNameException or CourseAlreadyExistsException
        Create a new course folder and set initial descriptor content, folder can already exist
        """
        if not id_checker(courseid):
            raise InvalidNameException("Course with invalid name: " + courseid)

        course_directory = os.path.join(self._tasks_directory, courseid)
        if not os.path.exists(course_directory):
            os.mkdir(course_directory)

        base_file = os.path.join(course_directory, "course")
        if not os.path.isfile(base_file +
                              ".yaml") and not os.path.isfile(base_file +
                                                              ".json"):
            write_yaml(os.path.join(course_directory, "course.yaml"),
                       init_content)
        else:
            raise CourseAlreadyExistsException("Course with id " + courseid +
                                               " already exists.")

        get_course_logger(courseid).info("Course %s created in the factory.",
                                         courseid)
Exemplo n.º 3
0
    def create_course(self, courseid, init_content):
        """
        :param courseid: the course id of the course
        :param init_content: initial descriptor content
        :raise InvalidNameException or CourseAlreadyExistsException
        Create a new course folder and set initial descriptor content, folder can already exist
        """
        if not id_checker(courseid):
            raise InvalidNameException("Course with invalid name: " + courseid)

        course_fs = self.get_course_fs(courseid)
        course_fs.ensure_exists()

        if course_fs.exists("course.yaml") or course_fs.exists("course.json"):
            raise CourseAlreadyExistsException("Course with id " + courseid +
                                               " already exists.")
        else:
            course_fs.put("course.yaml",
                          get_json_or_yaml("course.yaml", init_content))

        get_course_logger(courseid).info("Course %s created in the factory.",
                                         courseid)
        self._hook_manager.call_hook('course_created',
                                     courseid=courseid,
                                     new_content=init_content)
Exemplo n.º 4
0
 def get_all_courses(self):
     """
     :return: a table containing courseid=>Course pairs
     """
     course_ids = [f[0:len(f)-1] for f in self._filesystem.list(folders=True, files=False, recursive=False)]  # remove trailing "/"
     output = {}
     for courseid in course_ids:
         try:
             output[courseid] = self.get_course(courseid)
         except Exception:
             get_course_logger(courseid).warning("Cannot open course", exc_info=True)
     return output
Exemplo n.º 5
0
 def get_all_courses(self):
     """
     :return: a table containing courseid=>Course pairs
     """
     course_ids = [os.path.splitext(f)[0] for f in os.listdir(self._tasks_directory) if
                   os.path.isfile(os.path.join(self._tasks_directory, f, "course.yaml")) or
                   os.path.isfile(os.path.join(self._tasks_directory, f, "course.json"))]
     output = {}
     for courseid in course_ids:
         try:
             output[courseid] = self.get_course(courseid)
         except Exception as e:  # todo log the error
             get_course_logger(courseid).warning("Cannot open course", exc_info=True)
     return output
Exemplo n.º 6
0
 def get_all_courses(self):
     """
     :return: a table containing courseid=>Course pairs
     """
     course_ids = [
         f[0:len(f) - 1] for f in self._filesystem.list(
             folders=True, files=False, recursive=False)
     ]  # remove trailing "/"
     output = {}
     for courseid in course_ids:
         try:
             output[courseid] = self.get_course(courseid)
         except Exception:
             get_course_logger(courseid).warning("Cannot open course",
                                                 exc_info=True)
     return output
Exemplo n.º 7
0
    def delete_task(self, courseid, taskid):
        """ Erase the content of the task folder
        :param courseid: the course id of the course
        :param taskid: the task id of the task
        :raise: InvalidNameException or CourseNotFoundException
        """
        if not id_checker(courseid):
            raise InvalidNameException("Course with invalid name: " + courseid)
        if not id_checker(taskid):
            raise InvalidNameException("Task with invalid name: " + taskid)

        task_fs = self.get_task_fs(courseid, taskid)

        if task_fs.exists():
            task_fs.delete()
            get_course_logger(courseid).info("Task %s erased from the factory.", taskid)
Exemplo n.º 8
0
    def delete_course(self, courseid):
        """
        :param courseid: the course id of the course
        :raise InvalidNameException or CourseNotFoundException
        Erase the content of the course folder
        """
        if not id_checker(courseid):
            raise InvalidNameException("Course with invalid name: " + courseid)

        course_directory = os.path.join(self._tasks_directory, courseid)

        if not os.path.exists(course_directory):
            raise CourseNotFoundException()

        shutil.rmtree(course_directory)

        get_course_logger(courseid).info("Course %s erased from the factory.", courseid)
Exemplo n.º 9
0
    def delete_course(self, courseid):
        """
        :param courseid: the course id of the course
        :raise InvalidNameException or CourseNotFoundException
        Erase the content of the course folder
        """
        if not id_checker(courseid):
            raise InvalidNameException("Course with invalid name: " + courseid)

        course_fs = self.get_course_fs(courseid)

        if not course_fs.exists():
            raise CourseNotFoundException()

        course_fs.delete()

        get_course_logger(courseid).info("Course %s erased from the factory.", courseid)
Exemplo n.º 10
0
    def delete_course(self, courseid):
        """
        :param courseid: the course id of the course
        :raise InvalidNameException or CourseNotFoundException
        Erase the content of the course folder
        """
        if not id_checker(courseid):
            raise InvalidNameException("Course with invalid name: " + courseid)

        course_fs = self.get_course_fs(courseid)

        if not course_fs.exists():
            raise CourseNotFoundException()

        course_fs.delete()

        get_course_logger(courseid).info("Course %s erased from the factory.",
                                         courseid)
Exemplo n.º 11
0
    def create_course(self, courseid, init_content):
        """
        :param courseid: the course id of the course
        :param init_content: initial descriptor content
        :raise InvalidNameException or CourseAlreadyExistsException
        Create a new course folder and set initial descriptor content, folder can already exist
        """
        if not id_checker(courseid):
            raise InvalidNameException("Course with invalid name: " + courseid)

        course_fs = self.get_course_fs(courseid)
        course_fs.ensure_exists()

        if course_fs.exists("course.yaml") or course_fs.exists("course.json"):
            raise CourseAlreadyExistsException("Course with id " + courseid + " already exists.")
        else:
            course_fs.put("course.yaml", get_json_or_yaml("course.yaml", init_content))

        get_course_logger(courseid).info("Course %s created in the factory.", courseid)
Exemplo n.º 12
0
    def create_task(self, course, taskid, init_content):
        """ Create a new course folder and set initial descriptor content, folder can already exist
        :param course: a Course object
        :param taskid: the task id of the task
        :param init_content: initial descriptor content
        :raise: InvalidNameException or TaskAlreadyExistsException
        """
        if not id_checker(taskid):
            raise InvalidNameException("Task with invalid name: " + taskid)

        task_fs = self.get_task_fs(course.get_id(), taskid)
        task_fs.ensure_exists()

        if task_fs.exists("task.yaml"):
            raise TaskAlreadyExistsException("Task with id " + taskid + " already exists.")
        else:
            task_fs.put("task.yaml", get_json_or_yaml("task.yaml", init_content))

        get_course_logger(course.get_id()).info("Task %s created in the factory.", taskid)
Exemplo n.º 13
0
    def delete_task(self, courseid, taskid):
        """
        :param courseid: the course id of the course
        :param taskid: the task id of the task
        :raise InvalidNameException or CourseNotFoundException
        Erase the content of the task folder
        """
        if not id_checker(courseid):
            raise InvalidNameException("Course with invalid name: " + courseid)
        if not id_checker(taskid):
            raise InvalidNameException("Task with invalid name: " + taskid)

        task_fs = self.get_task_fs(courseid, taskid)

        if not task_fs.exists():
            raise TaskNotFoundException()

        task_fs.delete()

        get_course_logger(courseid).info("Task %s erased from the factory.",
                                         taskid)
        self._hook_manager.call_hook('task_deleted',
                                     courseid=courseid,
                                     taskid=taskid)
Exemplo n.º 14
0
 def _validate_list(self, usernames, coursename):
     """ Prevent MongoDB injections by verifying arrays sent to it """
     for i in usernames:
         if not username_checker(i):
             get_course_logger(coursename).debug("invalid username in list")
             raise web.notfound()