Exemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        """
        Create a new task for this user.

        Note that this also cancels and clears any old tasks for the user,
        so there should be only one task in the list at any time.
        """

        if 'task_type' not in self.request.data:
            raise ValidationError("Missing task_type.")
        if 'task_info' not in self.request.data:
            raise ValidationError("Missing task_info.")

        try:
            result = create_task(
                self.request.session,
                self.request.user.id,
                self.request.data['task_type'],
                self.request.data['task_info']
            )
        except PermissionDenied:
            raise DjangoPermissionDenied
        except NotFound:
            raise Http404

        return Response(
            {"id": result['id']},
            status=status.HTTP_201_CREATED
        )
Exemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        """
        Create a new export task for this user.

        Note that this also cancels and clears any old tasks for the user,
        so there should be only one task in the list at any time.

        The export list will be left alone. Once the list is exported
        the client may DELETE the export list as a separate REST call.
        """
        repo_slug = self.kwargs['repo_slug']
        export_tasks = [
            task for task in get_tasks(self.request.session).values()
            if task['task_type'] == EXPORT_TASK_TYPE and
            task['task_info']['repo_slug'] == repo_slug
        ]

        # Cancel any old tasks.
        for task in export_tasks:
            remove_task(self.request.session, task['id'])

        try:
            ids = self.request.data['ids']
        except KeyError:
            raise ValidationError("Missing ids")

        task = create_task(
            self.request.session,
            self.request.user.id,
            EXPORT_TASK_TYPE,
            {
                'repo_slug': repo_slug,
                'ids': ids
            }
        )

        return Response(
            {"id": task['id']},
            status=status.HTTP_201_CREATED
        )