Beispiel #1
0
    def upload(self, request):

        if not settings.QUERY_UPLOAD:
            raise Http404

        serializer = QueryJobUploadSerializer(data=request.data,
                                              context={
                                                  'request': request,
                                                  'view': self
                                              })
        serializer.is_valid(raise_exception=True)

        file_name = handle_file_upload(
            get_user_upload_directory(self.request.user),
            serializer.validated_data['file'])

        job = QueryJob(job_type=QueryJob.JOB_TYPE_INTERFACE,
                       owner=(None if self.request.user.is_anonymous else
                              self.request.user),
                       run_id=serializer.validated_data.get('run_id'),
                       table_name=serializer.validated_data.get('table_name'),
                       client_ip=get_client_ip(self.request))
        job.process(upload=True)
        job.save()
        job.ingest(file_name)

        serializer = QueryJobRetrieveSerializer(instance=job)
        return Response(serializer.data)
Beispiel #2
0
    def perform_sync_job(self, request, data):
        # create the job objects
        job = self.get_queryset().model(
            job_type=Job.JOB_TYPE_SYNC,
            owner=(None
                   if self.request.user.is_anonymous else self.request.user),
            response_format=data.get('RESPONSEFORMAT'),
            max_records=data.get('MAXREC'),
            run_id=data.get('RUNID'),
            client_ip=get_client_ip(self.request))

        # add parameters to the job object
        for parameter, model_field in self.parameter_map.items():
            value = data.get(parameter)
            if value is not None:
                setattr(job, model_field, value)

        # handle possible uploads
        try:
            self.handle_upload(job, data.get('UPLOAD'))
        except ValueError:
            raise ValidationError('Could not parse VOTable')

        try:
            job.process()
        except ValidationError as e:
            raise ValidationError(self.rewrite_exception(e))

        return FileResponse(job.run_sync(),
                            content_type=job.formats[job.response_format])
Beispiel #3
0
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        # create the job objects
        job = self.get_queryset().model(
            job_type=Job.JOB_TYPE_ASYNC,
            owner=(None
                   if self.request.user.is_anonymous else self.request.user),
            response_format=serializer.validated_data.get('RESPONSEFORMAT'),
            max_records=serializer.validated_data.get('MAXREC'),
            # uploads=handle_uploads(request, serializer.validated_data.get('UPLOAD'), self.get_upload_directory()),
            run_id=serializer.validated_data.get('RUNID'),
            client_ip=get_client_ip(self.request))

        # add parameters to the job object
        for parameter, model_field in self.parameter_map.items():
            value = serializer.validated_data.get(parameter)
            if value is not None:
                setattr(job, model_field, value)

        # handle possible uploads
        self.handle_upload(job, serializer.validated_data.get('UPLOAD'))

        try:
            job.process()
        except ValidationError as e:
            raise ValidationError(self.rewrite_exception(e))

        job.save()

        if serializer.validated_data.get('PHASE') == job.PHASE_RUN:
            job.run()

        return HttpResponseSeeOther(self.get_success_url(job))
Beispiel #4
0
    def create(self, request):
        try:
            archive_job = ArchiveJob.objects.filter_by_owner(
                request.user).get(data=request.data)

            # check if the file was lost
            if archive_job.phase == archive_job.PHASE_COMPLETED and \
                    not os.path.isfile(archive_job.file_path):

                # set the phase back to pending so that the file is recreated
                archive_job.phase = archive_job.PHASE_PENDING
                archive_job.process()
                archive_job.save()
                archive_job.run()

        except ArchiveJob.DoesNotExist:
            archive_job = ArchiveJob(
                owner=(None if self.request.user.is_anonymous else
                       self.request.user),
                client_ip=get_client_ip(self.request),
                data=request.data)
            archive_job.process()
            archive_job.save()
            archive_job.run()

        return Response({'id': archive_job.id})
Beispiel #5
0
    def create_archive(self, request, pk=None):
        try:
            job = self.get_queryset().get(pk=pk)
        except QueryJob.DoesNotExist:
            raise NotFound

        column_name = request.data.get('column_name')

        try:
            archive_job = QueryArchiveJob.objects.get(job=job,
                                                      column_name=column_name)

            # check if the file was lost
            if archive_job.phase == archive_job.PHASE_COMPLETED and \
                    not os.path.isfile(archive_job.file_path):

                # set the phase back to pending so that the file is recreated
                archive_job.phase = archive_job.PHASE_PENDING
                archive_job.process()
                archive_job.save()
                archive_job.run()

        except QueryArchiveJob.DoesNotExist:
            archive_job = QueryArchiveJob(job_type=QueryJob.JOB_TYPE_INTERFACE,
                                          client_ip=get_client_ip(
                                              self.request),
                                          job=job,
                                          column_name=column_name)
            archive_job.process()
            archive_job.save()
            archive_job.run()

        return Response({'id': archive_job.id})
Beispiel #6
0
    def create_download(self, request, pk=None):
        try:
            job = self.get_queryset().get(pk=pk)
        except QueryJob.DoesNotExist:
            raise NotFound

        format_key = request.data.get('format_key')

        try:
            download_job = DownloadJob.objects.get(job=job,
                                                   format_key=format_key)

            # check if the file was lost
            if download_job.phase == download_job.PHASE_COMPLETED and \
                    not os.path.isfile(download_job.file_path):

                # set the phase back to pending so that the file is recreated
                download_job.phase = download_job.PHASE_PENDING
                download_job.process()
                download_job.save()
                download_job.run()

        except DownloadJob.DoesNotExist:
            download_job = DownloadJob(job_type=QueryJob.JOB_TYPE_INTERFACE,
                                       client_ip=get_client_ip(self.request),
                                       job=job,
                                       format_key=format_key)
            download_job.process()
            download_job.save()
            download_job.run()

        return Response({'id': download_job.id})
Beispiel #7
0
def send_file(request, file_path, search=None):
    # create a stats record for this download
    resource = {'file_path': file_path}
    if search:
        resource['search'] = search

    Record.objects.create(
        time=now(),
        resource_type='FILE',
        resource=resource,
        client_ip=get_client_ip(request),
        user=request.user if request.user.is_authenticated else None)

    # send the file to the client
    absolute_file_path = os.path.join(settings.FILES_BASE_PATH, file_path)
    return sendfile(request, absolute_file_path)
Beispiel #8
0
    def perform_create(self, serializer):
        job = QueryJob(job_type=QueryJob.JOB_TYPE_INTERFACE,
                       owner=(None if self.request.user.is_anonymous else
                              self.request.user),
                       run_id=serializer.data.get('run_id'),
                       table_name=serializer.data.get('table_name'),
                       query_language=serializer.data.get('query_language'),
                       query=serializer.data.get('query'),
                       queue=serializer.data.get('queue'),
                       client_ip=get_client_ip(self.request))
        job.process()
        job.save()
        job.run()

        # inject the job id into the serializers data
        serializer._data['id'] = job.id
Beispiel #9
0
    def create_download(self, request, pk=None, download_key=None):
        try:
            job = self.get_queryset().get(pk=pk)
        except QueryJob.DoesNotExist:
            raise NotFound

        download_config = get_download_config(download_key)
        if download_config is None:
            raise ValidationError({
                'download':
                'Download key "{}" is not supported.'.format(download_key)
            })

        download_job_model = import_class(download_config['model'])

        params = {
            param: request.data.get(param)
            for param in download_config.get('params', [])
        }

        try:
            download_job = download_job_model.objects.get(query_job=job,
                                                          **params)

            # check if the file was lost
            if download_job.phase == download_job.PHASE_COMPLETED and \
                    not os.path.isfile(download_job.file_path):

                # set the phase back to pending so that the file is recreated
                download_job.phase = download_job.PHASE_PENDING
                download_job.process()
                download_job.save()
                download_job.run()

        except download_job_model.DoesNotExist:
            download_job = download_job_model(
                job_type=QueryJob.JOB_TYPE_INTERFACE,
                owner=(None if self.request.user.is_anonymous else
                       self.request.user),
                client_ip=get_client_ip(self.request),
                query_job=job,
                **params)
            download_job.process()
            download_job.save()
            download_job.run()

        return Response({'id': download_job.id})
Beispiel #10
0
    def retrieve(self, request, pk=None):
        # get database adapter
        adapter = DatabaseAdapter()

        # get the schema_name and the table_name from the settings
        schema_name = settings.ARCHIVE_SCHEMA
        table_name = settings.ARCHIVE_TABLE

        # get collecions for this user
        collections = [
            collection.name for collection in
            Collection.objects.filter_by_access_level(request.user)
        ]

        # fetch the path for this file from the database
        row = adapter.fetch_row(schema_name,
                                table_name, ['path'],
                                filters={
                                    'id': pk,
                                    'collection': collections
                                })

        if row:
            if request.GET.get('download', True):
                # create a stats record for this download
                Record.objects.create(time=now(),
                                      resource_type='ARCHIVE_DOWNLOAD',
                                      resource=row[0],
                                      client_ip=get_client_ip(request),
                                      user=request.user if
                                      request.user.is_authenticated else None)

                # send the file to the client
                file_path = os.path.join(settings.ARCHIVE_BASE_PATH, row[0])
                return sendfile(request, file_path, attachment=True)
            else:
                # send an empty response
                return Response()

        # if the file was not found, return 404
        raise NotFound()