Esempio n. 1
0
    def test_kml_exports(self):
        """
        Test generate_kml_export()
        """
        export_type = "kml"
        options = {
            "group_delimiter": "/",
            "remove_group_name": False,
            "split_select_multiples": True,
            "extension": 'kml'
        }

        self._publish_transportation_form_and_submit_instance()
        username = self.xform.user.username
        id_string = self.xform.id_string

        export = generate_kml_export(export_type,
                                     username,
                                     id_string,
                                     options=options)
        self.assertIsNotNone(export)
        self.assertTrue(export.is_successful)

        export_id = export.id

        export.delete()

        export = generate_kml_export(export_type,
                                     username,
                                     id_string,
                                     export_id=export_id,
                                     options=options)

        self.assertIsNotNone(export)
        self.assertTrue(export.is_successful)
Esempio n. 2
0
    def test_kml_exports(self):
        """
        Test generate_kml_export()
        """
        export_type = "kml"
        options = {
            "group_delimiter": "/",
            "remove_group_name": False,
            "split_select_multiples": True,
            "extension": 'kml'
        }

        self._publish_transportation_form_and_submit_instance()
        username = self.xform.user.username
        id_string = self.xform.id_string

        export = generate_kml_export(
            export_type, username, id_string, options=options)
        self.assertIsNotNone(export)
        self.assertTrue(export.is_successful)

        export_id = export.id

        export.delete()

        export = generate_kml_export(
            export_type,
            username,
            id_string,
            export_id=export_id,
            options=options)

        self.assertIsNotNone(export)
        self.assertTrue(export.is_successful)
Esempio n. 3
0
def create_kml_export(username, id_string, export_id, query=None):
    # we re-query the db instead of passing model objects according to
    # http://docs.celeryproject.org/en/latest/userguide/tasks.html#state

    export = Export.objects.get(id=export_id)
    try:
        # though export is not available when for has 0 submissions, we
        # catch this since it potentially stops celery
        gen_export = generate_kml_export(
            Export.KML_EXPORT, 'kml', username, id_string, export_id, query)
    except (Exception, NoRecordsFoundError) as e:
        export.internal_status = Export.FAILED
        export.save()
        # mail admins
        details = {
            'export_id': export_id,
            'username': username,
            'id_string': id_string
        }
        report_exception("KML Export Exception: Export ID - "
                         "%(export_id)s, /%(username)s/%(id_string)s"
                         % details, e, sys.exc_info())
        raise
    else:
        return gen_export.id
Esempio n. 4
0
def _generate_new_export(request, xform, query, export_type):
    query = _set_start_end_params(request, query)
    extension = _get_extension_from_export_type(export_type)

    try:
        if export_type == Export.EXTERNAL_EXPORT:
            export = generate_external_export(export_type, xform.user.username,
                                              xform.id_string, None,
                                              request.GET.get('token'), query,
                                              request.GET.get('meta'),
                                              request.GET.get('data_id'))
        elif export_type == Export.OSM_EXPORT:
            export = generate_osm_export(export_type,
                                         extension,
                                         xform.user.username,
                                         xform.id_string,
                                         export_id=None,
                                         filter_query=None)
        elif export_type == Export.KML_EXPORT:
            export = generate_kml_export(export_type,
                                         extension,
                                         xform.user.username,
                                         xform.id_string,
                                         export_id=None,
                                         filter_query=None)
        else:
            remove_group_name = False

            if "remove_group_name" in request.QUERY_PARAMS:
                remove_group_name = \
                    str_to_bool(request.QUERY_PARAMS["remove_group_name"])

            export = generate_export(export_type,
                                     extension,
                                     xform.user.username,
                                     xform.id_string,
                                     None,
                                     query,
                                     remove_group_name=remove_group_name)
        audit = {"xform": xform.id_string, "export_type": export_type}
        log.audit_log(
            log.Actions.EXPORT_CREATED, request.user, xform.user,
            _("Created %(export_type)s export on '%(id_string)s'.") % {
                'id_string': xform.id_string,
                'export_type': export_type.upper()
            }, audit, request)
    except NoRecordsFoundError:
        raise Http404(_("No records found to export"))
    except J2XException as e:
        # j2x exception
        return {'error': str(e)}
    else:
        return export
Esempio n. 5
0
def _generate_new_export(request, xform, query, export_type):
    query = _set_start_end_params(request, query)
    extension = _get_extension_from_export_type(export_type)

    try:
        if export_type == Export.EXTERNAL_EXPORT:
            export = generate_external_export(
                export_type, xform.user.username,
                xform.id_string, None, request.GET.get('token'), query,
                request.GET.get('meta'), request.GET.get('data_id')
            )
        elif export_type == Export.OSM_EXPORT:
            export = generate_osm_export(
                export_type, extension, xform.user.username,
                xform.id_string, export_id=None, filter_query=None)
        elif export_type == Export.KML_EXPORT:
            export = generate_kml_export(
                export_type, extension, xform.user.username,
                xform.id_string, export_id=None, filter_query=None)
        else:
            remove_group_name = False

            if "remove_group_name" in request.QUERY_PARAMS:
                remove_group_name = \
                    str_to_bool(request.QUERY_PARAMS["remove_group_name"])

            export = generate_export(
                export_type, extension, xform.user.username,
                xform.id_string, None, query,
                remove_group_name=remove_group_name
            )
        audit = {
            "xform": xform.id_string,
            "export_type": export_type
        }
        log.audit_log(
            log.Actions.EXPORT_CREATED, request.user, xform.user,
            _("Created %(export_type)s export on '%(id_string)s'.") %
            {
                'id_string': xform.id_string,
                'export_type': export_type.upper()
            }, audit, request)
    except NoRecordsFoundError:
        raise Http404(_("No records found to export"))
    except J2XException as e:
        # j2x exception
        return {'error': str(e)}
    else:
        return export
Esempio n. 6
0
def _generate_new_export(request,
                         xform,
                         query,
                         export_type,
                         dataview_pk=False):
    query = _set_start_end_params(request, query)
    extension = _get_extension_from_export_type(export_type)

    options = {
        "extension": extension,
        "username": xform.user.username,
        "id_string": xform.id_string,
    }
    if query:
        options['query'] = query

    options["dataview_pk"] = dataview_pk
    if export_type == Export.GOOGLE_SHEETS_EXPORT:
        options['google_credentials'] = \
            _get_google_credential(request).to_json()

    try:
        if export_type == Export.EXTERNAL_EXPORT:
            options['token'] = request.GET.get('token')
            options['data_id'] = request.GET.get('data_id')
            options['meta'] = request.GET.get('meta')

            export = generate_external_export(export_type,
                                              xform.user.username,
                                              xform.id_string,
                                              None,
                                              options,
                                              xform=xform)
        elif export_type == Export.OSM_EXPORT:
            export = generate_osm_export(export_type,
                                         xform.user.username,
                                         xform.id_string,
                                         None,
                                         options,
                                         xform=xform)
        elif export_type == Export.ZIP_EXPORT:
            export = generate_attachments_zip_export(export_type,
                                                     xform.user.username,
                                                     xform.id_string,
                                                     None,
                                                     options,
                                                     xform=xform)
        elif export_type == Export.KML_EXPORT:
            export = generate_kml_export(export_type,
                                         xform.user.username,
                                         xform.id_string,
                                         None,
                                         options,
                                         xform=xform)
        else:
            options.update(parse_request_export_options(request.query_params))

            export = generate_export(export_type, xform, None, options)

        audit = {"xform": xform.id_string, "export_type": export_type}
        log.audit_log(
            log.Actions.EXPORT_CREATED, request.user, xform.user,
            _("Created %(export_type)s export on '%(id_string)s'.") % {
                'id_string': xform.id_string,
                'export_type': export_type.upper()
            }, audit, request)
    except NoRecordsFoundError:
        raise Http404(_("No records found to export"))
    except J2XException as e:
        # j2x exception
        return async_status(FAILED, str(e))
    except SPSSIOError as e:
        raise exceptions.ParseError(str(e))
    else:
        return export