def get_static_report_mapping(from_domain, to_domain): from corehq.apps.userreports.models import StaticReportConfiguration, STATIC_PREFIX, \ CUSTOM_REPORT_PREFIX report_map = {} for static_report in StaticReportConfiguration.by_domain(from_domain): if static_report.get_id.startswith(STATIC_PREFIX): report_id = static_report.get_id.replace( STATIC_PREFIX + from_domain + '-', '') is_custom_report = False else: report_id = static_report.get_id.replace( CUSTOM_REPORT_PREFIX + from_domain + '-', '') is_custom_report = True new_id = StaticReportConfiguration.get_doc_id(to_domain, report_id, is_custom_report) # check that new report is in new domain's list of static reports try: StaticReportConfiguration.by_id(new_id, to_domain) except (BadSpecError, DocumentNotFound): pass else: report_map[static_report.get_id] = new_id return report_map
def copy_ucr_reports(self, datasource_map): report_map = {} reports = get_report_configs_for_domain(self.existing_domain) for report in reports: old_datasource_id = report.config_id try: report.config_id = datasource_map[old_datasource_id] except KeyError: pass # datasource not found old_id, new_id = self.save_couch_copy(report, self.new_domain) report_map[old_id] = new_id for static_report in StaticReportConfiguration.by_domain(self.existing_domain): if static_report.get_id.startswith(STATIC_PREFIX): report_id = static_report.get_id.replace( STATIC_PREFIX + self.existing_domain + '-', '' ) is_custom_report = False else: report_id = static_report.get_id.replace( CUSTOM_REPORT_PREFIX + self.existing_domain + '-', '' ) is_custom_report = True new_id = StaticReportConfiguration.get_doc_id( self.new_domain, report_id, is_custom_report ) # check that new report is in new domain's list of static reports StaticReportConfiguration.by_id(new_id) report_map[static_report.get_id] = new_id return report_map
def copy_ucr_reports(self, datasource_map): report_map = {} reports = get_report_configs_for_domain(self.existing_domain) for report in reports: old_datasource_id = report.config_id try: report.config_id = datasource_map[old_datasource_id] except KeyError: pass # datasource not found old_id, new_id = self.save_couch_copy(report, self.new_domain) report_map[old_id] = new_id for static_report in StaticReportConfiguration.by_domain( self.existing_domain): if static_report.get_id.startswith(STATIC_PREFIX): report_id = static_report.get_id.replace( STATIC_PREFIX + self.existing_domain + '-', '') is_custom_report = False else: report_id = static_report.get_id.replace( CUSTOM_REPORT_PREFIX + self.existing_domain + '-', '') is_custom_report = True new_id = StaticReportConfiguration.get_doc_id( self.new_domain, report_id, is_custom_report) # check that new report is in new domain's list of static reports StaticReportConfiguration.by_id(new_id) report_map[static_report.get_id] = new_id return report_map
def get_static_report_mapping(from_domain, to_domain): from corehq.apps.userreports.models import StaticReportConfiguration, STATIC_PREFIX, \ CUSTOM_REPORT_PREFIX report_map = {} for static_report in StaticReportConfiguration.by_domain(from_domain): if static_report.get_id.startswith(STATIC_PREFIX): report_id = static_report.get_id.replace( STATIC_PREFIX + from_domain + '-', '' ) is_custom_report = False else: report_id = static_report.get_id.replace( CUSTOM_REPORT_PREFIX + from_domain + '-', '' ) is_custom_report = True new_id = StaticReportConfiguration.get_doc_id( to_domain, report_id, is_custom_report ) # check that new report is in new domain's list of static reports try: StaticReportConfiguration.by_id(new_id, to_domain) except (BadSpecError, DocumentNotFound): pass else: report_map[static_report.get_id] = new_id return report_map
def test_static_reports(self): with override_settings(STATIC_UCR_REPORTS=[ self.get_path('static_report_config', 'json'), self.get_path('static_report_2_config', 'json') ]): reports = get_report_configs([ StaticReportConfiguration.get_doc_id('example', 'a-custom-report', False) ], 'example') self.assertEqual(len(reports), 1) reports = get_report_configs([ StaticReportConfiguration.get_doc_id('example', 'a-custom-report', False), StaticReportConfiguration.get_doc_id( 'example', 'another-custom-report', False), ], 'example') self.assertEqual(len(reports), 2)
def test_static_reports(self): with override_settings(STATIC_UCR_REPORTS=[ self.get_path('static_report_config', 'json'), self.get_path('static_report_2_config', 'json') ]): reports = get_report_configs( [StaticReportConfiguration.get_doc_id('example', 'a-custom-report', False)], 'example' ) self.assertEqual(len(reports), 1) reports = get_report_configs( [ StaticReportConfiguration.get_doc_id('example', 'a-custom-report', False), StaticReportConfiguration.get_doc_id('example', 'another-custom-report', False), ], 'example' ) self.assertEqual(len(reports), 2)
def test_mixed_reports(self): dynamic_report_id = uuid.uuid4().hex dynamic_report = ReportConfiguration( _id=dynamic_report_id, domain='example', config_id=uuid.uuid4().hex ) def get_docs_mock(db, ids): if ids == [dynamic_report_id]: return [dynamic_report.to_json()] return [] with patch('corehq.apps.userreports.models.get_docs', new=get_docs_mock): with override_settings(STATIC_UCR_REPORTS=[self.get_path('static_report_config', 'json')]): configs = get_report_configs( [dynamic_report_id, StaticReportConfiguration.get_doc_id('example', 'a-custom-report', False)], 'example' ) self.assertEqual(len(configs), 2)
def test_mixed_reports(self): dynamic_report_id = uuid.uuid4().hex dynamic_report = ReportConfiguration(_id=dynamic_report_id, domain='example', config_id=uuid.uuid4().hex) def get_docs_mock(db, ids): if ids == [dynamic_report_id]: return [dynamic_report.to_json()] return [] with patch('corehq.apps.userreports.models.get_docs', new=get_docs_mock): with override_settings(STATIC_UCR_REPORTS=[ self.get_path('static_report_config', 'json') ]): configs = get_report_configs([ dynamic_report_id, StaticReportConfiguration.get_doc_id( 'example', 'a-custom-report', False) ], 'example') self.assertEqual(len(configs), 2)