Esempio n. 1
0
    def test_full_delete(self):
        test_loc = make_loc(
            'test_loc',
            type='state',
            parent=self.user.location
        )
        test_loc.save()

        original_count = len(list(Location.by_domain(self.domain.name)))

        loc = self.user.location
        loc.full_delete()

        # it should also delete children
        self.assertEqual(
            len(list(Location.by_domain(self.domain.name))),
            original_count - 2
        )
        self.assertEqual(
            len(Location.root_locations(self.domain.name)),
            0
        )
        # permanently gone from sql db
        self.assertEqual(
            len(SQLLocation.objects.all()),
            0
        )
Esempio n. 2
0
    def test_archive(self):
        test_state = make_loc(
            'teststate',
            type='state',
            parent=self.user.location
        )
        test_state.save()

        original_count = len(list(Location.by_domain(self.domain.name)))

        loc = self.user.location
        loc.archive()

        # it should also archive children
        self.assertEqual(
            len(list(Location.by_domain(self.domain.name))),
            original_count - 2
        )
        self.assertEqual(
            len(Location.root_locations(self.domain.name)),
            0
        )

        loc.unarchive()

        # and unarchive children
        self.assertEqual(
            len(list(Location.by_domain(self.domain.name))),
            original_count
        )
        self.assertEqual(
            len(Location.root_locations(self.domain.name)),
            1
        )
Esempio n. 3
0
    def test_id_of_invalid_parent_type(self):
        # state can't have outlet as child
        data = {'name': 'oops', 'parent_site_code': self.test_state.site_code}

        original_count = len(list(Location.by_domain(self.domain.name)))

        result = import_location(self.domain.name, 'village', data)

        self.assertEqual(result['id'], None)
        self.assertEqual(len(list(Location.by_domain(self.domain.name))),
                         original_count)
        self.assertTrue('Invalid parent type' in result['message'])
Esempio n. 4
0
    def test_id_of_invalid_parent_type(self):
        # state can't have outlet as child
        parent = make_loc('sillyparents', type='state')
        data = {'name': 'oops', 'outlet_type': 'SHG', 'parent_id': parent._id}

        original_count = len(list(Location.by_domain(self.domain.name)))

        result = import_location(self.domain.name, 'outlet', data)

        self.assertEqual(result['id'], None)
        self.assertEqual(len(list(Location.by_domain(self.domain.name))),
                         original_count)
        self.assertTrue('Invalid parent type' in result['message'])
Esempio n. 5
0
    def test_id_of_invalid_parent_type(self):
        # state can't have outlet as child
        data = {
            'name': 'oops',
            'parent_site_code': self.test_state.site_code
        }

        original_count = len(list(Location.by_domain(self.domain.name)))

        result = import_location(self.domain.name, 'village', data)

        self.assertEqual(result['id'], None)
        self.assertEqual(len(list(Location.by_domain(self.domain.name))), original_count)
        self.assertTrue('Invalid parent type' in result['message'])
Esempio n. 6
0
    def testCreateSupplyPointFromFacility(self):
        [f1, f2] = self._get_facilities()
        self.assertEqual(0, len(list(Location.by_domain(TEST_DOMAIN))))
        sp1 = sync_facility_to_supply_point(TEST_DOMAIN, f1)
        locs = list(Location.by_domain(TEST_DOMAIN))
        self.assertEqual(1, len(locs))
        [loc1] = locs
        # check loc
        self.assertEqual(f1.name, loc1.name)
        self.assertEqual(f1.code, loc1.external_id)

        # check supply point
        self.assertEqual(f1.name, sp1.name)
        self.assertEqual(f1.code, sp1.external_id)
        self.assertEqual(sp1.location.location_id, loc1._id)
Esempio n. 7
0
    def testCreateSupplyPointFromFacility(self):
        [f1, f2] = self._get_facilities()
        self.assertEqual(0, len(list(Location.by_domain(TEST_DOMAIN))))
        sp1 = sync_facility_to_supply_point(TEST_DOMAIN, f1)
        locs = list(Location.by_domain(TEST_DOMAIN))
        self.assertEqual(1, len(locs))
        [loc1] = locs
        # check loc
        self.assertEqual(f1.name, loc1.name)
        self.assertEqual(f1.code, loc1.external_id)

        # check supply point
        self.assertEqual(f1.name, sp1.name)
        self.assertEqual(f1.code, sp1.external_id)
        self.assertEqual(sp1.location._id, loc1._id)
Esempio n. 8
0
    def test_invalid_parent_domain(self):
        parent = make_loc('someparent', domain='notright', type='village')

        data = {
            'name': 'bad parent',
            'outlet_type': 'SHG',
            'site_code': 'wat',
            'parent_id': parent._id,
        }

        original_count = len(list(Location.by_domain(self.domain.name)))
        result = import_location(self.domain.name, 'outlet', data)
        self.assertEqual(result['id'], None)
        self.assertEqual(len(list(Location.by_domain(self.domain.name))), original_count)
        self.assertTrue('references a location in another project' in result['message'])
Esempio n. 9
0
def update_historical_data(domain, locations=None):
    """
    If we don't have a record of this supply point being updated, run
    through all historical data and just fill in with zeros.
    """
    org_summaries = OrganizationSummary.objects.order_by('date')
    if org_summaries.count() == 0:
        return

    start_date = org_summaries[0].date

    if locations is None:
        if not ILSGatewayConfig.for_domain(domain).all_stock_data:
            locations = _get_test_locations(domain)
        else:
            locations = Location.by_domain(domain)

    for sp in locations:
        try:
            SupplyPointWarehouseRecord.objects.get(supply_point=sp._id)
        except SupplyPointWarehouseRecord.DoesNotExist:
            # we didn't have a record so go through and historically update
            # anything we maybe haven't touched
            for year, month in months_between(start_date,
                                              sp.sql_location.created_at):
                window_date = datetime(year, month, 1)
                for cls in [
                        OrganizationSummary, ProductAvailabilityData,
                        GroupSummary
                ]:
                    _init_warehouse_model(cls, sp, window_date)
            SupplyPointWarehouseRecord.objects.create(
                supply_point=sp._id, create_date=datetime.utcnow())
Esempio n. 10
0
    def handle(self, *args, **options):
        if not len(args) == 1:
            print "Format is ./manage.py delete_locations {}".format(self.args)
            return

        domain = args[0]
        domain_obj = Domain.get_by_name(domain)
        if domain_obj is None:
            print "Domain '{}' not found".format(domain)
            return

        couch_total = locs_by_domain(domain)
        sql_total = SQLLocation.objects.filter(domain=domain).count()
        msg = ("{} has {} Locations and {} SQLLocations, do you REALLY want "
               "to delete them?\n(y/n)"
               .format(domain, couch_total, sql_total))
        if raw_input(msg) != 'y':
            return

        print "Fine, your funeral"
        print '"Deleting" couch locs'
        self.bulk_delete_locs(
            Location.by_domain(domain, include_docs=False),
            couch_total,
        )

        print "Archiving SQLLocations"
        SQLLocation.objects.filter(domain=domain).update(is_archived=True)

        print "Finished"
Esempio n. 11
0
def generate_fixtures_for_domain(domain, db, data_source):

    location_ids = [location.get_id for location in Location.by_domain(domain)]
    dates = get_last_n_months(NUMBER_OF_MONTHS_FOR_FIXTURES)

    for date in dates:
        for location_id in location_ids:
            data_source.configure(config={
                "startdate": date[0],
                "enddate": date[1],
                "location_id": location_id,
                "domain": domain
            })
            report_data = data_source.get_data()

            for report_slug in report_data:
                rows = dict(report_data[report_slug].get("data", []))
                name = report_data[report_slug].get("name")

                # Remove cached fixture docs
                db.delete_docs(FixtureReportResult.all_by_composite_key(domain, location_id,
                                                                        date[0].strftime("%Y-%m-%d"),
                                                                        date[1].strftime("%Y-%m-%d"), report_slug))

                FixtureReportResult.save_result(domain, location_id, date[0].date(), date[1].date(),
                                                report_slug, rows, name)
Esempio n. 12
0
 def test_migration(self):
     bootstrap_domain(ILSGatewayAPI(TEST_DOMAIN, MockEndpoint('http://test-api.com/', 'dummy', 'dummy')))
     self.assertEqual(6, len(list(Product.by_domain(TEST_DOMAIN))))
     self.assertEqual(5, len(list(Location.by_domain(TEST_DOMAIN))))
     self.assertEqual(6, len(list(CommCareUser.by_domain(TEST_DOMAIN))))
     self.assertEqual(5, len(list(WebUser.by_domain(TEST_DOMAIN))))
     self.assertEqual(ILSMigrationStats.objects.filter(domain=TEST_DOMAIN).count(), 1)
Esempio n. 13
0
    def test_id_of_invalid_parent_type(self):
        # state can't have outlet as child
        parent = make_loc('sillyparents', type='state')
        data = {
            'name': 'oops',
            'outlet_type': 'SHG',
            'parent_id': parent._id
        }

        original_count = len(list(Location.by_domain(self.domain.name)))

        result = import_location(self.domain.name, 'outlet', data)

        self.assertEqual(result['id'], None)
        self.assertEqual(len(list(Location.by_domain(self.domain.name))), original_count)
        self.assertTrue('Invalid parent type' in result['message'])
Esempio n. 14
0
 def setUp(self):
     self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy')
     self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint)
     self.datapath = os.path.join(os.path.dirname(__file__), 'data')
     initial_bootstrap(TEST_DOMAIN)
     for location in Location.by_domain(TEST_DOMAIN):
         location.delete()
    def handle(self, *args, **options):
        for domain in Domain.get_all_names():
            fields_definition = cdm.CustomDataFieldsDefinition.get_or_create(
                domain,
                'LocationFields'
            )
            had_fields = bool(fields_definition.fields)

            existing_field_slugs = set([field.slug for field in fields_definition.fields])
            for location in Location.by_domain(domain):
                location_data = location.metadata
                for key in location_data.keys():
                    if (key and key not in existing_field_slugs
                        and not cdm.is_system_key(key)):
                        existing_field_slugs.add(key)
                        fields_definition.fields.append(cdm.CustomDataField(
                            slug=key,
                            label=key,
                            is_required=False,
                        ))

            for field in fields_definition.fields:
                if cdm.is_system_key(field.slug):
                    fields_definition.fields.remove(field)
            # Only save a definition for domains which use custom location data
            if fields_definition.fields or had_fields:
                fields_definition.save()
            print 'finished domain "{}"'.format(domain)
Esempio n. 16
0
    def test_locations_migration(self):
        checkpoint = MigrationCheckpoint(
            domain=TEST_DOMAIN,
            start_date=datetime.utcnow(),
            date=datetime.utcnow(),
            api='product',
            limit=100,
            offset=0
        )
        location_api = ApiSyncObject(
            'location_facility',
            self.endpoint.get_locations,
            self.api_object.location_sync,
            filters=dict(type='facility')
        )
        synchronization(location_api, checkpoint, None, 100, 0)
        self.assertEqual('location_facility', checkpoint.api)
        self.assertEqual(100, checkpoint.limit)
        self.assertEqual(0, checkpoint.offset)
        self.assertEqual(6, len(list(Location.by_domain(TEST_DOMAIN))))
        self.assertEqual(6, SQLLocation.objects.filter(domain=TEST_DOMAIN).count())
        sql_location = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='DM520053')
        self.assertEqual('FACILITY', sql_location.location_type.name)
        self.assertIsNotNone(sql_location.supply_point_id)

        sql_location2 = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='region-dodoma')
        self.assertEqual('REGION', sql_location2.location_type.name)
        self.assertIsNone(sql_location2.supply_point_id)
Esempio n. 17
0
    def handle(self, *args, **options):
        for domain in Domain.get_all_names():
            fields_definition = cdm.CustomDataFieldsDefinition.get_or_create(
                domain, 'LocationFields')
            had_fields = bool(fields_definition.fields)

            existing_field_slugs = set(
                [field.slug for field in fields_definition.fields])
            for location in Location.by_domain(domain):
                location_data = location.metadata
                for key in location_data.keys():
                    if (key and key not in existing_field_slugs
                            and not cdm.is_system_key(key)):
                        existing_field_slugs.add(key)
                        fields_definition.fields.append(
                            cdm.CustomDataField(
                                slug=key,
                                label=key,
                                is_required=False,
                            ))

            for field in fields_definition.fields:
                if cdm.is_system_key(field.slug):
                    fields_definition.fields.remove(field)
            # Only save a definition for domains which use custom location data
            if fields_definition.fields or had_fields:
                fields_definition.save()
            print 'finished domain "{}"'.format(domain)
Esempio n. 18
0
    def handle(self, *args, **options):
        if not len(args) == 1:
            print "Format is ./manage.py delete_locations {}".format(self.args)
            return

        domain = args[0]
        domain_obj = Domain.get_by_name(domain)
        if domain_obj is None:
            print "Domain '{}' not found".format(domain)
            return

        couch_total = locs_by_domain(domain)
        sql_total = SQLLocation.objects.filter(domain=domain).count()
        msg = ("{} has {} Locations and {} SQLLocations, do you REALLY want "
               "to delete them?\n(y/n)".format(domain, couch_total, sql_total))
        if raw_input(msg) != 'y':
            return

        print "Fine, your funeral"
        print '"Deleting" couch locs'
        self.bulk_delete_locs(
            Location.by_domain(domain, include_docs=False),
            couch_total,
        )

        print "Archiving SQLLocations"
        SQLLocation.objects.filter(domain=domain).update(is_archived=True)
        print "Finished"
Esempio n. 19
0
def generate_fixtures_for_domain(domain, db, data_source):

    location_ids = [location.get_id for location in Location.by_domain(domain)]
    dates = get_last_n_months(NUMBER_OF_MONTHS_FOR_FIXTURES)

    for date in dates:
        for location_id in location_ids:
            data_source.configure(
                config={
                    "startdate": date[0],
                    "enddate": date[1],
                    "location_id": location_id,
                    "domain": domain
                })
            report_data = data_source.get_data()

            for report_slug in report_data:
                rows = dict(report_data[report_slug].get("data", []))
                name = report_data[report_slug].get("name")

                # Remove cached fixture docs
                db.delete_docs(
                    FixtureReportResult.all_by_composite_key(
                        domain, location_id, date[0].strftime("%Y-%m-%d"),
                        date[1].strftime("%Y-%m-%d"), report_slug))

                FixtureReportResult.save_result(domain, location_id,
                                                date[0].date(), date[1].date(),
                                                report_slug, rows, name)
Esempio n. 20
0
    def main_context(self):
        try:
            facilities = Location.filter_by_type_count(self.domain, 'FACILITY')
        except TypeError:
            facilities = 0

        contacts = CommCareUser.by_domain(self.domain, reduce=True)
        web_users = WebUser.by_domain(self.domain, reduce=True)

        try:
            products = len(Product.by_domain(self.domain))
        except ResourceNotFound:
            products = 0

        main_context = super(GlobalStats, self).main_context
        context = {
            'supply_points': len(list(Location.by_domain(self.domain))),
            'facilities': facilities,
            'contacts': contacts[0]['value'] if contacts else 0,
            'web_users': web_users[0]['value'] if web_users else 0,
            'products': products,
            #TODO add next after the enlargement ILS migration
            'product_stocks': 0,
            'stock_transactions': 0,
            'inbound_messages': 0,
            'outbound_messages': 0
        }
        main_context.update(context)
        return main_context
Esempio n. 21
0
    def test_locations_migration(self):
        checkpoint = MigrationCheckpoint(
            domain=TEST_DOMAIN,
            start_date=datetime.utcnow(),
            date=datetime.utcnow(),
            api='product',
            limit=100,
            offset=0
        )
        location_api = ApiSyncObject(
            'location_facility',
            self.endpoint.get_locations,
            self.api_object.location_sync,
            filters=dict(type='facility')
        )
        synchronization(location_api, checkpoint, None, 100, 0)
        self.assertEqual('location_facility', checkpoint.api)
        self.assertEqual(100, checkpoint.limit)
        self.assertEqual(0, checkpoint.offset)
        self.assertEqual(5, len(list(Location.by_domain(TEST_DOMAIN))))
        self.assertEqual(5, SQLLocation.objects.filter(domain=TEST_DOMAIN).count())
        sql_location = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='DM520053')
        self.assertEqual('FACILITY', sql_location.location_type.name)
        self.assertIsNotNone(sql_location.supply_point_id)

        sql_location2 = SQLLocation.objects.get(domain=TEST_DOMAIN, site_code='region-dodoma')
        self.assertEqual('REGION', sql_location2.location_type.name)
        self.assertIsNone(sql_location2.supply_point_id)
Esempio n. 22
0
    def main_context(self):
        try:
            facilities = Location.filter_by_type_count(self.domain, 'FACILITY')
        except TypeError:
            facilities = 0

        contacts = CommCareUser.by_domain(self.domain, reduce=True)
        web_users = WebUser.by_domain(self.domain, reduce=True)

        try:
            products = len(Product.by_domain(self.domain))
        except ResourceNotFound:
            products = 0

        main_context = super(GlobalStats, self).main_context
        context = {
            'supply_points':  len(list(Location.by_domain(self.domain))),
            'facilities': facilities,
            'contacts':  contacts[0]['value'] if contacts else 0,
            'web_users': web_users[0]['value'] if web_users else 0,
            'products':  products,
            #TODO add next after the enlargement ILS migration
            'product_stocks':  0,
            'stock_transactions':  0,
            'inbound_messages':  0,
            'outbound_messages':  0
        }
        main_context.update(context)
        return main_context
Esempio n. 23
0
    def test_invalid_parent_domain(self):
        parent = make_loc('someparent', domain='notright', type='village')

        data = {
            'name': 'bad parent',
            'outlet_type': 'SHG',
            'site_code': 'wat',
            'parent_site_code': parent.site_code,
        }

        original_count = len(list(Location.by_domain(self.domain.name)))
        result = import_location(self.domain.name, 'outlet', data)
        self.assertEqual(result['id'], None)
        self.assertEqual(len(list(Location.by_domain(self.domain.name))),
                         original_count)
        self.assertTrue('does not exist in this project' in result['message'])
Esempio n. 24
0
def update_historical_data(domain, locations=None):
    """
    If we don't have a record of this supply point being updated, run
    through all historical data and just fill in with zeros.
    """
    org_summaries = OrganizationSummary.objects.order_by('date')
    if org_summaries.count() == 0:
        return

    start_date = org_summaries[0].date

    if locations is None:
        if not ILSGatewayConfig.for_domain(domain).all_stock_data:
            locations = _get_test_locations(domain)
        else:
            locations = Location.by_domain(domain)

    for sp in locations:
        try:
            SupplyPointWarehouseRecord.objects.get(supply_point=sp._id)
        except SupplyPointWarehouseRecord.DoesNotExist:
            # we didn't have a record so go through and historically update
            # anything we maybe haven't touched
            for year, month in months_between(start_date, sp.sql_location.created_at):
                window_date = datetime(year, month, 1)
                for cls in [OrganizationSummary, ProductAvailabilityData, GroupSummary]:
                    _init_warehouse_model(cls, sp, window_date)
            SupplyPointWarehouseRecord.objects.create(supply_point=sp._id,
                                                      create_date=datetime.utcnow())
Esempio n. 25
0
 def test_migration(self):
     ils_bootstrap_domain_test_task(
         TEST_DOMAIN, MockEndpoint('http://test-api.com/', 'dummy',
                                   'dummy'))
     self.assertEqual(6, len(list(Product.by_domain(TEST_DOMAIN))))
     self.assertEqual(5, len(list(Location.by_domain(TEST_DOMAIN))))
     self.assertEqual(6, len(list(CommCareUser.by_domain(TEST_DOMAIN))))
     self.assertEqual(5, len(list(WebUser.by_domain(TEST_DOMAIN))))
Esempio n. 26
0
    def test_id_of_invalid_parent_type(self):
        parent = make_loc("sillyparents")
        parent.location_type = "state"  # state can't have outlet as child
        parent.save()

        data = {"name": "oops", "outlet_type": "SHG", "parent_id": parent._id}

        original_count = len(list(Location.by_domain(self.domain.name)))

        try:
            result = import_location(self.domain.name, "outlet", data)
        except Exception as e:
            self.fail("import_location raised an error: %s" % e)

        self.assertEqual(result["id"], None)
        self.assertEqual(len(list(Location.by_domain(self.domain.name))), original_count)
        self.assertTrue("Invalid parent type" in result["message"])
Esempio n. 27
0
def fix_groups_in_location_task(domain):
    locations = Location.by_domain(domain=domain)
    for loc in locations:
        groups = loc.metadata.get('groups', None)
        if groups:
            loc.metadata['group'] = groups[0]
            del loc.metadata['groups']
            loc.save()
Esempio n. 28
0
    def test_full_delete(self):
        test_loc = make_loc('test_loc',
                            type='state',
                            parent=self.user.location)
        test_loc.save()

        original_count = len(list(Location.by_domain(self.domain.name)))

        loc = self.user.sql_location
        loc.full_delete()

        # it should also delete children
        self.assertEqual(len(list(Location.by_domain(self.domain.name))),
                         original_count - 2)
        self.assertEqual(len(Location.root_locations(self.domain.name)), 0)
        # permanently gone from sql db
        self.assertEqual(len(SQLLocation.objects.all()), 0)
Esempio n. 29
0
def fix_groups_in_location_task(domain):
    locations = Location.by_domain(domain=domain)
    for loc in locations:
        groups = loc.metadata.get('groups', None)
        if groups:
            loc.metadata['group'] = groups[0]
            del loc.metadata['groups']
            loc.save()
Esempio n. 30
0
 def setUp(self):
     self.endpoint = MockEndpoint("http://test-api.com/", "dummy", "dummy")
     self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint)
     self.datapath = os.path.join(os.path.dirname(__file__), "data")
     domain = initial_bootstrap(TEST_DOMAIN)
     CommtrackConfig(domain=domain.name).save()
     self.api_object.prepare_commtrack_config()
     for location in Location.by_domain(TEST_DOMAIN):
         location.delete()
Esempio n. 31
0
def get_location_hierarchy_by_id(location_id, domain, CCT_only=False):
    if location_id is None or len(location_id) == 0:
        return [location.get_id for location in Location.by_domain(domain) if not CCT_only or _is_location_CCT(location)]
    else:
        user_location = Location.get(location_id)
        locations = [location.get_id for location in user_location.descendants if not CCT_only or _is_location_CCT(location)]
        if not CCT_only or _is_location_CCT(user_location):
            locations.insert(0, user_location.get_id)
        return locations
Esempio n. 32
0
 def setUp(self):
     self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy')
     self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint)
     self.datapath = os.path.join(os.path.dirname(__file__), 'data')
     domain = initial_bootstrap(TEST_DOMAIN)
     CommtrackConfig(domain=domain.name).save()
     self.api_object.prepare_commtrack_config()
     for location in Location.by_domain(TEST_DOMAIN):
         location.delete()
Esempio n. 33
0
    def tearDown(self):
        for location in Location.by_domain(self.TEST_DOMAIN):
            location.delete()

        for user in WebUser.by_domain(self.TEST_DOMAIN):
            user.delete()

        for vn in VerifiedNumber.by_domain(self.TEST_DOMAIN):
            vn.delete()
Esempio n. 34
0
 def setUp(self):
     self.datapath = os.path.join(os.path.dirname(__file__), 'data')
     bootstrap_domain(TEST_DOMAIN)
     delete_all_cases()
     for loc in Location.by_domain(TEST_DOMAIN):
         loc.delete()
     LocationType.objects.get_or_create(
         domain=TEST_DOMAIN,
         name="Lvl3 Hospital",
     )
Esempio n. 35
0
    def tearDown(self):
        for location in Location.by_domain(self.TEST_DOMAIN):
            location.delete()

        for user in WebUser.by_domain(self.TEST_DOMAIN):
            user.delete()

        delete_domain_phone_numbers(self.TEST_DOMAIN)

        super(SMSNotificationTestCase, self).tearDown()
Esempio n. 36
0
 def setUp(self):
     self.datapath = os.path.join(os.path.dirname(__file__), 'data')
     bootstrap_domain(TEST_DOMAIN)
     delete_all_cases()
     for loc in Location.by_domain(TEST_DOMAIN):
         loc.delete()
     LocationType.objects.get_or_create(
         domain=TEST_DOMAIN,
         name="Lvl3 Hospital",
     )
Esempio n. 37
0
    def tearDown(self):
        for location in Location.by_domain(self.TEST_DOMAIN):
            location.delete()

        for user in WebUser.by_domain(self.TEST_DOMAIN):
            user.delete()

        delete_domain_phone_numbers(self.TEST_DOMAIN)

        super(SMSNotificationTestCase, self).tearDown()
Esempio n. 38
0
def location_fixture_generator(user, version, last_sync=None):
    """
    By default this will generate a fixture for the users
    location and it's "footprint", meaning the path
    to a root location through parent hierarchies.

    There is an admin feature flag that will make this generate
    a fixture with ALL locations for the domain.
    """
    project = user.project
    if (not project or not project.commtrack_enabled
        or not project.commtrack_settings
        or not project.commtrack_settings.sync_location_fixtures):
            return []

    if toggles.SYNC_ALL_LOCATIONS.enabled(user.domain):
        location_db = _location_footprint(Location.by_domain(user.domain))
    else:
        locations = []
        if user.location:
            # add users location (and ancestors) to fixture
            locations.append(user.location)

            # optionally add all descendants as well
            if user.location.location_type_object.view_descendants:
                locations += user.location.descendants

        if user.project.supports_multiple_locations_per_user:
            # this might add duplicate locations but we filter that out later
            locations += user.locations
        location_db = _location_footprint(locations)

    if not should_sync_locations(last_sync, location_db):
        return []

    root = ElementTree.Element('fixture',
                               {'id': 'commtrack:locations',
                                'user_id': user.user_id})

    loc_types = project.location_types
    type_to_slug_mapping = dict((ltype.name, ltype.code) for ltype in loc_types)

    def location_type_lookup(location_type):
        return type_to_slug_mapping.get(location_type, unicode_slug(location_type))

    if toggles.SYNC_ALL_LOCATIONS.enabled(user.domain):
        root_locations = Location.root_locations(user.domain)
    else:
        root_locations = filter(lambda loc: loc.parent_id is None, location_db.by_id.values())

    if not root_locations:
        return []
    else:
        _append_children(root, location_db, root_locations, location_type_lookup)
        return [root]
Esempio n. 39
0
    def test_location_queries(self):
        test_state1 = make_loc("teststate1", type="state", parent=self.user.location, domain=self.domain.name)
        test_state2 = make_loc("teststate2", type="state", parent=self.user.location, domain=self.domain.name)
        test_village1 = make_loc("testvillage1", type="village", parent=test_state1, domain=self.domain.name)
        test_village1.site_code = "tv1"
        test_village1.save()
        test_village2 = make_loc("testvillage2", type="village", parent=test_state2, domain=self.domain.name)

        def compare(list1, list2):
            self.assertEqual(set([l._id for l in list1]), set([l._id for l in list2]))

        # descendants
        compare([test_state1, test_state2, test_village1, test_village2], self.user.location.descendants)

        # children
        compare([test_state1, test_state2], self.user.location.children)

        # siblings
        compare([test_state2], test_state1.siblings())

        # parent and parent_id
        self.assertEqual(self.user.location._id, test_state1.parent_id)
        self.assertEqual(self.user.location._id, test_state1.parent._id)

        # is_root
        self.assertTrue(self.user.location.is_root)
        self.assertFalse(test_state1.is_root)

        # Location.root_locations
        compare([self.user.location], Location.root_locations(self.domain.name))

        # Location.filter_by_type
        compare([test_village1, test_village2], Location.filter_by_type(self.domain.name, "village"))
        compare([test_village1], Location.filter_by_type(self.domain.name, "village", test_state1))

        # Location.get_in_domain
        test_village2.domain = "rejected"
        bootstrap_location_types("rejected")
        test_village2.save()
        self.assertEqual(Location.get_in_domain(self.domain.name, test_village1._id)._id, test_village1._id)
        self.assertIsNone(Location.get_in_domain(self.domain.name, test_village2._id))
        self.assertIsNone(Location.get_in_domain(self.domain.name, "not-a-real-id"))

        self.assertEqual(
            {loc._id for loc in [self.user.location, test_state1, test_state2, test_village1]},
            set(SQLLocation.objects.filter(domain=self.domain.name).location_ids()),
        )

        # Location.by_site_code
        self.assertEqual(test_village1._id, Location.by_site_code(self.domain.name, "tv1")._id)
        self.assertIsNone(None, Location.by_site_code(self.domain.name, "notreal"))

        # Location.by_domain
        compare([self.user.location, test_state1, test_state2, test_village1], Location.by_domain(self.domain.name))
Esempio n. 40
0
    def setUp(self):
        self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy')
        self.api_object = EWSApi(TEST_DOMAIN, self.endpoint)
        self.datapath = os.path.join(os.path.dirname(__file__), 'data')
        initial_bootstrap(TEST_DOMAIN)
        self.api_object.prepare_commtrack_config()
        for location in CouchLocation.by_domain(TEST_DOMAIN):
            location.delete()

        for sql_location in SQLLocation.objects.all():
            sql_location.delete()
    def test_id_of_invalid_parent_type(self):
        parent = make_loc('sillyparents')
        parent.location_type = 'state'  # state can't have outlet as child
        parent.save()

        data = {
            'name': 'oops',
            'outlet_type': 'SHG',
            'parent_id': parent._id
        }

        original_count = len(list(Location.by_domain(self.domain.name)))

        try:
            result = import_location(self.domain.name, 'outlet', data)
        except Exception as e:
            self.fail("import_location raised an error: %s" % e)

        self.assertEqual(result['id'], None)
        self.assertEqual(len(list(Location.by_domain(self.domain.name))), original_count)
        self.assertTrue('Invalid parent type' in result['message'])
Esempio n. 42
0
    def tearDown(self):
        for location in Location.by_domain(self.TEST_DOMAIN):
            location.delete()

        for user in WebUser.by_domain(self.TEST_DOMAIN):
            user.delete()

        delete_domain_phone_numbers(self.TEST_DOMAIN)

        for product in Product.by_domain(self.TEST_DOMAIN):
            product.delete()

        super(UrgentNonReportingNotificationTestCase, self).tearDown()
Esempio n. 43
0
    def test_archive(self):
        test_state = make_loc('teststate',
                              type='state',
                              parent=self.user.location)
        test_state.save()

        original_count = len(list(Location.by_domain(self.domain.name)))

        loc = self.user.sql_location
        loc.archive()

        # it should also archive children
        self.assertEqual(len(list(Location.by_domain(self.domain.name))),
                         original_count - 2)
        self.assertEqual(len(Location.root_locations(self.domain.name)), 0)

        loc.unarchive()

        # and unarchive children
        self.assertEqual(len(list(Location.by_domain(self.domain.name))),
                         original_count)
        self.assertEqual(len(Location.root_locations(self.domain.name)), 1)
Esempio n. 44
0
    def tearDown(self):
        for location in Location.by_domain(self.TEST_DOMAIN):
            location.delete()

        for user in WebUser.by_domain(self.TEST_DOMAIN):
            user.delete()

        delete_domain_phone_numbers(self.TEST_DOMAIN)

        for product in Product.by_domain(self.TEST_DOMAIN):
            product.delete()

        super(MissingReportNotificationTestCase, self).tearDown()
Esempio n. 45
0
    def tearDown(self):
        for product in Product.by_domain(TEST_DOMAIN):
            product.delete()

        for location in Location.by_domain(TEST_DOMAIN):
            location.delete()

        for user in CommCareUser.by_domain(TEST_DOMAIN):
            user.delete()

        for web_user in WebUser.by_domain(TEST_DOMAIN):
            web_user.delete()

        ILSMigrationStats.objects.all().delete()
        ILSMigrationProblem.objects.all().delete()
Esempio n. 46
0
def get_location_hierarchy_by_id(location_id, domain, CCT_only=False):
    if location_id is None or len(location_id) == 0:
        return [
            location.get_id for location in Location.by_domain(domain)
            if not CCT_only or _is_location_CCT(location)
        ]
    else:
        user_location = Location.get(location_id)
        locations = [
            location.get_id for location in user_location.descendants
            if not CCT_only or _is_location_CCT(location)
        ]
        if not CCT_only or _is_location_CCT(user_location):
            locations.insert(0, user_location.get_id)
        return locations
Esempio n. 47
0
 def test_locations_migration(self):
     checkpoint = MigrationCheckpoint(
         domain=TEST_DOMAIN,
         start_date=datetime.now(),
         date=datetime.now(),
         api='product',
         limit=100,
         offset=0
     )
     synchronization('location_facility',
                     self.endpoint.get_locations,
                     self.api_object.location_sync, checkpoint, None, 100, 0, filters=dict(type='facility'))
     self.assertEqual('location_facility', checkpoint.api)
     self.assertEqual(100, checkpoint.limit)
     self.assertEqual(0, checkpoint.offset)
     self.assertEqual(4, len(list(Location.by_domain(TEST_DOMAIN))))
Esempio n. 48
0
    def test_delivery_group_basic(self):
        submitting_group = DeliveryGroups().current_submitting_group()
        original_submitting = len(list(get_sql_locations_by_domain_and_group(
            TEST_DOMAIN,
            submitting_group
        )))

        for location in Location.by_domain(TEST_DOMAIN):
            if location.metadata.get('group') != submitting_group:
                location.metadata['group'] = submitting_group
                location.save()
                break

        new_submitting = len(list(get_sql_locations_by_domain_and_group(
            TEST_DOMAIN,
            submitting_group
        )))
        self.assertEqual(original_submitting + 1, new_submitting)
 def test_locations_migration(self):
     checkpoint = MigrationCheckpoint(
         domain=TEST_DOMAIN,
         start_date=datetime.now(),
         date=datetime.now(),
         api='product',
         limit=1000,
         offset=0
     )
     locations_sync(TEST_DOMAIN, MockEndpoint('http://test-api.com/', 'dummy', 'dummy'),
                    checkpoint,
                    limit=1000,
                    offset=0,
                    filters=dict(type='facility'))
     self.assertEqual('location_facility', checkpoint.api)
     self.assertEqual(1000, checkpoint.limit)
     self.assertEqual(0, checkpoint.offset)
     self.assertEqual(4, len(list(Location.by_domain(TEST_DOMAIN))))
Esempio n. 50
0
    def test_delivery_group_basic(self):
        submitting_group = DeliveryGroups().current_submitting_group()
        original_submitting = len(
            list(
                get_sql_locations_by_domain_and_group(TEST_DOMAIN,
                                                      submitting_group)))

        for location in Location.by_domain(TEST_DOMAIN):
            if location.metadata.get('group') != submitting_group:
                location.metadata['group'] = submitting_group
                location.save()
                break

        new_submitting = len(
            list(
                get_sql_locations_by_domain_and_group(TEST_DOMAIN,
                                                      submitting_group)))
        self.assertEqual(original_submitting + 1, new_submitting)
Esempio n. 51
0
def generate_fixtures_for_domain(domain, db, data_source):
    # Remove all FixtureReportResult instances, as they would either be deleted or replaced anyway
    db.delete_docs(FixtureReportResult.by_domain(domain=domain))

    location_ids = [location.get_id for location in Location.by_domain(domain)]
    dates = get_last_n_months(NUMBER_OF_MONTHS_FOR_FIXTURES)

    for date in dates:
        for location_id in location_ids:
            data_source.configure(config={
                "startdate": date[0],
                "enddate": date[1],
                "location_id": location_id,
                "domain": domain
            })
            report_data = data_source.get_data()

            for report_slug in report_data:
                rows = dict(report_data[report_slug].get("data", []))
                name = report_data[report_slug].get("name")
                FixtureReportResult.save_result(domain, location_id, date[0].date(), date[1].date(), report_slug, rows, name)
Esempio n. 52
0
    def test_location_queries(self):
        test_state1 = make_loc(
            'teststate1',
            type='state',
            parent=self.user.location,
            domain=self.domain.name
        )
        test_state2 = make_loc(
            'teststate2',
            type='state',
            parent=self.user.location,
            domain=self.domain.name
        )
        test_village1 = make_loc(
            'testvillage1',
            type='village',
            parent=test_state1,
            domain=self.domain.name
        )
        test_village1.site_code = 'tv1'
        test_village1.save()
        test_village2 = make_loc(
            'testvillage2',
            type='village',
            parent=test_state2,
            domain=self.domain.name
        )

        def compare(list1, list2):
            self.assertEqual(
                set([l._id for l in list1]),
                set([l._id for l in list2])
            )

        # descendants
        compare(
            [test_state1, test_state2, test_village1, test_village2],
            self.user.location.descendants
        )

        # children
        compare(
            [test_state1, test_state2],
            self.user.location.children
        )

        # siblings
        compare(
            [test_state2],
            test_state1.siblings()
        )

        # parent and parent_id
        self.assertEqual(
            self.user.location._id,
            test_state1.parent_id
        )
        self.assertEqual(
            self.user.location._id,
            test_state1.parent._id
        )


        # is_root
        self.assertTrue(self.user.location.is_root)
        self.assertFalse(test_state1.is_root)

        # Location.root_locations
        compare(
            [self.user.location],
            Location.root_locations(self.domain.name)
        )

        # Location.filter_by_type
        compare(
            [test_village1, test_village2],
            Location.filter_by_type(self.domain.name, 'village')
        )
        compare(
            [test_village1],
            Location.filter_by_type(self.domain.name, 'village', test_state1)
        )

        # Location.get_in_domain
        test_village2.domain = 'rejected'
        bootstrap_location_types('rejected')
        test_village2.save()
        self.assertEqual(
            Location.get_in_domain(self.domain.name, test_village1._id)._id,
            test_village1._id
        )
        self.assertIsNone(
            Location.get_in_domain(self.domain.name, test_village2._id),
        )
        self.assertIsNone(
            Location.get_in_domain(self.domain.name, 'not-a-real-id'),
        )

        self.assertEqual(
            {loc._id for loc in [self.user.location, test_state1, test_state2,
                                 test_village1]},
            set(SQLLocation.objects.filter(domain=self.domain.name).location_ids()),
        )

        # Location.by_site_code
        self.assertEqual(
            test_village1._id,
            Location.by_site_code(self.domain.name, 'tv1')._id
        )
        self.assertIsNone(
            None,
            Location.by_site_code(self.domain.name, 'notreal')
        )

        # Location.by_domain
        compare(
            [self.user.location, test_state1, test_state2, test_village1],
            Location.by_domain(self.domain.name)
        )
Esempio n. 53
0
 def setUp(self):
     self.datapath = os.path.join(os.path.dirname(__file__), 'data')
     bootstrap_domain(TEST_DOMAIN)
     delete_all_cases()
     for loc in Location.by_domain(TEST_DOMAIN):
         loc.delete()
 def setUp(self):
     self.datapath = os.path.join(os.path.dirname(__file__), 'data')
     initial_bootstrap(TEST_DOMAIN)
     for location in Location.by_domain(TEST_DOMAIN):
         location.delete()
Esempio n. 55
0
 def names_of_locs(self):
     return [loc.name for loc in Location.by_domain(self.domain.name)]