Example #1
0
    def create(self, validated_data: dict) -> ExportTask:
        # Create a new export task
        user = get_database_user(self._get_request.user)
        export_task = ExportTask.objects.create(user=user, data=validated_data)
        # Have Celery run the export in the background
        export_in_background.delay(export_task_uid=export_task.uid)

        return export_task
Example #2
0
    def create(self, request, *args, **kwargs):
        if self.request.user.is_anonymous:
            raise exceptions.NotAuthenticated()

        # Read valid options from POST data
        valid_options = (
            'type',
            'source',
            'group_sep',
            'lang',
            'hierarchy_in_labels',
            'fields_from_all_versions',
        )
        task_data = {}
        for opt in valid_options:
            opt_val = request.POST.get(opt, None)
            if opt_val is not None:
                task_data[opt] = opt_val
        # Complain if no source was specified
        if not task_data.get('source', False):
            raise exceptions.ValidationError(
                {'source': 'This field is required.'})
        # Get the source object
        source_type, source = _resolve_url_to_asset_or_collection(
            task_data['source'])
        # Complain if it's not an Asset
        if source_type != 'asset':
            raise exceptions.ValidationError(
                {'source': 'This field must specify an asset.'})
        # Complain if it's not deployed
        if not source.has_deployment:
            raise exceptions.ValidationError(
                {'source': 'The specified asset must be deployed.'})
        # Create a new export task
        export_task = ExportTask.objects.create(user=request.user,
                                                data=task_data)
        # Have Celery run the export in the background
        export_in_background.delay(export_task_uid=export_task.uid)
        return Response(
            {
                'uid':
                export_task.uid,
                'url':
                reverse('exporttask-detail',
                        kwargs={'uid': export_task.uid},
                        request=request),
                'status':
                ExportTask.PROCESSING
            }, status.HTTP_201_CREATED)