Esempio n. 1
0
def _get_export_properties(export_id, export_is_legacy):
    """
    Return a list of strings corresponding to form questions that are
    included in the export.
    """
    properties = set()
    if export_id:
        if export_is_legacy:
            schema = FormExportSchema.get(export_id)
            for table in schema.tables:
                # - in question id is replaced by . in excel exports
                properties |= {
                    c.display.replace('.', '-')
                    for c in table.columns if c.display
                }
        else:
            from corehq.apps.export.models import FormExportInstance
            export = FormExportInstance.get(export_id)
            for table in export.tables:
                for column in table.columns:
                    if column.item:
                        path_parts = [n.name for n in column.item.path]
                        path_parts = path_parts[1:] if path_parts[
                            0] == "form" else path_parts
                        properties.add("-".join(path_parts))
    return properties
Esempio n. 2
0
def build_form_multimedia_zip(
        domain,
        export_id,
        datespan,
        user_types,
        download_id,
        owner_id,
):
    from corehq.apps.export.models import FormExportInstance
    export = FormExportInstance.get(export_id)
    form_ids = get_form_ids_having_multimedia(
        domain, export.app_id, export.xmlns, datespan, user_types
    )
    forms_info = _get_form_attachment_info(domain, form_ids, export)

    num_forms = len(forms_info)
    DownloadBase.set_progress(build_form_multimedia_zip, 0, num_forms)

    all_case_ids = set.union(*(info['case_ids'] for info in forms_info)) if forms_info else set()
    case_id_to_name = _get_case_names(domain, all_case_ids)

    with TransientTempfile() as temp_path:
        with open(temp_path, 'wb') as f:
            _write_attachments_to_file(temp_path, num_forms, forms_info, case_id_to_name)
        with open(temp_path, 'rb') as f:
            zip_name = 'multimedia-{}'.format(unidecode(export.name))
            _save_and_expose_zip(f, zip_name, domain, download_id, owner_id)

    DownloadBase.set_progress(build_form_multimedia_zip, num_forms, num_forms)
Esempio n. 3
0
 def test_formid_column_label(self):
     export_with_modified_formid_column = FormExportInstance(
         is_odata_config=True,
         tables=[
             TableConfiguration(
                 selected=True,
                 columns=[
                     ExportColumn(
                         label='modified_form_id_column',
                         item=ExportItem(
                             path=[
                                 PathNode(name='form'),
                                 PathNode(name='meta'),
                                 PathNode(name='instanceID')
                             ]
                         ),
                         selected=True,
                     )
                 ]
             )
         ]
     )
     export_with_modified_formid_column.save()
     self.addCleanup(export_with_modified_formid_column.delete)
     cleaned_export = FormExportInstance.get(export_with_modified_formid_column.get_id)
     self.assertEqual(cleaned_export.tables[0].columns[0].label, 'formid')
Esempio n. 4
0
 def test_ignore_form_link_label(self):
     export_with_form_link = FormExportInstance(
         is_odata_config=True,
         tables=[
             TableConfiguration(
                 selected=True,
                 columns=[
                     ExportColumn(
                         label='my_form_link',
                         item=ExportItem(
                             path=[
                                 PathNode(name='form'),
                                 PathNode(name='meta'),
                                 PathNode(name='instanceID')
                             ],
                             transform=FORM_ID_TO_LINK,
                         ),
                         selected=True,
                     )
                 ]
             )
         ]
     )
     export_with_form_link.save()
     self.addCleanup(export_with_form_link.delete)
     cleaned_export = FormExportInstance.get(export_with_form_link.get_id)
     self.assertEqual(cleaned_export.tables[0].columns[0].label, 'my_form_link')
Esempio n. 5
0
def get_export(export_type, domain, export_id=None, username=None):
    from corehq.apps.export.models import (
        FormExportInstance,
        CaseExportInstance,
        SMSExportInstance,
        SMSExportDataSchema
    )
    if export_type == 'form':
        try:
            return FormExportInstance.get(export_id)
        except ResourceNotFound:
            raise Http404()
    elif export_type == 'case':
        try:
            return CaseExportInstance.get(export_id)
        except ResourceNotFound:
            raise Http404()
    elif export_type == 'sms':
        if not username:
            raise Exception("Username needed to ensure permissions")
        include_metadata = MESSAGE_LOG_METADATA.enabled(username)
        return SMSExportInstance._new_from_schema(
            SMSExportDataSchema.get_latest_export_schema(domain, include_metadata)
        )
    raise Exception("Unexpected export type received %s" % export_type)
Esempio n. 6
0
def has_multimedia(request, domain):
    """Checks to see if this form export has multimedia available to export
    """
    form_or_case = request.GET.get('form_or_case')
    if form_or_case != 'form':
        raise ValueError("has_multimedia is only available for form exports")
    permissions = ExportsPermissionsManager(form_or_case, domain, request.couch_user)
    permissions.access_download_export_or_404()
    export_object = FormExportInstance.get(request.GET.get('export_id'))
    return json_response({
        'success': True,
        'hasMultimedia': export_object.has_multimedia,
    })
Esempio n. 7
0
def build_form_multimedia_zip(
        domain,
        export_id,
        datespan,
        user_types,
        download_id,
        owner_id,
):
    from corehq.apps.export.models import FormExportInstance
    export = FormExportInstance.get(export_id)
    form_ids = get_form_ids_having_multimedia(
        domain, export.app_id, export.xmlns, datespan, user_types
    )
    _generate_form_multimedia_zipfile(domain, export, form_ids, download_id, owner_id, build_form_multimedia_zip)
Esempio n. 8
0
def build_form_multimedia_zipfile(
        domain,
        export_id,
        es_filters,
        download_id,
        owner_id,
):
    from corehq.apps.export.models import FormExportInstance
    from corehq.apps.export.export import get_export_query
    export = FormExportInstance.get(export_id)
    es_query = get_export_query(export, es_filters)
    form_ids = get_form_ids_with_multimedia(es_query)
    _generate_form_multimedia_zipfile(domain, export, form_ids, download_id, owner_id,
                                      build_form_multimedia_zipfile)
Esempio n. 9
0
def _get_export_properties(export_id):
    """
    Return a list of strings corresponding to form questions that are
    included in the export.
    """
    properties = set()
    if export_id:
        from corehq.apps.export.models import FormExportInstance
        export = FormExportInstance.get(export_id)
        for table in export.tables:
            for column in table.columns:
                if column.selected and column.item:
                    path_parts = [n.name for n in column.item.path]
                    path_parts = path_parts[1:] if path_parts[0] == "form" else path_parts
                    properties.add("-".join(path_parts))
    return properties
Esempio n. 10
0
def _get_export_properties(export_id):
    """
    Return a list of strings corresponding to form questions that are
    included in the export.
    """
    properties = set()
    if export_id:
        from corehq.apps.export.models import FormExportInstance
        export = FormExportInstance.get(export_id)
        for table in export.tables:
            for column in table.columns:
                if column.selected and column.item:
                    path_parts = [n.name for n in column.item.path]
                    path_parts = path_parts[1:] if path_parts[
                        0] == "form" else path_parts
                    properties.add("-".join(path_parts))
    return properties
Esempio n. 11
0
    def get(self, request, *args, **kwargs):
        try:
            export_instance = FormExportInstance.get(self.export_id)
        except ResourceNotFound:
            # If it's not found, try and see if it's on the legacy system before throwing a 404
            try:
                export_helper = make_custom_export_helper(self.request, self.export_type, self.domain, self.export_id)

                export_instance = convert_saved_export_to_export_instance(export_helper.custom_export)
            except ResourceNotFound:
                raise Http404()

        schema = self.get_export_schema(export_instance)
        self.export_instance = self.export_instance_cls.generate_instance_from_schema(
            schema, saved_export=export_instance
        )
        return super(BaseEditNewCustomExportView, self).get(request, *args, **kwargs)
Esempio n. 12
0
def has_multimedia(request, domain):
    """Checks to see if this form export has multimedia available to export
    """
    form_or_case = request.GET.get('form_or_case')
    if form_or_case != 'form':
        raise ValueError("has_multimedia is only available for form exports")
    permissions = ExportsPermissionsManager(form_or_case, domain,
                                            request.couch_user)
    permissions.access_download_export_or_404()
    export_object = FormExportInstance.get(request.GET.get('export_id'))
    if isinstance(export_object, ExportInstance):
        has_multimedia = export_object.has_multimedia
    else:
        has_multimedia = forms_have_multimedia(
            domain, export_object.app_id, getattr(export_object, 'xmlns', ''))
    return JsonResponse({
        'success': True,
        'hasMultimedia': has_multimedia,
    })
Esempio n. 13
0
def has_multimedia(request, domain):
    """Checks to see if this form export has multimedia available to export
    """
    form_or_case = request.GET.get('form_or_case')
    if form_or_case != 'form':
        raise ValueError("has_multimedia is only available for form exports")
    permissions = ExportsPermissionsManager(form_or_case, domain, request.couch_user)
    permissions.access_download_export_or_404()
    export_object = FormExportInstance.get(request.GET.get('export_id'))
    if isinstance(export_object, ExportInstance):
        has_multimedia = export_object.has_multimedia
    else:
        has_multimedia = forms_have_multimedia(
            domain,
            export_object.app_id,
            getattr(export_object, 'xmlns', '')
        )
    return json_response({
        'success': True,
        'hasMultimedia': has_multimedia,
    })
Esempio n. 14
0
def _get_export_properties(export_id, export_is_legacy):
    """
    Return a list of strings corresponding to form questions that are
    included in the export.
    """
    properties = set()
    if export_id:
        if export_is_legacy:
            schema = FormExportSchema.get(export_id)
            for table in schema.tables:
                properties |= _convert_legacy_indices_to_export_properties(
                    map(lambda column: column.index, table.columns))
        else:
            from corehq.apps.export.models import FormExportInstance
            export = FormExportInstance.get(export_id)
            for table in export.tables:
                for column in table.columns:
                    if column.selected and column.item:
                        path_parts = [n.name for n in column.item.path]
                        path_parts = path_parts[1:] if path_parts[
                            0] == "form" else path_parts
                        properties.add("-".join(path_parts))
    return properties
Esempio n. 15
0
def _get_export_properties(export_id, export_is_legacy):
    """
    Return a list of strings corresponding to form questions that are
    included in the export.
    """
    properties = set()
    if export_id:
        if export_is_legacy:
            schema = FormExportSchema.get(export_id)
            for table in schema.tables:
                properties |= _convert_legacy_indices_to_export_properties(
                    map(lambda column: column.index, table.columns)
                )
        else:
            from corehq.apps.export.models import FormExportInstance
            export = FormExportInstance.get(export_id)
            for table in export.tables:
                for column in table.columns:
                    if column.item:
                        path_parts = [n.name for n in column.item.path]
                        path_parts = path_parts[1:] if path_parts[0] == "form" else path_parts
                        properties.add("-".join(path_parts))
    return properties
Esempio n. 16
0
def get_export(export_type, domain, export_id=None, username=None):
    from corehq.apps.export.models import (FormExportInstance,
                                           CaseExportInstance,
                                           SMSExportInstance,
                                           SMSExportDataSchema)
    if export_type == 'form':
        try:
            return FormExportInstance.get(export_id)
        except ResourceNotFound:
            raise Http404()
    elif export_type == 'case':
        try:
            return CaseExportInstance.get(export_id)
        except ResourceNotFound:
            raise Http404()
    elif export_type == 'sms':
        if not username:
            raise Exception("Username needed to ensure permissions")
        include_metadata = MESSAGE_LOG_METADATA.enabled(username)
        return SMSExportInstance._new_from_schema(
            SMSExportDataSchema.get_latest_export_schema(
                domain, include_metadata))
    raise Exception("Unexpected export type received %s" % export_type)
Esempio n. 17
0
def _get_export_properties(export_id, export_is_legacy):
    """
    Return a list of strings corresponding to form questions that are
    included in the export.
    """
    properties = set()
    if export_id:
        if export_is_legacy:
            schema = FormExportSchema.get(export_id)
            for table in schema.tables:
                # - in question id is replaced by . in excel exports
                properties |= {c.display.replace('.', '-') for c in
                               table.columns}
        else:
            from corehq.apps.export.models import FormExportInstance
            export = FormExportInstance.get(export_id)
            for table in export.tables:
                for column in table.columns:
                    if column.item:
                        path_parts = [n.name for n in column.item.path]
                        path_parts = path_parts[1:] if path_parts[0] == "form" else path_parts
                        properties.add("-".join(path_parts))
    return properties
Esempio n. 18
0
    def to_json(self, data, options=None):
        # Convert bundled objects to JSON
        data['objects'] = [
            bundle.obj for bundle in data['objects']
        ]

        domain = data.pop('domain', None)
        config_id = data.pop('config_id', None)
        api_path = data.pop('api_path', None)
        assert all([domain, config_id, api_path]), [domain, config_id, api_path]

        data['@odata.context'] = '{}#{}'.format(
            absolute_reverse(ODataFormMetadataView.urlname, args=[domain, config_id]),
            'feed'
        )

        next_link = self.get_next_url(data.pop('meta'), api_path)
        if next_link:
            data['@odata.nextLink'] = next_link

        config = FormExportInstance.get(config_id)
        data['value'] = self.serialize_forms_using_config(data.pop('objects'), config)

        return json.dumps(data, cls=DjangoJSONEncoder, sort_keys=True)
Esempio n. 19
0
 def get_export(self, id=None):
     try:
         return FormExportInstance.get(id)
     except ResourceNotFound:
         raise Http404()
Esempio n. 20
0
 def _get_export(self, domain, export_id):
     return FormExportInstance.get(self.export_id)
Esempio n. 21
0
 def get_config(self, config_id):
     return FormExportInstance.get(config_id)