示例#1
0
    def test_export_task(self):
        """Test exporting resources task."""
        resources = LearningResource.objects.all()

        result = export_resources.delay(
            resources, self.user.username).get()
        path = result['name']
        collision = result['collision']
        tempdir = mkdtemp()

        self.assertTrue(collision)

        # HACK: Have to patch in "seekable" attribute for python3 and tar
        # See: https://code.djangoproject.com/ticket/24963#ticket. Remove
        # when updating to Django 1.9
        def seekable():
            """Hacked seekable for django storage to work in python3."""
            return True
        try:
            resource_archive = default_storage.open(path)
            resource_archive.seekable = seekable
            Archive(resource_archive, ext='.tar.gz').extract(
                to_path=tempdir, method='safe'
            )
            assert_resource_directory(self, resources, tempdir)
        finally:
            rmtree(tempdir)
            default_storage.delete(path)
示例#2
0
    def test_export_task(self):
        """Test exporting resources task."""
        resources = LearningResource.objects.all()

        result = export_resources.delay(resources, self.user.username).get()
        path = result['name']
        collision = result['collision']
        tempdir = mkdtemp()

        self.assertTrue(collision)

        # HACK: Have to patch in "seekable" attribute for python3 and tar
        # See: https://code.djangoproject.com/ticket/24963#ticket. Remove
        # when updating to Django 1.9
        def seekable():
            """Hacked seekable for django storage to work in python3."""
            return True

        try:
            resource_archive = default_storage.open(path)
            resource_archive.seekable = seekable
            Archive(resource_archive, ext='.tar.gz').extract(to_path=tempdir,
                                                             method='safe')
            assert_resource_directory(self, resources, tempdir)
        finally:
            rmtree(tempdir)
            default_storage.delete(path)
示例#3
0
文件: tasks.py 项目: olabi/lore
def create_task(session, user_id, task_type, task_info):
    """
    Start a new Celery task from REST API.

    Args:
        session (SessionStore): The request session.
        user_id (int): The id for user creating task.
        task_type (unicode): The type of task being started.
        task_info (dict): Extra information about the task.
    Returns:
        dict: The initial task data (will also be stored in session).
    """

    if task_type == EXPORT_TASK_TYPE:
        try:
            repo_slug = task_info['repo_slug']
        except KeyError:
            raise ValidationError("Missing repo_slug")

        # Verify repository ownership.
        get_repo(repo_slug, user_id)

        try:
            exports = set(session[EXPORTS_KEY][repo_slug])
        except KeyError:
            exports = set()

        try:
            ids = task_info['ids']
        except KeyError:
            raise ValidationError("Missing ids")

        for resource_id in ids:
            if resource_id not in exports:
                raise ValidationError("id {id} is not in export list".format(
                    id=resource_id
                ))

        learning_resources = LearningResource.objects.filter(id__in=ids).all()
        user = User.objects.get(id=user_id)
        result = export_resources.delay(learning_resources, user.username)

        # Put new task in session.
        initial_data = track_task(session, result, task_type, task_info)

        return initial_data
    else:
        raise ValidationError("Unknown task_type {task_type}".format(
            task_type=task_type
        ))
示例#4
0
def create_task(session, user_id, task_type, task_info):
    """
    Start a new Celery task from REST API.

    Args:
        session (SessionStore): The request session.
        user_id (int): The id for user creating task.
        task_type (unicode): The type of task being started.
        task_info (dict): Extra information about the task.
    Returns:
        dict: The initial task data (will also be stored in session).
    """

    if task_type == EXPORT_TASK_TYPE:
        try:
            repo_slug = task_info['repo_slug']
        except KeyError:
            raise ValidationError("Missing repo_slug")

        # Verify repository ownership.
        get_repo(repo_slug, user_id)

        try:
            exports = set(session[EXPORTS_KEY][repo_slug])
        except KeyError:
            exports = set()

        try:
            ids = task_info['ids']
        except KeyError:
            raise ValidationError("Missing ids")

        for resource_id in ids:
            if resource_id not in exports:
                raise ValidationError(
                    "id {id} is not in export list".format(id=resource_id))

        learning_resources = LearningResource.objects.filter(id__in=ids).all()
        user = User.objects.get(id=user_id)
        result = export_resources.delay(learning_resources, user.username)

        # Put new task in session.
        initial_data = track_task(session, result, task_type, task_info)

        return initial_data
    else:
        raise ValidationError(
            "Unknown task_type {task_type}".format(task_type=task_type))