示例#1
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')
    def test_export_instance_deleted_columns_updated(self):
        """This test ensures that when building from a saved export that the new instance correctly labels the
        old columns as advanced
        """
        build_ids_and_versions = {
            self.app_id: 3,
        }
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_built_app_ids_and_versions',
                return_value=build_ids_and_versions):
            instance = FormExportInstance.generate_instance_from_schema(self.schema)

        instance.save()
        self.assertEqual(len(instance.tables), 1)
        self.assertEqual(len(instance.tables[0].columns), 1)
        self.assertTrue(instance.tables[0].columns[0].selected)

        # Every column should now be marked as advanced
        build_ids_and_versions = {
            self.app_id: 4,
        }
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_built_app_ids_and_versions',
                return_value=build_ids_and_versions):

            instance = FormExportInstance.generate_instance_from_schema(
                self.new_schema,
                saved_export=instance
            )

        self.assertEqual(len(instance.tables), 2)
        self.assertEqual(len(instance.tables[0].columns), 2)
        self.assertEqual(len(filter(lambda c: c.is_advanced, instance.tables[0].columns)), 2)
示例#3
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')
示例#4
0
    def test_populated_metadata_document(self):
        odata_config = FormExportInstance(
            _id='my_config_id',
            domain=self.domain.name,
            is_odata_config=True,
            tables=[
                TableConfiguration(
                    columns=[
                        ExportColumn(label='selected_property_1', selected=True),
                        ExportColumn(label='selected_property_2', selected=True),
                        ExportColumn(label='unselected_property'),
                    ],
                ),
            ]
        )
        odata_config.save()
        self.addCleanup(odata_config.delete)

        correct_credentials = self._get_correct_credentials()
        with flag_enabled('ODATA'):
            response = self._execute_query(correct_credentials)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Type'], 'application/xml')
        self.assertEqual(response['OData-Version'], '4.0')
        self.assertXmlEqual(
            self.get_xml(
                'populated_form_odata_metadata_document_from_config',
                override_path=PATH_TO_TEST_DATA
            ),
            response.content
        )
    def test_export_instance_from_saved(self):
        """This test ensures that when we build from a saved export instance that the selection that a user
        makes is still there"""
        build_ids_and_versions = {
            self.app_id: 3,
        }
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_built_app_ids_and_versions',
                return_value=build_ids_and_versions):
            instance = FormExportInstance.generate_instance_from_schema(self.schema)

        instance.save()
        self.assertEqual(len(instance.tables), 1)
        self.assertEqual(len(instance.tables[0].columns), 1)
        self.assertTrue(instance.tables[0].columns[0].selected)

        # Simulate a selection
        instance.tables[0].columns[0].selected = False

        instance.save()
        self.assertFalse(instance.tables[0].columns[0].selected)

        with mock.patch(
                'corehq.apps.export.models.new.get_latest_built_app_ids_and_versions',
                return_value=build_ids_and_versions):

            instance = FormExportInstance.generate_instance_from_schema(
                self.new_schema,
                saved_export=instance
            )

        self.assertEqual(len(instance.tables), 2)
        self.assertEqual(len(instance.tables[0].columns), 2)
        # Selection from previous instance should hold the same and not revert to defaults
        self.assertFalse(instance.tables[0].columns[0].selected)
示例#6
0
    def setUpClass(cls):
        super(TestExportInstanceDBAccessors, cls).setUpClass()
        cls.form_instance_deid = FormExportInstance(domain=cls.domain,
                                                    name='Forms',
                                                    is_deidentified=True)
        cls.form_instance_wrong = FormExportInstance(
            domain='wrong-domain',
            name='Forms',
        )
        cls.form_instance_daily_saved = FormExportInstance(
            domain='wrong-domain',
            is_daily_saved_export=True,
        )
        cls.case_instance_deid = CaseExportInstance(domain=cls.domain,
                                                    name='Cases',
                                                    is_deidentified=True)
        cls.case_instance = CaseExportInstance(domain=cls.domain,
                                               name='Cases',
                                               is_deidentified=False)
        cls.case_instance_daily_saved = CaseExportInstance(
            domain='wrong-domain',
            is_daily_saved_export=True,
        )

        cls.instances = [
            cls.form_instance_deid,
            cls.form_instance_wrong,
            cls.form_instance_daily_saved,
            cls.case_instance,
            cls.case_instance_deid,
            cls.case_instance_daily_saved,
        ]
        for instance in cls.instances:
            instance.save()
 def setUpClass(cls):
     cls.exports = [
         FormExportInstance(is_daily_saved_export=True),
         FormExportInstance(is_daily_saved_export=False),
         CaseExportInstance(is_daily_saved_export=True),
     ]
     for export in cls.exports:
         export.save()
示例#8
0
 def setUp(self):
     self.instance = FormExportInstance(tables=[
         TableConfiguration(path=MAIN_TABLE),
         TableConfiguration(path=[
             PathNode(name='data', is_repeat=False),
             PathNode(name='repeat', is_repeat=True)
         ],
                            columns=[MultiMediaExportColumn(selected=True)])
     ])
 def setUpClass(cls):
     super(TestDailySavedExports, cls).setUpClass()
     cls.exports = [
         FormExportInstance(is_daily_saved_export=True),
         FormExportInstance(is_daily_saved_export=False),
         CaseExportInstance(is_daily_saved_export=True),
     ]
     for export in cls.exports:
         export.save()
示例#10
0
    def setUpClass(cls):
        super(TestExportInstanceDBAccessors, cls).setUpClass()
        cls.form_instance = FormExportInstance(
            domain=cls.domain,
            name='Forms',
            is_deidentified=False
        )
        cls.form_instance_deid = FormExportInstance(
            domain=cls.domain,
            name='Forms',
            is_deidentified=True
        )
        cls.form_instance_wrong = FormExportInstance(
            domain='wrong-domain',
            name='Forms',
        )
        cls.form_instance_daily_saved = FormExportInstance(
            domain='wrong-domain',
            is_daily_saved_export=True,
            auto_rebuild_enabled=True,
            last_accessed=datetime.utcnow()
        )
        cls.case_instance_deid = CaseExportInstance(
            domain=cls.domain,
            name='Cases',
            is_deidentified=True
        )
        cls.case_instance = CaseExportInstance(
            domain=cls.domain,
            name='Cases',
            is_deidentified=False
        )
        cls.case_instance_daily_saved = CaseExportInstance(
            domain='wrong-domain',
            is_daily_saved_export=True,
            auto_rebuild_enabled=True,
            last_accessed=(datetime.utcnow() - timedelta(days=4))
        )

        cls.instances = [
            cls.form_instance,
            cls.form_instance_deid,
            cls.form_instance_wrong,
            cls.form_instance_daily_saved,
            cls.case_instance,
            cls.case_instance_deid,
            cls.case_instance_daily_saved,
        ]
        for instance in cls.instances:
            instance.save()
示例#11
0
    def test_config_in_different_domain(self):
        export_config_in_other_domain = FormExportInstance(
            _id='config_id',
            tables=[TableConfiguration(columns=[])],
            domain='different_domain')
        export_config_in_other_domain.save()
        self.addCleanup(export_config_in_other_domain.delete)

        correct_credentials = self._get_correct_credentials()
        response = self.client.get(
            self._odata_feed_url_by_domain_and_config_id(
                self.domain.name, export_config_in_other_domain.get_id),
            HTTP_AUTHORIZATION='Basic ' + correct_credentials,
        )
        self.assertEqual(response.status_code, 404)
示例#12
0
    def test_ensure_that_column_is_not_deleted(self, _, __):
        """This test ensures that as long as ONE app in last_occurrences is the most recent version then the
        column is still marked as is_deleted=False
        """

        build_ids_and_versions = {
            self.app_id: 3,
            self.second_app_id: 4,
        }
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_app_ids_and_versions',
                return_value=build_ids_and_versions):
            instance = FormExportInstance.generate_instance_from_schema(
                self.schema)

        selected = [
            column for column in instance.tables[0].columns +
            instance.tables[1].columns if column.selected
        ]
        is_advanced = [
            column for column in instance.tables[0].columns +
            instance.tables[1].columns if column.is_advanced
        ]
        selected_system_props = len(
            [x for x in MAIN_FORM_TABLE_PROPERTIES if x.selected])
        advanced_system_props = len(
            [x for x in MAIN_FORM_TABLE_PROPERTIES if x.is_advanced])
        self.assertEqual(len(selected), 1 + selected_system_props)
        self.assertEqual(len(is_advanced), 0 + advanced_system_props)
示例#13
0
    def test_copy_instance(self, _):
        build_ids_and_versions = {
            self.app_id: 3,
        }
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_app_ids_and_versions',
                return_value=build_ids_and_versions):
            instance = FormExportInstance.generate_instance_from_schema(
                self.schema)

        instance.save()
        self.addCleanup(instance.delete)

        new_export = instance.copy_export()
        new_export.save()
        self.assertNotEqual(new_export._id, instance._id)
        self.assertEqual(new_export.name, '{} - Copy'.format(instance.name))
        old_json = instance.to_json()
        del old_json['name']
        del old_json['_id']
        del old_json['_rev']
        new_json = new_export.to_json()
        del new_json['name']
        del new_json['_id']
        del new_json['_rev']
        self.assertDictEqual(old_json, new_json)
示例#14
0
    def _generate_instance(self, build_ids_and_versions, saved_export=None):
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_app_ids_and_versions',
                return_value=build_ids_and_versions):

            return FormExportInstance.generate_instance_from_schema(
                self.schema, saved_export=saved_export)
示例#15
0
    def test_copy_instance(self, _):
        build_ids_and_versions = {
            self.app_id: 3,
        }
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_app_ids_and_versions',
                return_value=build_ids_and_versions):
            instance = FormExportInstance.generate_instance_from_schema(self.schema)

        instance.save()
        self.addCleanup(instance.delete)

        new_export = instance.copy_export()
        new_export.save()
        self.assertNotEqual(new_export._id, instance._id)
        self.assertEqual(new_export.name, '{} - Copy'.format(instance.name))
        old_json = instance.to_json()
        del old_json['name']
        del old_json['_id']
        del old_json['_rev']
        new_json = new_export.to_json()
        del new_json['name']
        del new_json['_id']
        del new_json['_rev']
        self.assertDictEqual(old_json, new_json)
示例#16
0
    def test_simple_table(self, export_save):
        """
        Confirm that some simple documents and a simple FormExportInstance
        are writtern with _write_export_file() correctly
        """

        export_instance = FormExportInstance(
            export_format=Format.JSON,
            tables=[
                TableConfiguration(label="My table",
                                   selected=True,
                                   columns=[
                                       ExportColumn(label="Q3",
                                                    item=ScalarItem(path=[
                                                        PathNode(name='form'),
                                                        PathNode(name='q3')
                                                    ], ),
                                                    selected=True),
                                       ExportColumn(label="Q1",
                                                    item=ScalarItem(path=[
                                                        PathNode(name='form'),
                                                        PathNode(name='q1')
                                                    ], ),
                                                    selected=True),
                                   ])
            ])

        assert_instance_gives_results(
            self.docs, export_instance, {
                'My table': {
                    'headers': ['Q3', 'Q1'],
                    'rows': [['baz', 'foo'], ['bop', 'bip']],
                }
            })
        self.assertTrue(export_save.called)
    def test_ensure_that_column_is_not_deleted(self, _, __):
        """This test ensures that as long as ONE app in last_occurrences is the most recent version then the
        column is still marked as is_deleted=False
        """

        build_ids_and_versions = {
            self.app_id: 3,
            self.second_app_id: 4,
        }
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_built_app_ids_and_versions',
                return_value=build_ids_and_versions):
            instance = FormExportInstance.generate_instance_from_schema(self.schema)

        selected = filter(
            lambda column: column.selected,
            instance.tables[0].columns + instance.tables[1].columns
        )
        is_advanced = filter(
            lambda column: column.is_advanced,
            instance.tables[0].columns + instance.tables[1].columns
        )
        selected_system_props = len([x for x in MAIN_FORM_TABLE_PROPERTIES if x.selected])
        advanced_system_props = len([x for x in MAIN_FORM_TABLE_PROPERTIES if x.is_advanced])
        self.assertEqual(len(selected), 1 + selected_system_props)
        self.assertEqual(len(is_advanced), 0 + advanced_system_props)
示例#18
0
    def test_transform_dates(self, export_save):
        """Ensure dates are transformed for excel when `transform_dates` is set to True"""
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            domain=DOMAIN,
            case_type=DEFAULT_CASE_TYPE,
            transform_dates=True,
            tables=[
                TableConfiguration(label="My table",
                                   selected=True,
                                   path=[],
                                   columns=[
                                       ExportColumn(
                                           label="Date",
                                           item=MultipleChoiceItem(path=[
                                               PathNode(name='form'),
                                               PathNode(name='date')
                                           ], ),
                                           selected=True,
                                       )
                                   ])
            ])

        assert_instance_gives_results(
            self.docs, export_instance, {
                'My table': {
                    'headers': ['Date'],
                    'rows': [[MISSING_VALUE],
                             [
                                 couch_to_excel_datetime(
                                     '2015-07-22T14:16:49.584880Z', None)
                             ]],
                }
            })
        self.assertTrue(export_save.called)
示例#19
0
    def test_empty_table_label(self, export_save):
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            domain=DOMAIN,
            case_type=DEFAULT_CASE_TYPE,
            split_multiselects=True,
            tables=[
                TableConfiguration(label="",
                                   selected=True,
                                   path=[],
                                   columns=[
                                       ExportColumn(label="Q1",
                                                    item=ScalarItem(path=[
                                                        PathNode(name='form'),
                                                        PathNode(name='q1')
                                                    ], ),
                                                    selected=True),
                                   ])
            ])

        assert_instance_gives_results(
            self.docs, export_instance,
            {'Sheet1': {
                'headers': ['Q1'],
                'rows': [['foo'], ['bip']],
            }})
        self.assertTrue(export_save.called)
    def test_generate_instance_from_schema(self, _, __):
        """Only questions that are in the main table and of the same version should be shown"""
        build_ids_and_versions = {self.app_id: 3}
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_built_app_ids_and_versions',
                return_value=build_ids_and_versions):

            instance = FormExportInstance.generate_instance_from_schema(self.schema)

        self.assertEqual(len(instance.tables), 2)

        index, split_column = instance.tables[0].get_column(
            [PathNode(name='data'), PathNode(name='question1')],
            'MultipleChoiceItem',
            None
        )
        self.assertTrue(isinstance(split_column, SplitExportColumn))

        selected = filter(
            lambda column: column.selected,
            instance.tables[0].columns + instance.tables[1].columns
        )
        shown = filter(
            lambda column: column.selected,
            instance.tables[0].columns + instance.tables[1].columns
        )
        selected_system_props = len([x for x in MAIN_FORM_TABLE_PROPERTIES if x.selected])
        self.assertEqual(len(selected), 1 + selected_system_props)
        self.assertEqual(len(shown), 1 + selected_system_props)
示例#21
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)
示例#22
0
    def test_multi_table_order(self):
        tables = [
            TableConfiguration(label="My table {}".format(i),
                               selected=True,
                               path=[],
                               columns=[
                                   ExportColumn(
                                       label="Q{}".format(i),
                                       item=ScalarItem(path=[
                                           PathNode(name='form'),
                                           PathNode(name='q{}'.format(i))
                                       ], ),
                                       selected=True,
                                   ),
                               ]) for i in range(10)
        ]
        export_instance = FormExportInstance(export_format=Format.HTML,
                                             tables=tables)
        writer = _get_writer([export_instance])
        docs = [{
            'domain': 'my-domain',
            '_id': '1234',
            "form": {'q{}'.format(i): 'value {}'.format(i)
                     for i in range(10)}
        }]
        with writer.open([export_instance]):
            _write_export_instance(writer, export_instance, docs)
        with ExportFile(writer.path, writer.format) as export:
            exported_tables = [
                table for table in re.findall('<h2>(.*)</h2>', export.read())
            ]

        expected_tables = [t.label for t in tables]
        self.assertEqual(expected_tables, exported_tables)
示例#23
0
    def test_empty_table_label(self):
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            domain=DOMAIN,
            case_type=DEFAULT_CASE_TYPE,
            split_multiselects=True,
            tables=[
                TableConfiguration(label="",
                                   selected=True,
                                   path=[],
                                   columns=[
                                       ExportColumn(label="Q1",
                                                    item=ScalarItem(path=[
                                                        PathNode(name='form'),
                                                        PathNode(name='q1')
                                                    ], ),
                                                    selected=True),
                                   ])
            ])
        writer = _get_writer([export_instance])
        with writer.open([export_instance]):
            _write_export_instance(writer, export_instance, self.docs)

        with ExportFile(writer.path, writer.format) as export:
            self.assertEqual(json.loads(export.read()), {
                u'Sheet1': {
                    u'headers': [u'Q1'],
                    u'rows': [[u'foo'], [u'bip']],
                }
            })
示例#24
0
 def get_instance(cls, domain_name):
     return FormExportInstance(
         domain=domain_name,
         is_odata_config=True,
         transform_dates=False,
         tables=[
             TableConfiguration(
                 selected=True,
                 columns=[
                     ExportColumn(label='received_on',
                                  selected=True,
                                  item=ExportItem(
                                      path=[PathNode(name='received_on')])),
                     ExportColumn(label='started_time',
                                  selected=True,
                                  item=ExportItem(path=[
                                      PathNode(name='form'),
                                      PathNode(name='meta'),
                                      PathNode(name='timeStart'),
                                  ])),
                     ExportColumn(label='selected_property_1',
                                  selected=True),
                     ExportColumn(label='selected_property_2',
                                  selected=True),
                     ExportColumn(label='unselected_property'),
                 ],
             ),
         ])
示例#25
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
示例#26
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)
示例#27
0
    def test_paginated_table(self, export_save):
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            tables=[
                TableConfiguration(label="My table",
                                   selected=True,
                                   columns=[
                                       ExportColumn(label="Q3",
                                                    item=ScalarItem(path=[
                                                        PathNode(name='form'),
                                                        PathNode(name='q3')
                                                    ], ),
                                                    selected=True),
                                       ExportColumn(label="Q1",
                                                    item=ScalarItem(path=[
                                                        PathNode(name='form'),
                                                        PathNode(name='q1')
                                                    ], ),
                                                    selected=True),
                                   ])
            ])

        assert_instance_gives_results(
            self.docs + self.docs, export_instance, {
                'My table_000': {
                    'headers': ['Q3', 'Q1'],
                    'rows': [['baz', 'foo'], ['bop', 'bip']],
                },
                'My table_001': {
                    'headers': ['Q3', 'Q1'],
                    'rows': [['baz', 'foo'], ['bop', 'bip']],
                }
            })
        self.assertTrue(export_save.called)
    def test_split_questions_false(self):
        """Ensure multiselects are not split when `split_multiselects` is set to False"""
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            domain=DOMAIN,
            case_type=DEFAULT_CASE_TYPE,
            split_multiselects=False,
            tables=[
                TableConfiguration(label="My table",
                                   selected=True,
                                   path=[],
                                   columns=[
                                       SplitExportColumn(
                                           label="MC",
                                           item=MultipleChoiceItem(
                                               path=[
                                                   PathNode(name='form'),
                                                   PathNode(name='mc')
                                               ],
                                               options=[
                                                   Option(value='one'),
                                                   Option(value='two'),
                                               ]),
                                           selected=True,
                                       )
                                   ])
            ])

        assert_instance_gives_results(
            self.docs, export_instance, {
                'My table': {
                    'headers': ['MC'],
                    'rows': [['two extra'], ['one two']],
                }
            })
示例#29
0
    def test_generate_instance_from_schema(self, _, __):
        """Only questions that are in the main table and of the same version should be shown"""
        build_ids_and_versions = {self.app_id: 3}
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_app_ids_and_versions',
                return_value=build_ids_and_versions):

            instance = FormExportInstance.generate_instance_from_schema(
                self.schema)

        self.assertEqual(len(instance.tables), 2)

        index, split_column = instance.tables[0].get_column(
            [PathNode(name='data'),
             PathNode(name='question1')], 'MultipleChoiceItem', None)
        self.assertTrue(isinstance(split_column, SplitExportColumn))

        index, stock_column = instance.tables[0].get_column([
            PathNode(name='data'),
            PathNode(name='balance:question-id'),
            PathNode(name='@type')
        ], 'StockItem', None)
        self.assertTrue(isinstance(stock_column, StockFormExportColumn))

        selected = filter(
            lambda column: column.selected,
            instance.tables[0].columns + instance.tables[1].columns)
        selected_system_props = len(
            [x for x in MAIN_FORM_TABLE_PROPERTIES if x.selected])
        self.assertEqual(len(selected), 2 + selected_system_props)
示例#30
0
 def setUp(cls):
     cls.new_exports = [
         FormExportInstance(),
         CaseExportInstance(),
     ]
     for export in cls.new_exports:
         export.save()
示例#31
0
    def test_user_permissions(self):
        self.web_user.set_role(self.domain.name, 'none')
        self.web_user.save()
        self.addCleanup(self._setup_user_permissions)

        export_config = FormExportInstance(
            _id='config_id',
            tables=[TableConfiguration(columns=[])],
            domain=self.domain.name,
        )
        export_config.save()
        self.addCleanup(export_config.delete)

        correct_credentials = self._get_correct_credentials()
        with flag_enabled('ODATA'):
            response = self._execute_query(correct_credentials)
        self.assertEqual(response.status_code, 403)
示例#32
0
 def _update_instance(self, build_ids_and_versions, instance):
     with mock.patch(
             'corehq.apps.export.models.new.get_latest_app_ids_and_versions',
             return_value=build_ids_and_versions):
         instance = FormExportInstance.generate_instance_from_schema(
             self.new_schema,
             saved_export=instance
         )
     return instance
    def test_export_instance_from_saved(self, _):
        """This test ensures that when we build from a saved export instance that the selection that a user
        makes is still there"""
        first_non_system_property = len(TOP_MAIN_FORM_TABLE_PROPERTIES)
        build_ids_and_versions = {
            self.app_id: 3,
        }
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_app_ids_and_versions',
                return_value=build_ids_and_versions):
            instance = FormExportInstance.generate_instance_from_schema(
                self.schema)

        instance.save()
        self.assertEqual(len(instance.tables), 1)
        self.assertEqual(len(instance.tables[0].columns),
                         1 + len(MAIN_FORM_TABLE_PROPERTIES))
        self.assertTrue(
            instance.tables[0].columns[first_non_system_property].selected)
        item = instance.tables[0].columns[first_non_system_property].item

        # Simulate a selection
        instance.tables[0].columns[first_non_system_property].selected = False

        instance.save()
        self.assertFalse(
            instance.tables[0].columns[first_non_system_property].selected)

        with mock.patch(
                'corehq.apps.export.models.new.get_latest_app_ids_and_versions',
                return_value=build_ids_and_versions):

            instance = FormExportInstance.generate_instance_from_schema(
                self.new_schema, saved_export=instance)

        self.assertEqual(len(instance.tables), 2)
        self.assertEqual(len(instance.tables[0].columns),
                         2 + len(MAIN_FORM_TABLE_PROPERTIES))

        # Selection from previous instance should hold the same and not revert to defaults
        idx, column = instance.tables[0].get_column(item.path, item.doc_type,
                                                    item.transform)
        self.assertFalse(column.selected)
示例#34
0
class TestExportInstance(SimpleTestCase):

    def setUp(self):
        self.instance = FormExportInstance(
            tables=[
                TableConfiguration(
                    path=MAIN_TABLE
                ),
                TableConfiguration(
                    path=[PathNode(name='data', is_repeat=False), PathNode(name='repeat', is_repeat=True)],
                    columns=[
                        MultiMediaExportColumn(
                            selected=True
                        )
                    ]

                )
            ]
        )

    def test_get_table(self):
        table = self.instance.get_table(MAIN_TABLE)
        self.assertEqual(table.path, MAIN_TABLE)

        table = self.instance.get_table([
            PathNode(name='data', is_repeat=False), PathNode(name='repeat', is_repeat=True)
        ])
        self.assertEqual(
            table.path,
            [PathNode(name='data', is_repeat=False), PathNode(name='repeat', is_repeat=True)]
        )

        table = self.instance.get_table([
            PathNode(name='data', is_repeat=False), PathNode(name='DoesNotExist', is_repeat=False)
        ])
        self.assertIsNone(table)

    def test_has_multimedia(self):
        self.assertTrue(self.instance.has_multimedia)

    def test_has_multimedia_not_selected(self):
        self.instance.tables[1].columns[0].selected = False
        self.assertFalse(self.instance.has_multimedia)
示例#35
0
class TestExportInstance(SimpleTestCase):

    def setUp(self):
        self.instance = FormExportInstance(
            tables=[
                TableConfiguration(
                    path=MAIN_TABLE
                ),
                TableConfiguration(
                    path=[PathNode(name='data', is_repeat=False), PathNode(name='repeat', is_repeat=True)],
                    columns=[
                        MultiMediaExportColumn(
                            selected=True
                        )
                    ]

                )
            ]
        )

    def test_get_table(self):
        table = self.instance.get_table(MAIN_TABLE)
        self.assertEqual(table.path, MAIN_TABLE)

        table = self.instance.get_table([
            PathNode(name='data', is_repeat=False), PathNode(name='repeat', is_repeat=True)
        ])
        self.assertEqual(
            table.path,
            [PathNode(name='data', is_repeat=False), PathNode(name='repeat', is_repeat=True)]
        )

        table = self.instance.get_table([
            PathNode(name='data', is_repeat=False), PathNode(name='DoesNotExist', is_repeat=False)
        ])
        self.assertIsNone(table)

    def test_has_multimedia(self):
        self.assertTrue(self.instance.has_multimedia)

    def test_has_multimedia_not_selected(self):
        self.instance.tables[1].columns[0].selected = False
        self.assertFalse(self.instance.has_multimedia)
示例#36
0
 def _get_instance(self, build_ids_and_versions):
     with mock.patch(
             'corehq.apps.export.models.new.get_latest_app_ids_and_versions',
             return_value=build_ids_and_versions):
         instance = FormExportInstance.generate_instance_from_schema(self.schema)
     instance.save()
     self.addCleanup(instance.delete)
     self.assertEqual(len(instance.tables), 1)
     self.assertEqual(len(instance.tables[0].columns), 3 + len(MAIN_FORM_TABLE_PROPERTIES))
     self.assertTrue(instance.tables[0].columns[len(TOP_MAIN_FORM_TABLE_PROPERTIES)].selected)
     return instance
示例#37
0
 def _get_instance(self, build_ids_and_versions):
     with mock.patch(
             'corehq.apps.export.models.new.get_latest_app_ids_and_versions',
             return_value=build_ids_and_versions):
         instance = FormExportInstance.generate_instance_from_schema(self.schema)
     instance.save()
     self.addCleanup(instance.delete)
     self.assertEqual(len(instance.tables), 1)
     self.assertEqual(len(instance.tables[0].columns), 3 + len(MAIN_FORM_TABLE_PROPERTIES))
     self.assertTrue(instance.tables[0].columns[len(TOP_MAIN_FORM_TABLE_PROPERTIES)].selected)
     return instance
示例#38
0
    def test_multi_table(self):
        export_instance = FormExportInstance(
            export_format=Format.JSON,
            tables=[
                TableConfiguration(
                    label="My table",
                    selected=True,
                    path=[],
                    columns=[
                        ExportColumn(
                            label="Q3",
                            item=ScalarItem(
                                path=[PathNode(name='form'), PathNode(name='q3')],
                            ),
                            selected=True,
                        ),
                    ]
                ),
                TableConfiguration(
                    label="My other table",
                    selected=True,
                    path=[PathNode(name='form', is_repeat=False), PathNode(name="q2", is_repeat=False)],
                    columns=[
                        ExportColumn(
                            label="Q4",
                            item=ScalarItem(
                                path=[PathNode(name='form'), PathNode(name='q2'), PathNode(name='q4')],
                            ),
                            selected=True,
                        ),
                    ]
                )
            ]
        )
        writer = _get_writer([export_instance])
        with writer.open([export_instance]):
            _write_export_instance(writer, export_instance, self.docs)
        with ExportFile(writer.path, writer.format) as export:
            self.assertEqual(
                json.loads(export.read()),
                {
                    u'My table': {
                        u'headers': [u'Q3'],
                        u'rows': [[u'baz'], [u'bop']],

                    },
                    u'My other table': {
                        u'headers': [u'Q4'],
                        u'rows': [[u'bar'], [u'boop']],
                    }
                }
            )
示例#39
0
    def test_request_succeeded(self):
        export_config = FormExportInstance(
            _id='config_id',
            tables=[TableConfiguration(columns=[])],
            domain=self.domain.name,
            xmlns='my_xmlns',
        )
        export_config.save()
        self.addCleanup(export_config.delete)

        correct_credentials = self._get_correct_credentials()
        response = self._execute_query(correct_credentials)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['Content-Type'],
                         'application/json; charset=utf-8')
        self.assertEqual(response['OData-Version'], '4.0')
        self.assertEqual(
            json.loads(response.content.decode('utf-8')), {
                '@odata.context':
                'http://localhost:8000/a/test_domain/api/v0.5/odata/forms/config_id/$metadata#feed',
                'value': []
            })
示例#40
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
示例#41
0
    def test_ensure_that_column_is_deleted(self, _, __):
        """If both apps are out of date then, the question is indeed deleted"""
        build_ids_and_versions = {
            self.app_id: 3,
            self.second_app_id: 5,
        }
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_app_ids_and_versions',
                return_value=build_ids_and_versions):
            instance = FormExportInstance.generate_instance_from_schema(self.schema)

        selected = [column for column in instance.tables[0].columns + instance.tables[1].columns if column.selected]
        shown = [column for column in instance.tables[0].columns + instance.tables[1].columns if column.selected]
        selected_system_props = len([x for x in MAIN_FORM_TABLE_PROPERTIES if x.selected])
        self.assertEqual(len(selected), 0 + selected_system_props)
        self.assertEqual(len(shown), 0 + selected_system_props)
示例#42
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)
示例#43
0
    def setUp(self):
        self.instance = FormExportInstance(
            tables=[
                TableConfiguration(
                    path=MAIN_TABLE
                ),
                TableConfiguration(
                    path=[PathNode(name='data', is_repeat=False), PathNode(name='repeat', is_repeat=True)],
                    columns=[
                        MultiMediaExportColumn(
                            selected=True
                        )
                    ]

                )
            ]
        )
    def test_generate_instance_from_schema_deleted(self, _):
        """Given a higher app_version, all the old questions should not be shown or selected"""
        build_ids_and_versions = {self.app_id: 4}
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_built_app_ids_and_versions',
                return_value=build_ids_and_versions):
            instance = FormExportInstance.generate_instance_from_schema(self.schema)

        self.assertEqual(len(instance.tables), 2)

        selected = filter(
            lambda column: column.selected,
            instance.tables[0].columns + instance.tables[1].columns
        )
        shown = filter(
            lambda column: column.selected,
            instance.tables[0].columns + instance.tables[1].columns
        )
        self.assertEqual(len(selected), 0)
        self.assertEqual(len(shown), 0)
示例#45
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,
    })
    def test_ensure_that_column_is_deleted(self, _):
        """If both apps are out of date then, the question is indeed deleted"""
        build_ids_and_versions = {
            self.app_id: 3,
            self.second_app_id: 5,
        }
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_built_app_ids_and_versions',
                return_value=build_ids_and_versions):
            instance = FormExportInstance.generate_instance_from_schema(self.schema)

        selected = filter(
            lambda column: column.selected,
            instance.tables[0].columns + instance.tables[1].columns
        )
        shown = filter(
            lambda column: column.selected,
            instance.tables[0].columns + instance.tables[1].columns
        )
        self.assertEqual(len(selected), 0)
        self.assertEqual(len(shown), 0)
    def test_generate_instance_from_schema(self, _):
        """Only questions that are in the main table and of the same version should be shown"""
        build_ids_and_versions = {self.app_id: 3}
        with mock.patch(
                'corehq.apps.export.models.new.get_latest_built_app_ids_and_versions',
                return_value=build_ids_and_versions):

            instance = FormExportInstance.generate_instance_from_schema(self.schema)

        self.assertEqual(len(instance.tables), 2)

        selected = filter(
            lambda column: column.selected,
            instance.tables[0].columns + instance.tables[1].columns
        )
        shown = filter(
            lambda column: column.selected,
            instance.tables[0].columns + instance.tables[1].columns
        )
        self.assertEqual(len(selected), 1)
        self.assertEqual(len(shown), 1)
示例#48
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
示例#49
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
示例#50
0
 def test_default_form_values(self):
     # Confirm that FormExportInstances set the default user_types filter correctly
     form_export = FormExportInstance()
     form_export_wrapped = FormExportInstance.wrap({})
     for e in [form_export, form_export_wrapped]:
         self.assertListEqual(e.filters.user_types, [0, 5])
示例#51
0
 def _get_export(self, domain, export_id):
     return FormExportInstance.get(self.export_id)