def test_get_by_domain(self): results = RegistryDataSourceConfiguration.by_domain('foo') self.assertEqual(2, len(results)) for item in results: self.assertTrue(item.table_id in ('foo1', 'foo2')) results = RegistryDataSourceConfiguration.by_domain('not-foo') self.assertEqual(0, len(results))
def get_datasources_for_domain(domain, referenced_doc_type=None, include_static=False, include_aggregate=False): from corehq.apps.userreports.models import DataSourceConfiguration, StaticDataSourceConfiguration, \ RegistryDataSourceConfiguration key = [domain] if referenced_doc_type: key.append(referenced_doc_type) datasources = sorted(DataSourceConfiguration.view( 'userreports/data_sources_by_build_info', startkey=key, endkey=key + [{}], reduce=False, include_docs=True), key=lambda config: config.display_name or '') datasources.extend( sorted(RegistryDataSourceConfiguration.by_domain(domain), key=lambda config: config.display_name or '')) if include_static: static_ds = StaticDataSourceConfiguration.by_domain(domain) if referenced_doc_type: static_ds = [ ds for ds in static_ds if ds.referenced_doc_type == referenced_doc_type ] datasources.extend( sorted(static_ds, key=lambda config: config.display_name)) if include_aggregate: from corehq.apps.aggregate_ucrs.models import AggregateTableDefinition datasources.extend( AggregateTableDefinition.objects.filter(domain=domain).all()) return datasources
def test_registry_global_validation(self): # global config in owning domain RegistryDataSourceConfiguration( domain=self.owning_domain, table_id='table', referenced_doc_type='CommCareCase', globally_accessible=True, registry_slug=self.registry.slug).save() # global config in participating domain (not owner) with self.assertRaises(RegistryAccessDenied): RegistryDataSourceConfiguration( domain='foo', table_id='table', referenced_doc_type='CommCareCase', globally_accessible=True, registry_slug=self.registry.slug).save()
def get_registry_data_sources_modified_since(timestamp): from corehq.apps.userreports.models import RegistryDataSourceConfiguration return RegistryDataSourceConfiguration.view( 'registry_data_sources_by_last_modified/view', startkey=[timestamp.isoformat()], endkey=[{}], reduce=False, include_docs=True).all()
def get_registry_data_sources_by_domain(domain): from corehq.apps.userreports.models import RegistryDataSourceConfiguration return sorted(RegistryDataSourceConfiguration.view( 'registry_data_sources/view', startkey=[domain], endkey=[domain, {}], reduce=False, include_docs=True, ), key=lambda config: config.display_name or '')
def get_all_registry_data_source_ids(is_active=None, globally_accessible=None): from corehq.apps.userreports.models import RegistryDataSourceConfiguration rows = RegistryDataSourceConfiguration.view( 'registry_data_sources/view', reduce=False, include_docs=False, ) return [ row["id"] for row in rows if (is_active is None or row["value"]["is_deactivated"] != is_active) and (globally_accessible is None or row["value"]["globally_accessible"] == globally_accessible) ]
def __init__(self, domain, *args, **kwargs): self.domain = domain standard_sources = DataSourceConfiguration.by_domain(self.domain) registry_sources = RegistryDataSourceConfiguration.by_domain( self.domain) custom_sources = list(StaticDataSourceConfiguration.by_domain(domain)) available_data_sources = standard_sources + registry_sources + custom_sources if toggles.AGGREGATE_UCRS.enabled(domain): from corehq.apps.aggregate_ucrs.models import AggregateTableDefinition available_data_sources += AggregateTableDefinition.objects.filter( domain=self.domain) super(ReportDataSourceField, self).__init__(choices=[(src.data_source_id, src.display_name) for src in available_data_sources], *args, **kwargs)
def setUpClass(cls): super(DomainChoiceProviderTest, cls).setUpClass() cls.domain_a = create_domain(name="A") cls.domain_b = create_domain(name="B") cls.domain_c = create_domain(name="C") cls.domain_d = create_domain(name="D") for domain in [cls.domain_a, cls.domain_b, cls.domain_c, cls.domain_d]: domain.save() cls.user = create_user("admin", "123") cls.web_user = cls.make_web_user_with_registry_role( '*****@*****.**', cls.domain_a, has_registry_access=True) cls.web_user_no_registry_access = cls.make_web_user_with_registry_role( '*****@*****.**', cls.domain_a) invitations = [Invitation('A'), Invitation('B'), Invitation('C')] # A, B, and C are in the registry A has access to B and C, B has access to C grants = [ Grant("C", ["B", "A"]), Grant("B", ["A"]), Grant("A", []), ] cls.registry = create_registry_for_test(cls.user, cls.domain, invitations, grants=grants, name="registry") cls.config = RegistryDataSourceConfiguration( domain="A", table_id='foo', referenced_doc_type='CommCareCase', registry_slug=cls.registry.slug, ) cls.config.save() cls.report = RegistryReportConfiguration(domain="A", config_id=cls.config._id) cls.report.save() choices = [ SearchableChoice("A", "A", ["A"]), SearchableChoice("B", "B", ["B"]), SearchableChoice("C", "C", ["C"]), ] choices.sort(key=lambda choice: choice.display) cls.choice_provider = DomainChoiceProvider(cls.report, None) cls.static_choice_provider = StaticChoiceProvider(choices)
def setUpClass(cls): super(RegistryDataSourceConfigurationDbTest, cls).setUpClass() cls.user = create_user("admin", "123") cls.owning_domain = 'foo_bar' cls.registry = create_registry_for_test(cls.user, cls.owning_domain, invitations=[ Invitation('foo'), Invitation('bar'), ], name='foo_bar') for domain, table in [('foo', 'foo1'), ('foo', 'foo2'), ('bar', 'bar1')]: RegistryDataSourceConfiguration( domain=domain, table_id=table, referenced_doc_type='CommCareCase', registry_slug=cls.registry.slug).save()
def test_domain_with_no_grants(self): user = self.make_web_user_with_registry_role( '*****@*****.**', self.domain_c, has_registry_access=True) config = RegistryDataSourceConfiguration( domain="C", table_id='foo', referenced_doc_type='CommCareCase', registry_slug=self.registry.slug, ) config.save() report = RegistryReportConfiguration(domain="C", config_id=config._id) self.choice_provider = DomainChoiceProvider(report, None) self.assertEqual([Choice(value='C', display='C')], self.choice_provider.query( ChoiceQueryContext(query='', offset=0, user=user))) config.delete()
def test_registry_slug_is_required(self): with self.assertRaises(BadValueError): RegistryDataSourceConfiguration( domain='domain', table_id='table', referenced_doc_type='CommCareCase').save()
def test_get_all(self): self.assertEqual(3, len(list(RegistryDataSourceConfiguration.all())))
def tearDownClass(cls): for config in RegistryDataSourceConfiguration.all(): config.delete() super(RegistryDataSourceConfigurationDbTest, cls).tearDownClass()
def test_configured_filter_validation(self): source = self.config.to_json() config = RegistryDataSourceConfiguration.wrap(source) config.validate()
def tearDown(self): for config in RegistryDataSourceConfiguration.all(): config.get_db().delete_doc(config.get_id)
def get_all_data_sources(self): active_ids = get_all_registry_data_source_ids(is_active=True) for result in iter_docs(RegistryDataSourceConfiguration.get_db(), active_ids): yield RegistryDataSourceConfiguration.wrap(result)