Beispiel #1
0
def export_list(request, username, id_string, export_type):
    if export_type == Export.GDOC_EXPORT:
        redirect_url = reverse(
            export_list,
            kwargs={
                'username': username, 'id_string': id_string,
                'export_type': export_type})
        token = _get_google_token(request, redirect_url)
        if isinstance(token, HttpResponse):
            return token
    owner = get_object_or_404(User, username=username)
    xform = get_object_or_404(XForm, id_string=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if should_create_new_export(xform, export_type):
        try:
            create_async_export(
                xform, export_type, query=None, force_xlsx=True)
        except Export.ExportTypeError:
            return HttpResponseBadRequest(
                _("%s is not a valid export type" % export_type))

    context = RequestContext(request)
    context.username = owner.username
    context.xform = xform
    # TODO: better output e.g. Excel instead of XLS
    context.export_type = export_type
    context.export_type_name = Export.EXPORT_TYPE_DICT[export_type]
    exports = Export.objects.filter(xform=xform, export_type=export_type)\
        .order_by('-created_on')
    context.exports = exports
    return render_to_response('export_list.html', context_instance=context)
Beispiel #2
0
    def create(self, request, *args, **kwargs):
        params = self.request.query_params
        id = params.get('id')
        fsxf = params.get('fsxf')
        is_project = params.get('is_project')
        version = params.get('version', 0)
        if not (id and fsxf and is_project):
            return Response({'error': 'Parameters missing'},
                            status=status.HTTP_400_BAD_REQUEST)
        fsxf = FieldSightXF.objects.get(pk=fsxf)
        if is_project == 1 or is_project == '1':
            site_id = None
            query = {"fs_project_uuid": str(fsxf.id)}
        else:
            site_id = id
            if fsxf.site:
                query = {"fs_uuid": str(fsxf.id)}
            else:
                query = {"fs_project_uuid": str(fsxf.id), "fs_site": site_id}
        force_xlsx = True
        if version not in ["0", 0]:
            query["__version__"] = version
        deleted_at_query = {
            "$or": [{
                "_deleted_at": {
                    "$exists": False
                }
            }, {
                "_deleted_at": None
            }]
        }
        # join existing query with deleted_at_query on an $and
        query = {"$and": [query, deleted_at_query]}
        print("query at excel generation", query)

        # export options
        group_delimiter = request.POST.get("group_delimiter", '/')
        if group_delimiter not in ['.', '/']:
            return Response(
                {'error': _("%s is not a valid delimiter" % group_delimiter)},
                status=status.HTTP_400_BAD_REQUEST)

        # default is True, so when dont_.. is yes
        # split_select_multiples becomes False
        split_select_multiples = request.POST.get(
            "dont_split_select_multiples", "no") == "no"

        binary_select_multiples = False
        # external export option
        meta = request.POST.get("meta")
        options = {
            'group_delimiter': group_delimiter,
            'split_select_multiples': split_select_multiples,
            'binary_select_multiples': binary_select_multiples,
            'meta': meta.replace(",", "") if meta else None
        }

        create_async_export(fsxf.xf, 'xls', query, force_xlsx, options,
                            is_project, fsxf.id, site_id, version, False)
        return Response(status=status.HTTP_204_NO_CONTENT)
Beispiel #3
0
def export_list(request, username, id_string, export_type):
    if export_type == Export.GDOC_EXPORT:
        redirect_url = reverse(
            export_list,
            kwargs={
                'username': username, 'id_string': id_string,
                'export_type': export_type})
        token = _get_google_token(request, redirect_url)
        if isinstance(token, HttpResponse):
            return token
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_object_or_404(XForm, id_string__iexact=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if should_create_new_export(xform, export_type):
        try:
            create_async_export(
                xform, export_type, query=None, force_xlsx=True)
        except Export.ExportTypeError:
            return HttpResponseBadRequest(
                _("%s is not a valid export type" % export_type))

    data = {
        'username': owner.username,
        'xform': xform,
        'export_type': export_type,
        'export_type_name': Export.EXPORT_TYPE_DICT[export_type],
        'exports': Export.objects.filter(
            xform=xform, export_type=export_type).order_by('-created_on')
    }

    return render(request, 'export_list.html', data)
Beispiel #4
0
def export_list(request, username, id_string, export_type):
    if export_type == Export.GDOC_EXPORT:
        redirect_url = reverse(
            export_list,
            kwargs={
                'username': username, 'id_string': id_string,
                'export_type': export_type})
        token = _get_google_token(request, redirect_url)
        if isinstance(token, HttpResponse):
            return token
    owner = get_object_or_404(User, username=username)
    xform = get_object_or_404(XForm, id_string=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if should_create_new_export(xform, export_type):
        try:
            create_async_export(
                xform, export_type, query=None, force_xlsx=True)
        except Export.ExportTypeError:
            return HttpResponseBadRequest(
                _("%s is not a valid export type" % export_type))

    context = RequestContext(request)
    context.username = owner.username
    context.xform = xform
    # TODO: better output e.g. Excel instead of XLS
    context.export_type = export_type
    context.export_type_name = Export.EXPORT_TYPE_DICT[export_type]
    exports = Export.objects.filter(xform=xform, export_type=export_type)\
        .order_by('-created_on')
    context.exports = exports
    return render_to_response('export_list.html', context_instance=context)
Beispiel #5
0
def create_export(request, username, id_string, export_type):
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if export_type == Export.EXTERNAL_EXPORT:
        # check for template before trying to generate a report
        if not MetaData.external_export(xform=xform):
            return HttpResponseForbidden(_(u'No XLS Template set.'))

    query = request.POST.get("query")
    force_xlsx = request.POST.get('xls') != 'true'

    # export options
    group_delimiter = request.POST.get("options[group_delimiter]", '/')
    if group_delimiter not in ['.', '/']:
        return HttpResponseBadRequest(
            _("%s is not a valid delimiter" % group_delimiter))

    # default is True, so when dont_.. is yes
    # split_select_multiples becomes False
    split_select_multiples = request.POST.get(
        "options[dont_split_select_multiples]", "no") == "no"

    binary_select_multiples = getattr(settings, 'BINARY_SELECT_MULTIPLES',
                                      False)
    # external export option
    meta = request.POST.get("meta")
    options = {
        'group_delimiter': group_delimiter,
        'split_select_multiples': split_select_multiples,
        'binary_select_multiples': binary_select_multiples,
        'meta': meta.replace(",", "") if meta else None
    }

    try:
        create_async_export(xform, export_type, query, force_xlsx, options)
    except Export.ExportTypeError:
        return HttpResponseBadRequest(
            _("%s is not a valid export type" % export_type))
    else:
        audit = {"xform": xform.id_string, "export_type": export_type}
        audit_log(
            Actions.EXPORT_CREATED, request.user, owner,
            _("Created %(export_type)s export on '%(id_string)s'.") % {
                'export_type': export_type.upper(),
                'id_string': xform.id_string,
            }, audit, request)
        return HttpResponseRedirect(
            reverse(export_list,
                    kwargs={
                        "username": username,
                        "id_string": id_string,
                        "export_type": export_type
                    }))
Beispiel #6
0
def create_export(request, username, id_string, export_type):
    owner = get_object_or_404(User, username=username)
    xform = get_object_or_404(XForm, id_string=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    query = request.POST.get("query")
    force_xlsx = request.POST.get('xls') != 'true'

    # export options
    group_delimiter = request.POST.get("options[group_delimiter]", '/')
    if group_delimiter not in ['.', '/']:
        return HttpResponseBadRequest(
            _("%s is not a valid delimiter" % group_delimiter))

    # default is True, so when dont_.. is yes
    # split_select_multiples becomes False
    split_select_multiples = request.POST.get(
        "options[dont_split_select_multiples]", "no") == "no"

    binary_select_multiples = getattr(settings, 'BINARY_SELECT_MULTIPLES',
                                      False)

    options = {
        'group_delimiter': group_delimiter,
        'split_select_multiples': split_select_multiples,
        'binary_select_multiples': binary_select_multiples
    }

    try:
        create_async_export(xform, export_type, query, force_xlsx, options)
    except Export.ExportTypeError:
        return HttpResponseBadRequest(
            _("%s is not a valid export type" % export_type))
    else:
        audit = {
            "xform": xform.id_string,
            "export_type": export_type
        }
        audit_log(
            Actions.EXPORT_CREATED, request.user, owner,
            _("Created %(export_type)s export on '%(id_string)s'.") %
            {
                'export_type': export_type.upper(),
                'id_string': xform.id_string,
            }, audit, request)
        return HttpResponseRedirect(reverse(
            export_list,
            kwargs={
                "username": username,
                "id_string": id_string,
                "export_type": export_type
            })
        )
Beispiel #7
0
def export_list(request, username, id_string, export_type):
    if export_type == Export.GDOC_EXPORT:
        redirect_url = reverse(
            export_list,
            kwargs={
                'username': username, 'id_string': id_string,
                'export_type': export_type})
        token = _get_google_token(request, redirect_url)
        if isinstance(token, HttpResponse):
            return token
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if export_type == Export.EXTERNAL_EXPORT:
        # check for template before trying to generate a report
        if not MetaData.external_export(xform=xform):
            return HttpResponseForbidden(_(u'No XLS Template set.'))
    # Get meta and token
    export_token = request.GET.get('token')
    export_meta = request.GET.get('meta')
    options = {
        'meta': export_meta,
        'token': export_token,
    }

    if should_create_new_export(xform, export_type):
        try:
            create_async_export(
                xform, export_type, query=None, force_xlsx=True,
                options=options)
        except Export.ExportTypeError:
            return HttpResponseBadRequest(
                _("%s is not a valid export type" % export_type))

    metadata = MetaData.objects.filter(xform=xform,
                                       data_type="external_export")\
        .values('id', 'data_value')

    for m in metadata:
        m['data_value'] = m.get('data_value').split('|')[0]

    data = {
        'username': owner.username,
        'xform': xform,
        'export_type': export_type,
        'export_type_name': Export.EXPORT_TYPE_DICT[export_type],
        'exports': Export.objects.filter(
            xform=xform, export_type=export_type).order_by('-created_on'),
        'metas': metadata
    }

    return render(request, 'export_list.html', data)
Beispiel #8
0
def export_list(request, username, id_string, export_type):
    if export_type == Export.GDOC_EXPORT:
        redirect_url = reverse(
            export_list,
            kwargs={
                'username': username, 'id_string': id_string,
                'export_type': export_type})
        token = _get_google_token(request, redirect_url)
        if isinstance(token, HttpResponse):
            return token
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if export_type == Export.EXTERNAL_EXPORT:
        # check for template before trying to generate a report
        if not MetaData.external_export(xform=xform):
            return HttpResponseForbidden(_(u'No XLS Template set.'))
    # Get meta and token
    export_token = request.GET.get('token')
    export_meta = request.GET.get('meta')
    options = {
        'meta': export_meta,
        'token': export_token,
    }

    if should_create_new_export(xform, export_type):
        try:
            create_async_export(
                xform, export_type, query=None, force_xlsx=True,
                options=options)
        except Export.ExportTypeError:
            return HttpResponseBadRequest(
                _("%s is not a valid export type" % export_type))

    metadata = MetaData.objects.filter(xform=xform,
                                       data_type="external_export")\
        .values('id', 'data_value')

    for m in metadata:
        m['data_value'] = m.get('data_value').split('|')[0]

    data = {
        'username': owner.username,
        'xform': xform,
        'export_type': export_type,
        'export_type_name': Export.EXPORT_TYPE_DICT[export_type],
        'exports': Export.objects.filter(
            xform=xform, export_type=export_type).order_by('-created_on'),
        'metas': metadata
    }

    return render(request, 'export_list.html', data)
Beispiel #9
0
    def create(self, request, *args, **kwargs):
        params = self.request.query_params
        org_form_lib = params.get('org_form_lib')
        version = params.get('version', 0)

        if not org_form_lib:
            return Response({'error': 'Parameters missing'},
                            status=status.HTTP_400_BAD_REQUEST)
        xform = OrganizationFormLibrary.objects.get(id=org_form_lib).xf

        query = {"fs_organization_uuid": org_form_lib}

        force_xlsx = True
        if version not in ["0", 0]:
            query["__version__"] = version
        deleted_at_query = {
            "$or": [{
                "_deleted_at": {
                    "$exists": False
                }
            }, {
                "_deleted_at": None
            }]
        }
        # join existing query with deleted_at_query on an $and
        query = {"$and": [query, deleted_at_query]}
        print("query at excel generation", query)

        # export options
        group_delimiter = request.POST.get("group_delimiter", '/')
        if group_delimiter not in ['.', '/']:
            return Response(
                {'error': _("%s is not a valid delimiter" % group_delimiter)},
                status=status.HTTP_400_BAD_REQUEST)

        # default is True, so when dont_.. is yes
        # split_select_multiples becomes False
        split_select_multiples = request.POST.get(
            "dont_split_select_multiples", "no") == "no"

        binary_select_multiples = False
        # external export option
        meta = request.POST.get("meta")
        options = {
            'group_delimiter': group_delimiter,
            'split_select_multiples': split_select_multiples,
            'binary_select_multiples': binary_select_multiples,
            'meta': meta.replace(",", "") if meta else None
        }

        create_async_export(xform, 'xls', query, force_xlsx, options, False,
                            None, None, version, False, None, org_form_lib)
        return Response({'message': 'Your Download Has Been Started'},
                        status=status.HTTP_200_OK)
Beispiel #10
0
    def test_project_serializer(self):
        request = APIRequestFactory().get('/')
        self._publish_xls_form_to_project()
        temp_dir = settings.MEDIA_ROOT
        dummy_export_file = NamedTemporaryFile(suffix='.xlsx', dir=temp_dir)
        filename = os.path.basename(dummy_export_file.name)
        filedir = os.path.dirname(dummy_export_file.name)
        options = {"group_delimiter": "/",
                   "remove_group_name": False,
                   "split_select_multiples": True}
        export = Export.objects.create(xform=self.xform,
                                       filename=filename,
                                       filedir=filedir,
                                       options=options,
                                       internal_status=True)
        export.save()
        export = create_async_export(self.xform, 'csv', None, False, options)
        serializer = ExportSerializer(instance=export[0], context={'request':
                                                                   request})
        self.assertEqual(list(serializer.data), ['id', 'job_status', 'type',
                                                 'task_id', 'xform',
                                                 'date_created', 'filename',
                                                 'options', 'export_url'])
        self.assertEqual(
            serializer.data.get('export_url'),
            'http://testserver/api/v1/export/%s.csv' % export[0].id
        )

        new_url = 'http://test.url/export/endpoint/123ABC'
        export[0].export_url = new_url
        export[0].save()
        serializer = ExportSerializer(instance=export[0], context={'request':
                                                                   request})

        self.assertEqual(serializer.data.get('export_url'), new_url)
Beispiel #11
0
 def test_project_serializer(self):
     request = APIRequestFactory().get('/')
     self._publish_xls_form_to_project()
     temp_dir = settings.MEDIA_ROOT
     dummy_export_file = NamedTemporaryFile(suffix='.xlsx', dir=temp_dir)
     filename = os.path.basename(dummy_export_file.name)
     filedir = os.path.dirname(dummy_export_file.name)
     options = {
         "group_delimiter": "/",
         "remove_group_name": False,
         "split_select_multiples": True
     }
     export = Export.objects.create(xform=self.xform,
                                    filename=filename,
                                    filedir=filedir,
                                    options=options,
                                    internal_status=True)
     export.save()
     export = create_async_export(self.xform, 'csv', None, False, options)
     serializer = ExportSerializer(instance=export[0],
                                   context={'request': request})
     self.assertEqual(serializer.data.keys(), [
         'id', 'job_status', 'type', 'task_id', 'xform', 'date_created',
         'filename', 'options'
     ])
Beispiel #12
0
def _create_export_async(xform,
                         export_type,
                         query=None,
                         force_xlsx=False,
                         options=None):
    """
        Creates async exports
        :param xform:
        :param export_type:
        :param query:
        :param force_xlsx:
        :param options:
        :return:
            job_uuid generated
        """
    export = check_pending_export(xform, export_type, options)

    if export:
        return export.task_id

    try:
        export, async_result = viewer_task.create_async_export(xform,
                                                               export_type,
                                                               query,
                                                               force_xlsx,
                                                               options=options)
    except ExportConnectionError:
        raise ServiceUnavailable

    return async_result.task_id
Beispiel #13
0
    def test_create_async(self):

        self._publish_transportation_form_and_submit_instance()
        options = {"group_delimiter": "/",
                   "remove_group_name": False,
                   "split_select_multiples": True}
        export_types = ((Export.XLS_EXPORT, {}),
                        (Export.GOOGLE_SHEETS_EXPORT, {}),
                        (Export.CSV_EXPORT, {}),
                        (Export.CSV_ZIP_EXPORT, {}),
                        (Export.SAV_ZIP_EXPORT, {}),
                        (Export.ZIP_EXPORT, {}),
                        (Export.KML_EXPORT, {}),
                        (Export.OSM_EXPORT, {}),
                        (Export.EXTERNAL_EXPORT, {"meta": "j2x.ona.io"}),
                        )

        for export_type, extra_options in export_types:
            result = create_async_export(
                self.xform, export_type, None, False, options)

            export = result[0]
            self.assertTrue(export.id)
            self.assertIn("username", options)
            self.assertEquals(options.get("id_string"), self.xform.id_string)
Beispiel #14
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')
        remove_group_name = request.QUERY_PARAMS.get('remove_group_name')

        options = {
            'meta': meta,
            'token': token,
            'data_id': data_id,
            'remove_group_name': remove_group_name
        }

        if job_uuid:
            job = AsyncResult(job_uuid)
            if job.state == 'SUCCESS':
                export_id = job.result
                export = Export.objects.get(id=export_id)
                if export.status == Export.SUCCESSFUL:
                    if export.export_type != Export.EXTERNAL_EXPORT:
                        export_url = reverse('xform-detail',
                                             kwargs={
                                                 'pk': xform.pk,
                                                 'format': export.export_type
                                             },
                                             request=request)
                    else:
                        export_url = export.export_url
                    resp = {
                        u'job_status': job.state,
                        u'export_url': export_url
                    }
                elif export.status == Export.PENDING:
                    resp = {'export_status': 'Pending'}
                else:
                    resp = {'export_status': "Failed"}
            else:
                resp = {'job_status': job.state}

        else:
            export_type = _get_export_type(export_type)

            if export_type in external_export_types and \
                    (token is not None) or (meta is not None):
                export_type = Export.EXTERNAL_EXPORT
            export, async_result = viewer_task.create_async_export(
                xform, export_type, query, False, options=options)
            resp = {u'job_uuid': async_result.task_id}
        resp_code = status.HTTP_202_ACCEPTED
        return Response(data=resp,
                        status=resp_code,
                        content_type="application/json")
Beispiel #15
0
def _create_export_async(xform,
                         export_type,
                         query=None,
                         force_xlsx=False,
                         options=None):
    """
        Creates async exports
        :param xform:
        :param export_type:
        :param query:
        :param force_xlsx:
        :param options:
        :return:
            job_uuid generated
        """
    export, async_result \
        = viewer_task.create_async_export(xform, export_type, query,
                                          force_xlsx, options=options)
    return async_result.task_id
Beispiel #16
0
    def test_create_async(self):

        self._publish_transportation_form_and_submit_instance()
        options = {"group_delimiter": "/",
                   "remove_group_name": False,
                   "split_select_multiples": True}
        export_types = ((Export.XLS_EXPORT, {}),
                        (Export.GOOGLE_SHEETS_EXPORT, {}),
                        (Export.CSV_EXPORT, {}),
                        (Export.CSV_ZIP_EXPORT, {}),
                        (Export.SAV_ZIP_EXPORT, {}),
                        (Export.ZIP_EXPORT, {}),
                        (Export.KML_EXPORT, {}),
                        (Export.OSM_EXPORT, {}),
                        (Export.EXTERNAL_EXPORT, {"meta": "j2x.ona.io"}),
                        )

        for export_type, extra_options in export_types:
            result = create_async_export(
                self.xform, export_type, None, False, options)
            export = result[0]
            self.assertTrue(export.id)
            self.assertIn("username", options)
            self.assertEquals(options.get("id_string"), self.xform.id_string)
Beispiel #17
0
def export_list(request, username, id_string, export_type, is_project=0, id=0, site_id=0, version="0"):
    site_id = int(site_id)
    if export_type == Export.GDOC_EXPORT:
        return HttpResponseForbidden(_(u'Not shared.'))
        token = _get_google_token(request, redirect_url)
        if isinstance(token, HttpResponse):
            return token
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner)

    if export_type == Export.EXTERNAL_EXPORT:
        return HttpResponseForbidden(_(u'Not shared.'))
        # check for template before trying to generate a report
        if not MetaData.external_export(xform=xform):
            return HttpResponseForbidden(_(u'No XLS Template set.'))
    # Get meta and token
    export_token = request.GET.get('token')
    export_meta = request.GET.get('meta')
    options = {
        'meta': export_meta,
        'token': export_token,
    }
    if should_create_new_export(xform, export_type, id, site_id, version):

        if is_project == 1 or is_project == '1':
            query = {"fs_project_uuid": str(id)}
        else:
            fsxf = FieldSightXF.objects.get(pk=id)
            if fsxf.site:
                query = {"fs_uuid": str(id)}
            else:
                query = {"fs_project_uuid": str(id), "fs_site": site_id}
        force_xlsx = True
        if version not in ["0", 0]:
            query["__version__"] = version
        deleted_at_query = {
            "$or": [{"_deleted_at": {"$exists": False}},
                    {"_deleted_at": None}]}
        # join existing query with deleted_at_query on an $and
        query = {"$and": [query, deleted_at_query]}
        print("query at excel generation", query)
        try:
            create_async_export(xform, export_type, query,
                                force_xlsx, options, is_project, id, site_id, version)
        except Export.ExportTypeError:
            return HttpResponseBadRequest(
                _("%s is not a valid export type" % export_type))

    metadata = MetaData.objects.filter(xform=xform,
                                       data_type="external_export")\
        .values('id', 'data_value')

    for m in metadata:
        m['data_value'] = m.get('data_value').split('|')[0]

    data = {
        'username': xform.user.username,
        'xform': xform,
        'export_type': export_type,
        'export_type_name': Export.EXPORT_TYPE_DICT[export_type],
        'exports': Export.objects.filter(
            xform=xform, export_type=export_type, fsxf=id, site=site_id, version=version).order_by('-created_on'),
        'metas': metadata,
        'is_project': is_project,
        'id': id,
        'version': version,
            }
    data['site_id'] = site_id

    return render(request, 'export_list.html', data)
Beispiel #18
0
def create_export(request, username, id_string, export_type):
    """
    Create async export tasks view.
    """
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if export_type == Export.EXTERNAL_EXPORT:
        # check for template before trying to generate a report
        if not MetaData.external_export(xform):
            return HttpResponseForbidden(_(u'No XLS Template set.'))

    credential = None
    if export_type == Export.GOOGLE_SHEETS_EXPORT:

        credential = _get_google_credential(request)
        if isinstance(credential, HttpResponseRedirect):
            return credential

    query = request.POST.get("query")
    force_xlsx = request.POST.get('xls') != 'true'

    # export options
    group_delimiter = request.POST.get("options[group_delimiter]", '/')
    if group_delimiter not in ['.', '/']:
        return HttpResponseBadRequest(
            _("%s is not a valid delimiter" % group_delimiter))

    # default is True, so when dont_.. is yes
    # split_select_multiples becomes False
    split_select_multiples = request.POST.get(
        "options[dont_split_select_multiples]", "no") == "no"

    binary_select_multiples = getattr(settings, 'BINARY_SELECT_MULTIPLES',
                                      False)
    remove_group_name = request.POST.get("options[remove_group_name]", "false")
    value_select_multiples = request.POST.get(
        "options[value_select_multiples]", "false")

    # external export option
    meta = request.POST.get("meta")
    options = {
        'group_delimiter': group_delimiter,
        'split_select_multiples': split_select_multiples,
        'binary_select_multiples': binary_select_multiples,
        'value_select_multiples': str_to_bool(value_select_multiples),
        'remove_group_name': str_to_bool(remove_group_name),
        'meta': meta.replace(",", "") if meta else None,
        'google_credentials': credential
    }

    try:
        create_async_export(xform, export_type, query, force_xlsx, options)
    except ExportTypeError:
        return HttpResponseBadRequest(
            _("%s is not a valid export type" % export_type))
    else:
        audit = {"xform": xform.id_string, "export_type": export_type}
        audit_log(Actions.EXPORT_CREATED, request.user, owner,
                  _("Created %(export_type)s export on '%(id_string)s'.") % {
                      'export_type': export_type.upper(),
                      'id_string': xform.id_string,
                  }, audit, request)
        return HttpResponseRedirect(
            reverse(
                export_list,
                kwargs={
                    "username": username,
                    "id_string": id_string,
                    "export_type": export_type
                }))
Beispiel #19
0
def create_export(request,
                  username,
                  id_string,
                  export_type,
                  is_project=None,
                  id=None,
                  site_id=0,
                  version="0",
                  sync_to_gsuit="0"):
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner)
    if export_type == Export.EXTERNAL_EXPORT:
        # check for template before trying to generate a report
        if not MetaData.external_export(xform=xform):
            return HttpResponseForbidden(_(u'No XLS Template set.'))

    if is_project == 1 or is_project == '1':
        query = {"fs_project_uuid": str(id)}
    else:
        fsxf = FieldSightXF.objects.get(pk=id)
        if fsxf.site:
            query = {"fs_uuid": str(id)}
        else:
            query = {"fs_project_uuid": str(id), "fs_site": site_id}
    force_xlsx = True
    if version not in ["0", 0]:
        query["__version__"] = version
    deleted_at_query = {
        "$or": [{
            "_deleted_at": {
                "$exists": False
            }
        }, {
            "_deleted_at": None
        }]
    }
    # join existing query with deleted_at_query on an $and
    query = {"$and": [query, deleted_at_query]}
    print("query at excel generation", query)

    # export options
    group_delimiter = request.POST.get("options[group_delimiter]", '/')
    if group_delimiter not in ['.', '/']:
        return HttpResponseBadRequest(
            _("%s is not a valid delimiter" % group_delimiter))

    # default is True, so when dont_.. is yes
    # split_select_multiples becomes False
    split_select_multiples = request.POST.get(
        "options[dont_split_select_multiples]", "no") == "no"

    binary_select_multiples = getattr(settings, 'BINARY_SELECT_MULTIPLES',
                                      False)
    # external export option
    meta = request.POST.get("meta")
    options = {
        'group_delimiter': group_delimiter,
        'split_select_multiples': split_select_multiples,
        'binary_select_multiples': binary_select_multiples,
        'meta': meta.replace(",", "") if meta else None
    }

    try:
        sync_to_gsuit = True if sync_to_gsuit in ["1", 1] else False
        create_async_export(xform, export_type, query, force_xlsx, options,
                            is_project, id, site_id, version, sync_to_gsuit)
    except Export.ExportTypeError:
        return HttpResponseBadRequest(
            _("%s is not a valid export type" % export_type))
    else:
        audit = {"xform": xform.id_string, "export_type": export_type}
        audit_log(
            Actions.EXPORT_CREATED, request.user, owner,
            _("Created %(export_type)s export on '%(id_string)s'.") % {
                'export_type': export_type.upper(),
                'id_string': xform.id_string,
            }, audit, request)
        kwargs = {
            "username": username,
            "id_string": id_string,
            "export_type": export_type,
            "is_project": is_project,
            "id": id,
            "version": version
        }

        kwargs['site_id'] = site_id
        return HttpResponseRedirect(reverse(export_list, kwargs=kwargs))
Beispiel #20
0
def export_list(request, username, id_string, export_type):
    credential = None

    if export_type not in Export.EXPORT_TYPE_DICT:
        return HttpResponseBadRequest(
            _(u'Export type "%s" is not supported.' % export_type))

    if export_type == Export.GOOGLE_SHEETS_EXPORT:
        # Retrieve  google creds or redirect to google authorization page
        credential = _get_google_credential(request)
        if isinstance(credential, HttpResponseRedirect):
            return credential
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if export_type == Export.EXTERNAL_EXPORT:
        # check for template before trying to generate a report
        if not MetaData.external_export(xform):
            return HttpResponseForbidden(_(u'No XLS Template set.'))
    # Get meta and token
    export_token = request.GET.get('token')
    export_meta = request.GET.get('meta')
    options = {
        'group_delimiter': DEFAULT_GROUP_DELIMITER,
        'remove_group_name': False,
        'split_select_multiples': True,
        'binary_select_multiples': False,
        'meta': export_meta,
        'token': export_token,
        'google_credentials': credential
    }

    if should_create_new_export(xform, export_type, options):
        try:
            create_async_export(xform,
                                export_type,
                                query=None,
                                force_xlsx=True,
                                options=options)
        except Export.ExportTypeError:
            return HttpResponseBadRequest(
                _("%s is not a valid export type" % export_type))

    metadata = MetaData.objects.filter(object_id=xform.id,
                                       data_type="external_export")\
        .values('id', 'data_value')

    for m in metadata:
        m['data_value'] = m.get('data_value').split('|')[0]

    data = {
        'username':
        owner.username,
        'xform':
        xform,
        'export_type':
        export_type,
        'export_type_name':
        Export.EXPORT_TYPE_DICT[export_type],
        'exports':
        Export.objects.filter(xform=xform,
                              export_type=export_type).order_by('-created_on'),
        'metas':
        metadata
    }

    return render(request, 'export_list.html', data)
Beispiel #21
0
def export_list(request, username, id_string, export_type):
    """
    Export list view.
    """
    credential = None

    if export_type not in Export.EXPORT_TYPE_DICT:
        return HttpResponseBadRequest(
            _(u'Export type "%s" is not supported.' % export_type))

    if export_type == Export.GOOGLE_SHEETS_EXPORT:
        # Retrieve  google creds or redirect to google authorization page
        credential = _get_google_credential(request)
        if isinstance(credential, HttpResponseRedirect):
            return credential
    owner = get_object_or_404(User, username__iexact=username)
    xform = get_form({'user': owner, 'id_string__iexact': id_string})

    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))

    if export_type == Export.EXTERNAL_EXPORT:
        # check for template before trying to generate a report
        if not MetaData.external_export(xform):
            return HttpResponseForbidden(_(u'No XLS Template set.'))
    # Get meta and token
    export_token = request.GET.get('token')
    export_meta = request.GET.get('meta')
    options = {
        'group_delimiter': DEFAULT_GROUP_DELIMITER,
        'remove_group_name': False,
        'split_select_multiples': True,
        'binary_select_multiples': False,
        'meta': export_meta,
        'token': export_token,
        'google_credentials': credential
    }

    if should_create_new_export(xform, export_type, options):
        try:
            create_async_export(
                xform,
                export_type,
                query=None,
                force_xlsx=True,
                options=options)
        except Export.ExportTypeError:
            return HttpResponseBadRequest(
                _("%s is not a valid export type" % export_type))

    metadata_qs = MetaData.objects.filter(object_id=xform.id,
                                          data_type="external_export")\
        .values('id', 'data_value')

    for metadata in metadata_qs:
        metadata['data_value'] = metadata.get('data_value').split('|')[0]

    data = {
        'username': owner.username,
        'xform': xform,
        'export_type': export_type,
        'export_type_name': Export.EXPORT_TYPE_DICT[export_type],
        'exports': Export.objects.filter(
            xform=xform, export_type=export_type).order_by('-created_on'),
        'metas': metadata_qs
    }  # yapf: disable

    return render(request, 'export_list.html', data)
    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')
        remove_group_name = request.QUERY_PARAMS.get('remove_group_name')

        options = {
            'meta': meta,
            'token': token,
            'data_id': data_id,
            'remove_group_name': remove_group_name
        }

        if job_uuid:
            job = AsyncResult(job_uuid)
            if job.state == 'SUCCESS':
                export_id = job.result
                export = Export.objects.get(id=export_id)
                if export.status == Export.SUCCESSFUL:
                    if export.export_type != Export.EXTERNAL_EXPORT:
                        export_url = reverse(
                            'xform-detail',
                            kwargs={'pk': xform.pk,
                                    'format': export.export_type},
                            request=request
                        )
                    else:
                        export_url = export.export_url
                    resp = {
                        u'job_status': job.state,
                        u'export_url': export_url
                    }
                elif export.status == Export.PENDING:
                    resp = {
                        'export_status': 'Pending'
                    }
                else:
                    resp = {
                        'export_status': "Failed"
                    }
            else:
                resp = {
                    'job_status': job.state
                }

        else:
            export_type = _get_export_type(export_type)

            if export_type in external_export_types and \
                    (token is not None) or (meta is not None):
                export_type = Export.EXTERNAL_EXPORT
            export, async_result = viewer_task.create_async_export(
                xform, export_type, query, False, options=options)
            resp = {
                u'job_uuid': async_result.task_id
            }
        resp_code = status.HTTP_202_ACCEPTED
        return Response(data=resp,
                        status=resp_code,
                        content_type="application/json")