Ejemplo n.º 1
0
    def test_process_async_export_connection_error(self, mock_task):
        """
        Test process_async_export creates a new export.
        """
        mock_task.side_effect = ExportConnectionError
        self._publish_transportation_form_and_submit_instance()
        request = self.factory.post('/')
        request.user = self.user
        export_type = "csv"
        options = defaultdict(dict)

        with self.assertRaises(ServiceUnavailable):
            process_async_export(
                request, self.xform, export_type, options=options)
Ejemplo n.º 2
0
    def test_process_async_export_connection_error(self, mock_task):
        """
        Test process_async_export creates a new export.
        """
        mock_task.side_effect = ExportConnectionError
        self._publish_transportation_form_and_submit_instance()
        request = self.factory.post('/')
        request.user = self.user
        export_type = "csv"
        options = defaultdict(dict)

        with self.assertRaises(ServiceUnavailable):
            process_async_export(
                request, self.xform, export_type, options=options)
Ejemplo n.º 3
0
    def test_process_async_export_returns_existing_export(self):
        """
        Test process_async_export returns existing export.
        """
        self._publish_transportation_form_and_submit_instance()
        options = {
            "group_delimiter": "/",
            "remove_group_name": False,
            "split_select_multiples": True
        }

        request = Request(self.factory.post('/'))
        request.user = self.user
        export_type = "csv"

        self._create_old_export(self.xform,
                                export_type,
                                options,
                                filename="test_async_export")

        resp = process_async_export(request,
                                    self.xform,
                                    export_type,
                                    options=options)

        self.assertEquals(resp['job_status'], status_msg[SUCCESSFUL])
        self.assertIn("export_url", resp)
Ejemplo n.º 4
0
    def export_async(self, request, *args, **kwargs):
        params = request.query_params
        job_uuid = params.get('job_uuid')
        export_type = params.get('format')
        include_hxl = params.get('include_hxl', False)
        include_labels = params.get('include_labels', False)
        include_labels_only = params.get('include_labels_only', False)
        query = params.get("query")
        dataview = self.get_object()
        xform = dataview.xform

        if include_labels is not None:
            include_labels = str_to_bool(include_labels)

        if include_labels_only is not None:
            include_labels_only = str_to_bool(include_labels_only)

        if include_hxl is not None:
            include_hxl = str_to_bool(include_hxl)

        remove_group_name = params.get('remove_group_name', False)
        columns_with_hxl = get_columns_with_hxl(xform.survey.get('children'))

        if columns_with_hxl and include_hxl:
            include_hxl = include_hxl_row(
                dataview.columns, list(columns_with_hxl)
            )

        options = {
            'remove_group_name': remove_group_name,
            'dataview_pk': dataview.pk,
            'include_hxl': include_hxl,
            'include_labels': include_labels,
            'include_labels_only': include_labels_only
        }
        if query:
            options.update({'query': query})

        if job_uuid:
            job = AsyncResult(job_uuid)
            if job.state == 'SUCCESS':
                export_id = job.result
                export = Export.objects.get(id=export_id)

                resp = export_async_export_response(request, export)
            else:
                resp = {
                    'job_status': job.state
                }

        else:
            resp = process_async_export(request, xform, export_type,
                                        options=options)

        return Response(data=resp,
                        status=status.HTTP_202_ACCEPTED,
                        content_type="application/json")
Ejemplo n.º 5
0
    def export_async(self, request, *args, **kwargs):
        job_uuid = request.query_params.get('job_uuid')
        export_type = request.query_params.get('format')
        query = request.query_params.get("query")
        xform = self.get_object()

        token = request.query_params.get('token')
        meta = request.query_params.get('meta')
        data_id = request.query_params.get('data_id')
        options = parse_request_export_options(request.query_params)

        options.update({
            'meta': meta,
            'token': token,
            'data_id': data_id,
        })
        if query:
            options.update({'query': query})

        if request.query_params.get('format') in ['csvzip', 'savzip']:
            # Overide renderer and mediatype because all response are
            # suppose to be in json
            # TODO: Avoid overiding the format query param for export type
            #  DRF uses format to select the renderer
            self.request.accepted_renderer = renderers.JSONRenderer()
            self.request.accepted_mediatype = 'application/json'

        if job_uuid:
            try:
                resp = get_async_response(job_uuid, request, xform)
            except Export.DoesNotExist:
                # if this does not exist retry it against the primary
                try:
                    with use_master:
                        resp = get_async_response(job_uuid, request, xform)
                except NameError:
                    resp = get_async_response(job_uuid, request, xform)
        else:
            resp = process_async_export(request, xform, export_type, options)

            if isinstance(resp, HttpResponseRedirect):
                payload = {
                    "details": _("Google authorization needed"),
                    "url": resp.url
                }
                return Response(data=payload,
                                status=status.HTTP_403_FORBIDDEN,
                                content_type="application/json")

        self.etag_data = '{}'.format(timezone.now())

        return Response(data=resp,
                        status=status.HTTP_202_ACCEPTED,
                        content_type="application/json")
Ejemplo n.º 6
0
    def test_process_async_export_creates_new_export(self):
        self._publish_transportation_form_and_submit_instance()
        request = self.factory.post('/')
        request.user = self.user
        export_type = "csv"
        options = defaultdict(dict)

        resp = process_async_export(
            request, self.xform, export_type, options=options)

        self.assertIn('job_uuid', resp)
Ejemplo n.º 7
0
    def test_process_async_export_creates_new_export(self):
        """
        Test process_async_export creates a new export.
        """
        self._publish_transportation_form_and_submit_instance()
        request = self.factory.post('/')
        request.user = self.user
        export_type = "csv"
        options = defaultdict(dict)

        resp = process_async_export(
            request, self.xform, export_type, options=options)

        self.assertIn('job_uuid', resp)
Ejemplo n.º 8
0
    def export_async(self, request, *args, **kwargs):
        job_uuid = request.query_params.get('job_uuid')
        export_type = request.query_params.get('format')
        query = request.query_params.get("query")
        xform = self.get_object()

        token = request.query_params.get('token')
        meta = request.query_params.get('meta')
        data_id = request.query_params.get('data_id')
        options = parse_request_export_options(request.query_params)

        options.update({
            'meta': meta,
            'token': token,
            'data_id': data_id,
        })
        if query:
            options.update({'query': query})

        if job_uuid:
            try:
                resp = get_async_response(job_uuid, request, xform)
            except Export.DoesNotExist:
                # if this does not exist retry it against the primary
                try:
                    with use_master:
                        resp = get_async_response(job_uuid, request, xform)
                except NameError:
                    resp = get_async_response(job_uuid, request, xform)
        else:
            resp = process_async_export(request, xform, export_type, options)

            if isinstance(resp, HttpResponseRedirect):
                payload = {
                    "details": _("Google authorization needed"),
                    "url": resp.url
                }
                return Response(data=payload,
                                status=status.HTTP_403_FORBIDDEN,
                                content_type="application/json")

        self.etag_data = '{}'.format(timezone.now())

        return Response(data=resp,
                        status=status.HTTP_202_ACCEPTED,
                        content_type="application/json")
Ejemplo n.º 9
0
    def export_async(self, request, *args, **kwargs):
        job_uuid = request.query_params.get('job_uuid')
        export_type = request.query_params.get('format')
        query = request.query_params.get("query")
        xform = self.get_object()

        token = request.query_params.get('token')
        meta = request.query_params.get('meta')
        data_id = request.query_params.get('data_id')
        options = parse_request_export_options(request.query_params)

        options.update({
            'meta': meta,
            'token': token,
            'data_id': data_id,
        })
        if query:
            options.update({'query': query})

        if job_uuid:
            try:
                resp = get_async_response(job_uuid, request, xform)
            except Export.DoesNotExist:
                # if this does not exist retry it against the primary
                try:
                    with use_master:
                        resp = get_async_response(job_uuid, request, xform)
                except NameError:
                    resp = get_async_response(job_uuid, request, xform)
        else:
            resp = process_async_export(request, xform, export_type, options)

            if isinstance(resp, HttpResponseRedirect):
                payload = {
                    "details": _("Google authorization needed"),
                    "url": resp.url
                }
                return Response(data=payload,
                                status=status.HTTP_403_FORBIDDEN,
                                content_type="application/json")

        self.etag_data = '{}'.format(timezone.now())

        return Response(data=resp,
                        status=status.HTTP_202_ACCEPTED,
                        content_type="application/json")
Ejemplo n.º 10
0
    def export_async(self, request, *args, **kwargs):
        params = request.query_params
        job_uuid = params.get('job_uuid')
        export_type = params.get('format')
        include_hxl = params.get('include_hxl', False)
        dataview = self.get_object()
        xform = dataview.xform

        remove_group_name = params.get('remove_group_name', False)
        columns_with_hxl = get_columns_with_hxl(xform.survey.get('children'))

        if columns_with_hxl and include_hxl:
            include_hxl = include_hxl_row(dataview.columns,
                                          columns_with_hxl.keys())

        options = {
            'remove_group_name': remove_group_name,
            'dataview_pk': dataview.pk,
            'include_hxl': include_hxl
        }

        if job_uuid:
            job = AsyncResult(job_uuid)
            if job.state == 'SUCCESS':
                export_id = job.result
                export = Export.objects.get(id=export_id)

                resp = _export_async_export_response(request,
                                                     xform,
                                                     export,
                                                     dataview_pk=dataview.pk)
            else:
                resp = {'job_status': job.state}

        else:
            resp = process_async_export(request,
                                        xform,
                                        export_type,
                                        options=options)

        return Response(data=resp,
                        status=status.HTTP_202_ACCEPTED,
                        content_type="application/json")
Ejemplo n.º 11
0
    def test_process_async_export_returns_existing_export(self):
        """
        Test process_async_export returns existing export.
        """
        self._publish_transportation_form_and_submit_instance()
        options = {
            "group_delimiter": "/",
            "remove_group_name": False,
            "split_select_multiples": True
        }

        request = Request(self.factory.post('/'))
        request.user = self.user
        export_type = "csv"

        self._create_old_export(
            self.xform, export_type, options, filename="test_async_export")

        resp = process_async_export(
            request, self.xform, export_type, options=options)

        self.assertEquals(resp['job_status'], status_msg[SUCCESSFUL])
        self.assertIn("export_url", resp)