def test_invalidate_entity(self): e = Entity(self.dbm, entity_type='store', location=['nyc']) e.save() self.assertFalse(e._doc.void) apple_type = DataDictType(self.dbm, name='Apples', slug='apples', primitive_type='number') orange_type = DataDictType(self.dbm, name='Oranges', slug='oranges', primitive_type='number') apple_type.save() orange_type.save() data = [[('apples', 20, apple_type), ('oranges', 30, orange_type)], [('apples', 10, apple_type), ('oranges', 20, orange_type)]] data_ids = [] for d in data: id = e.add_data(d) self.assertFalse(self.dbm._load_document(id).void) data_ids.append(id) e.invalidate() self.assertTrue(e._doc.void) for id in data_ids: self.assertTrue(self.dbm._load_document(id).void)
def _setup_entities(self): ENTITY_TYPE = ["Health_Facility", "Clinic"] AGGREGATION_PATH_NAME = "governance" # Entities for State 1: Maharashtra # location, aggregation_path locations = [ ['India', 'MH', 'Pune'], ['India', 'MH', 'Mumbai'], ['India', 'Karnataka', 'Bangalore'], ['India', 'Karnataka', 'Hubli'], ['India', 'Kerala', 'Kochi'], ] aggregation_paths = [ ["Director", "Med_Supervisor", "Surgeon"], ["Director", "Med_Supervisor", "Nurse"], ["Director", "Med_Officer", "Doctor"], ["Director", "Med_Officer", "Surgeon"], ["Director", "Med_Officer", "Nurse"], ] self.entities = [] for i in range(self._number_of_entities): location = random.choice(locations) aggregation_path = random.choice(aggregation_paths) e = Entity(self.manager, entity_type=ENTITY_TYPE, location=location) e.set_aggregation_path(AGGREGATION_PATH_NAME, aggregation_path) e.save() self.entities.append(e)
def _setup_entities(self): ENTITY_TYPE = ["Health_Facility", "Clinic"] AGGREGATION_PATH_NAME = "governance" # Entities for State 1: Maharashtra # location, aggregation_path locations = [ ['India', 'MH', 'Pune'], ['India', 'MH', 'Mumbai'], ['India', 'Karnataka', 'Bangalore'], ['India', 'Karnataka', 'Hubli'], ['India', 'Kerala', 'Kochi'], ] aggregation_paths = [ ["Director", "Med_Supervisor", "Surgeon"], ["Director", "Med_Supervisor", "Nurse"], ["Director", "Med_Officer", "Doctor"], ["Director", "Med_Officer", "Surgeon"], ["Director", "Med_Officer", "Nurse"], ] self.entities = [] for i in range(self._number_of_entities): location = random.choice(locations) aggregation_path = random.choice(aggregation_paths) e = Entity(self.manager, entity_type=ENTITY_TYPE, location=location) e.set_aggregation_path(AGGREGATION_PATH_NAME, aggregation_path) e.save() self.entities.append(e)
def _create_clinic_and_reporter(self): clinic_entity_short_code = 'clinic01' clinic_entity = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Pune"], short_code=clinic_entity_short_code) clinic_entity.save() reporter_entity_short_code = 'reporter01' reporter_entity = Entity(self.dbm, entity_type="reporter", short_code=reporter_entity_short_code) reporter_entity.save() return clinic_entity, clinic_entity_short_code, reporter_entity, reporter_entity_short_code
def test_should_get_entity_by_short_code(self): reporter = Entity(self.dbm, entity_type=["Reporter"], location=["Pune", "India"], short_code="repx") reporter.save() entity = get_by_short_code(self.dbm, short_code="repx", entity_type=["Reporter"]) self.assertTrue(entity is not None) self.assertEqual("repx", entity.short_code) with self.assertRaises(DataObjectNotFound): entity = get_by_short_code(self.dbm, short_code="ABC", entity_type=["Waterpoint"])
def test_should_add_email_to_datasender(self): email = "*****@*****.**" entity = Entity(self.manager, entity_type=REPORTER_ENTITY_TYPE, short_code="short_code") entity.save() create_default_reg_form_model(self.manager) put_email_information_to_entity(self.manager, entity, email=email) entity_values = entity.data self.assertEquals(entity_values["email"]["value"], email)
def test_invalidate_data(self): e = Entity(self.dbm, entity_type='store', location=['nyc']) e.save() apple_type = DataDictType(self.dbm, name='Apples', slug='apples', primitive_type='number') orange_type = DataDictType(self.dbm, name='Oranges', slug='oranges', primitive_type='number') apple_type.save() orange_type.save() data = e.add_data([('apples', 20, apple_type), ('oranges', 30, orange_type)]) valid_doc = self.dbm._load_document(data) self.assertFalse(valid_doc.void) e.invalidate_data(data) invalid_doc = self.dbm._load_document(data) self.assertTrue(invalid_doc.void)
def _create_clinic_and_reporter(self): clinic_entity_short_code = 'clinic01' clinic_entity = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Pune"], short_code=clinic_entity_short_code) clinic_entity.save() reporter_entity_short_code = 'reporter01' reporter_entity = Entity(self.dbm, entity_type="reporter", short_code=reporter_entity_short_code) reporter_entity.save() return clinic_entity, clinic_entity_short_code, reporter_entity, reporter_entity_short_code
def test_should_add_email_to_datasender(self): email = "*****@*****.**" entity = Entity(self.manager, entity_type=REPORTER_ENTITY_TYPE, short_code="short_code") entity.save() delete_and_create_form_model(self.manager, GLOBAL_REGISTRATION_FORM_CODE) set_email_for_contact(self.manager, entity, email=email) entity_values = entity.data self.assertEquals(entity_values["email"]["value"], email)
def test_should_get_all_entities(self): e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Mumbai"], short_code='cli002') e.save() e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Jalgaon"], short_code='cli003') e.save() e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Nasik"], short_code='cli004') uuid = e.save() all_entities = get_all_entities(self.dbm) self.assertEqual(4, len(all_entities)) self.assertEqual([uuid], [e['id'] for e in all_entities if e['id'] == uuid])
def test_create_entity(self): e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Pune"]) uuid = e.save() self.assertTrue(uuid) self.dbm.delete(e)
def test_should_add_location_hierarchy_on_create(self): e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Pune"]) uuid = e.save() saved = get(self.dbm, uuid) self.assertEqual(saved.location_path, ["India", "MH", "Pune"])
def test_should_return_data_types(self): med_type = DataDictType(self.dbm, name='Medicines', slug='meds', primitive_type='number', description='Number of medications', tags=['med']) med_type.save() doctor_type = DataDictType(self.dbm, name='Doctor', slug='doc', primitive_type='string', description='Name of doctor', tags=['doctor', 'med']) doctor_type.save() facility_type = DataDictType(self.dbm, name='Facility', slug='facility', primitive_type='string', description='Name of facility') facility_type.save() e = Entity(self.dbm, entity_type='foo') e.save() data_record = [('meds', 20, med_type), ('doc', "aroj", doctor_type), ('facility', 'clinic', facility_type)] e.add_data(data_record) # med (tag in list) types = [typ.slug for typ in e.data_types(['med'])] self.assertTrue(med_type.slug in types) self.assertTrue(doctor_type.slug in types) self.assertTrue(facility_type.slug not in types) # doctor (tag as string) types = [typ.slug for typ in e.data_types('doctor')] self.assertTrue(doctor_type.slug in types) self.assertTrue(med_type.slug not in types) self.assertTrue(facility_type.slug not in types) # med and doctor (more than one tag) types = [typ.slug for typ in e.data_types(['med', 'doctor'])] self.assertTrue(doctor_type.slug in types) self.assertTrue(med_type.slug not in types) self.assertTrue(facility_type.slug not in types) # no tags types = [typ.slug for typ in e.data_types()] self.assertTrue(med_type.slug in types) self.assertTrue(doctor_type.slug in types) self.assertTrue(facility_type.slug in types)
def test_should_get_entity_by_short_code(self): reporter = Entity(self.dbm, entity_type=["Reporter"], location=["Pune", "India"], short_code="repx") reporter.save() entity = get_by_short_code(self.dbm, short_code="repx", entity_type=["Reporter"]) self.assertTrue(entity is not None) self.assertEqual("repx", entity.short_code) with self.assertRaises(DataObjectNotFound): entity = get_by_short_code(self.dbm, short_code="ABC", entity_type=["Waterpoint"])
def test_should_return_data_types(self): med_type = DataDictType(self.dbm, name='Medicines', slug='meds', primitive_type='number', description='Number of medications', tags=['med']) med_type.save() doctor_type = DataDictType(self.dbm, name='Doctor', slug='doc', primitive_type='string', description='Name of doctor', tags=['doctor', 'med']) doctor_type.save() facility_type = DataDictType(self.dbm, name='Facility', slug='facility', primitive_type='string', description='Name of facility') facility_type.save() e = Entity(self.dbm, entity_type='foo') e.save() data_record = [('meds', 20, med_type), ('doc', "aroj", doctor_type), ('facility', 'clinic', facility_type)] e.add_data(data_record) # med (tag in list) types = [typ.slug for typ in e.data_types(['med'])] self.assertTrue(med_type.slug in types) self.assertTrue(doctor_type.slug in types) self.assertTrue(facility_type.slug not in types) # doctor (tag as string) types = [typ.slug for typ in e.data_types('doctor')] self.assertTrue(doctor_type.slug in types) self.assertTrue(med_type.slug not in types) self.assertTrue(facility_type.slug not in types) # med and doctor (more than one tag) types = [typ.slug for typ in e.data_types(['med', 'doctor'])] self.assertTrue(doctor_type.slug in types) self.assertTrue(med_type.slug not in types) self.assertTrue(facility_type.slug not in types) # no tags types = [typ.slug for typ in e.data_types()] self.assertTrue(med_type.slug in types) self.assertTrue(doctor_type.slug in types) self.assertTrue(facility_type.slug in types)
def test_create_entity_with_id(self): e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Pune"], id="-1000") uuid = e.save() self.assertEqual(uuid, "-1000") self.dbm.delete(e)
def CreateSchool(self, data, id, short_code): school = Entity(self.manager, self.entity_type, data, id=id, short_code=short_code) school_id = school.save() school.add_data(data=[("SchoolID", id, self.default_ddtype), ("SchollName", short_code, self.default_ddtype), ("SchoolLocation", data, self.default_ddtype)]) return school_id
def test_should_add_passed_in_hierarchy_path_on_create(self): e = Entity(self.dbm, entity_type=["HealthFacility", "Clinic"], location=["India", "MH", "Pune"], aggregation_paths={"org": ["TW_Global", "TW_India", "TW_Pune"], "levels": ["Lead Consultant", "Sr. Consultant", "Consultant"]}) uuid = e.save() saved = get(self.dbm, uuid) hpath = saved._doc.aggregation_paths self.assertEqual(hpath["org"], ["TW_Global", "TW_India", "TW_Pune"]) self.assertEqual(hpath["levels"], ["Lead Consultant", "Sr. Consultant", "Consultant"])
def test_get_entities(self): e2 = Entity(self.dbm, "hospital", ["India", "TN", "Chennai"]) id2 = e2.save() entities = get_entities(self.dbm, [self.uuid, id2]) self.assertEqual(len(entities), 2) saved = dict([(e.id, e) for e in entities]) self.assertEqual(saved[id2].type_string, "hospital") self.assertEqual(saved[self.uuid].type_string, "clinic") self.dbm.delete(e2)
def test_get_entities(self): e2 = Entity(self.dbm, "hospital", ["India", "TN", "Chennai"]) id2 = e2.save() entities = get_entities(self.dbm, [self.uuid, id2]) self.assertEqual(len(entities), 2) saved = dict([(e.id, e) for e in entities]) self.assertEqual(saved[id2].type_string, "hospital") self.assertEqual(saved[self.uuid].type_string, "clinic") self.dbm.delete(e2)
def delete_and_create_entity_instance(manager, ENTITY_TYPE, location, short_code): try: entity = get_by_short_code(dbm=manager, short_code=short_code,entity_type=ENTITY_TYPE) entity.delete() except DataObjectNotFound: pass e = Entity(manager, entity_type=ENTITY_TYPE, location=location, short_code=short_code) id1 = e.save() return e, id1
def test_invalidate_data(self): e = Entity(self.dbm, entity_type='store', location=['nyc']) e.save() apple_type = DataDictType(self.dbm, name='Apples', slug='apples', primitive_type='number') orange_type = DataDictType(self.dbm, name='Oranges', slug='oranges', primitive_type='number') apple_type.save() orange_type.save() data = e.add_data([('apples', 20, apple_type), ('oranges', 30, orange_type)]) valid_doc = self.dbm._load_document(data) self.assertFalse(valid_doc.void) e.invalidate_data(data) invalid_doc = self.dbm._load_document(data) self.assertTrue(invalid_doc.void)
def test_invalidate_entity(self): e = Entity(self.dbm, entity_type='store', location=['nyc']) e.save() self.assertFalse(e._doc.void) apple_type = DataDictType(self.dbm, name='Apples', slug='apples', primitive_type='number') orange_type = DataDictType(self.dbm, name='Oranges', slug='oranges', primitive_type='number') apple_type.save() orange_type.save() data = [ [('apples', 20, apple_type), ('oranges', 30, orange_type)], [('apples', 10, apple_type), ('oranges', 20, orange_type)] ] data_ids = [] for d in data: id = e.add_data(d) self.assertFalse(self.dbm._load_document(id).void) data_ids.append(id) e.invalidate() self.assertTrue(e._doc.void) for id in data_ids: self.assertTrue(self.dbm._load_document(id).void)
def setUp(self): self.dbm = get_db_manager(database='mangrove-test') define_type(self.dbm, ["Reporter"]) reporter = Entity(self.dbm, entity_type=["Reporter"], location=["Pune", "India"], short_code="REPX") reporter.save() reporter = Entity(self.dbm, entity_type=["Reporter"], location=["Pune", "India"], short_code="REP1") reporter.save() reporter = Entity(self.dbm, entity_type=["Reporter"], location=["Pune", "India"], short_code="REP2") reporter.save()
def test_should_add_passed_in_hierarchy_path_on_create(self): e = Entity(self.dbm, entity_type=["HealthFacility", "Clinic"], location=["India", "MH", "Pune"], aggregation_paths={ "org": ["TW_Global", "TW_India", "TW_Pune"], "levels": ["Lead Consultant", "Sr. Consultant", "Consultant"] }) uuid = e.save() saved = get(self.dbm, uuid) hpath = saved._doc.aggregation_paths self.assertEqual(hpath["org"], ["TW_Global", "TW_India", "TW_Pune"]) self.assertEqual(hpath["levels"], ["Lead Consultant", "Sr. Consultant", "Consultant"])
def test_should_get_all_entities(self): e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Mumbai"], short_code='cli002') e.save() e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Jalgaon"], short_code='cli003') e.save() e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Nasik"], short_code='cli004') uuid = e.save() all_entities = get_all_entities(self.dbm) self.assertEqual(4, len(all_entities)) self.assertEqual([uuid], [e['id'] for e in all_entities if e['id'] == uuid])
def test_get_associated_data_senders(self): entity = Entity(self.manager, entity_type=["reporter"], short_code="rep1") entity_id = entity.save() project = Project(dbm=self.manager, name="TestDS", goals="Testing", devices=['web'], form_code="ds_form", fields=[]) project.data_senders = ["rep1"] project.save() result = project.get_associated_datasenders(self.manager) self.assertEquals(result[0].short_code, entity.short_code) self.assertEquals(result[0].id, entity_id)
def setUp(self): self.dbm = get_db_manager(database='mangrove-test') define_type(self.dbm, ["Reporter"]) reporter = Entity(self.dbm, entity_type=["Reporter"], location=["Pune", "India"], short_code="REPX") reporter.save() reporter = Entity(self.dbm, entity_type=["Reporter"], location=["Pune", "India"], short_code="REP1") reporter.save() reporter = Entity(self.dbm, entity_type=["Reporter"], location=["Pune", "India"], short_code="REP2") reporter.save()
def test_create_entity(self): e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Pune"]) uuid = e.save() self.assertTrue(uuid) self.dbm.delete(e)
def test_should_return_empty_list_if_location_path_is_not_stored(self): e = Entity(self.dbm, entity_type="clinic") uuid = e.save() saved = get(self.dbm, uuid) self.assertEqual(saved.location_path, [])
def test_should_add_location_hierarchy_on_create(self): e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Pune"]) uuid = e.save() saved = get(self.dbm, uuid) self.assertEqual(saved.location_path, ["India", "MH", "Pune"])
def test_create_entity_with_id(self): e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Pune"], id="-1000") uuid = e.save() self.assertEqual(uuid, "-1000") self.dbm.delete(e)
locations[(country, state)] = e.save() states[state] = e.id data = [(name_type.slug, state, name_type)] e.add_data(data, event_time=datetime.datetime(2011, 03, 01, tzinfo=UTC)) num_rows += 1 print "[%s]...(%s, %s) -- %s" % (num_rows, country, state, e.id) gr_id = lga_gr_id feature = get_feature_by_id(gr_id) # geometry = feature['geometry'] centroid = json.loads(feature['properties']['geometry_centroid']) e = Entity(dbm, entity_type=["Location", "LGA"], location=[country, state, lga], centroid=centroid, gr_id=gr_id) locations[location] = e.save() geo_id_dict[geo_id] = e data = [(name_type.slug, lga, name_type)] e.add_data(data, event_time=datetime.datetime(2011, 03, 01, tzinfo=UTC)) data = [(geo_id_type.slug, geo_id, geo_id_type)] e.add_data(data, event_time=datetime.datetime(2011, 03, 01, tzinfo=UTC)) num_rows += 1 print "[%s]...(%s, %s, %s) -- %s" % (num_rows, country, state, lga, e.id) if cgs: num_cgs += 1 e.add_data(data=[(cgs_type.slug, cgs, cgs_type)]) print "Countries (%d)" % len(countries) print "States (%d)" % len(states) print "LGAs (%d) (%d as CGS)" % ((len(locations) - len(countries) - len(states)), num_cgs)
def test_should_return_empty_list_if_location_path_is_not_stored(self): e = Entity(self.dbm, entity_type="clinic") uuid = e.save() saved = get(self.dbm, uuid) self.assertEqual(saved.location_path, [])
def _create_entities_and_data_records(self): ENTITY_TYPE = ["Health_Facility", "Clinic"] self.entity_type = ENTITY_TYPE FEB = datetime.datetime(2011, 02, 01, tzinfo=UTC) MARCH = datetime.datetime(2011, 03, 01, tzinfo=UTC) # Entities for State 1: Maharashtra e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'MH', 'Pune']) e.save() e.add_data(data=[("beds", 300, self.dd_types['beds']), ("meds", 20, self.dd_types['meds']), ("director", "Dr. A", self.dd_types['director']), ("patients", 10, self.dd_types['patients'])], event_time=FEB) e.add_data(data=[("beds", 500, self.dd_types['beds']), ("meds", 20, self.dd_types['meds']), ("patients", 20, self.dd_types['patients'])], event_time=MARCH) e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'MH', 'Pune']) e.save() e.add_data(data=[("beds", 100, self.dd_types['beds']), ("meds", 10, self.dd_types['meds']), ("director", "Dr. AA", self.dd_types['director']), ("patients", 50, self.dd_types['patients'])], event_time=FEB) e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 20, self.dd_types['meds']), ("patients", 20, self.dd_types['patients'])], event_time=MARCH) e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'MH', 'Mumbai']) e.save() e.add_data(data=[("beds", 100, self.dd_types['beds']), ("meds", 10, self.dd_types['meds']), ("director", "Dr. AAA", self.dd_types['director']), ("patients", 50, self.dd_types['patients'])], event_time=FEB) e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 20, self.dd_types['meds']), ("patients", 50, self.dd_types['patients'])], event_time=MARCH) # Entities for State 2: karnataka e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'Karnataka', 'Bangalore']) e.save() e.add_data(data=[("beds", 100, self.dd_types['beds']), ("meds", 250, self.dd_types['meds']), ("director", "Dr. B1", self.dd_types['director']), ("patients", 50, self.dd_types['patients'])], event_time=FEB) e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 400, self.dd_types['meds']), ("director", "Dr. B2", self.dd_types['director']), ("patients", 20, self.dd_types['patients'])], event_time=MARCH) e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'Karnataka', 'Hubli']) e.save() e.add_data(data=[("beds", 100, self.dd_types['beds']), ("meds", 250, self.dd_types['meds']), ("director", "Dr. B1", self.dd_types['director']), ("patients", 50, self.dd_types['patients'])], event_time=FEB) e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 400, self.dd_types['meds']), ("director", "Dr. B2", self.dd_types['director']), ("patients", 20, self.dd_types['patients'])], event_time=MARCH) # Entities for State 3: Kerala e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'Kerala', 'Kochi']) e.save() e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 50, self.dd_types['meds']), ("director", "Dr. C", self.dd_types['director']), ("patients", 12, self.dd_types['patients'])], event_time=MARCH)
def setUp(self): self.dbm = get_db_manager(database='mangrove-test') e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Pune"]) self.uuid = e.save()
def setUp(self): self.dbm = get_db_manager(database='mangrove-test') e = Entity(self.dbm, entity_type="clinic", location=["India", "MH", "Pune"]) self.uuid = e.save()
03, 01, tzinfo=UTC)) num_rows += 1 print "[%s]...(%s, %s) -- %s" % (num_rows, country, state, e.id) gr_id = lga_gr_id feature = get_feature_by_id(gr_id) # geometry = feature['geometry'] centroid = json.loads(feature['properties']['geometry_centroid']) e = Entity(dbm, entity_type=["Location", "LGA"], location=[country, state, lga], centroid=centroid, gr_id=gr_id) locations[location] = e.save() geo_id_dict[geo_id] = e data = [(name_type.slug, lga, name_type)] e.add_data(data, event_time=datetime.datetime(2011, 03, 01, tzinfo=UTC)) data = [(geo_id_type.slug, geo_id, geo_id_type)] e.add_data(data, event_time=datetime.datetime(2011, 03, 01, tzinfo=UTC)) num_rows += 1 print "[%s]...(%s, %s, %s) -- %s" % (num_rows, country, state, lga, e.id) if cgs: num_cgs += 1 e.add_data(data=[(cgs_type.slug, cgs, cgs_type)])
def _import_data(self, server, database, max_facilities_to_import=DEFAULT_FACILITIES_TO_IMPORT, max_mdg_to_import=DEFAULT_MDG_TO_IMPORT): from mangrove.datastore.database import DatabaseManager from mangrove.datastore.entity import Entity, get_entities_by_value from mangrove.datastore.datadict import DataDictType, get_datadict_type, create_datadict_type from mangrove.utils import GoogleSpreadsheetsClient from mangrove.utils.google_spreadsheets import get_string, get_number, get_boolean, get_list from mangrove.utils.spreadsheets import CsvReader from mangrove.utils.helpers import slugify from mangrove.georegistry.api import get_feature_by_id import os import datetime import json from pytz import UTC print "Loading 'NIMS Data'..." print "\tServer: %s" % server print "\tDatabase: %s" % database dbm = DatabaseManager(server=server, database=database) user_spreadsheets = GoogleSpreadsheetsClient(settings.GMAIL_USERNAME, settings.GMAIL_PASSWORD) nims_data = user_spreadsheets['NIMS Data Deux'] load_population = True load_other = True load_mdg = True load_health = True load_water = True load_education = True countries = {} states = {} locations = {} num_cgs = 0 datadict_types = {} geo_id_dict = {} cgs_type = create_datadict_type(dbm, slug='cgs', name='CGS', primitive_type='boolean') datadict_types['cgs'] = cgs_type.id geo_id_type = create_datadict_type(dbm, slug='geo_id', name='Geographic ID', primitive_type='string') datadict_types['geo_id'] = geo_id_type.id name_type = create_datadict_type(dbm, slug='name', name='Name', primitive_type='string') datadict_types['name'] = name_type.id mdg_type = create_datadict_type(dbm, slug='mdg', name='MDG', primitive_type='string') datadict_types['mdg'] = mdg_type.id country_geo_id = {} for row in nims_data['Nigeria Country ALL']: country_geo_id[row['name']] = row['grid'] state_geo_ids = {} for row in nims_data['Nigeria States ALL']: state_geo_ids[row['name']] = row['grid'] num_rows = 0 print "Importing location entities from 'Nigeria LGAs ALL' worksheet" for row in nims_data['Nigeria LGAs ALL']: country = get_string('country', row) state = get_string('state', row) lga = get_string('lga', row) cgs = get_boolean('cgs', row) geo_id = get_string('geoid', row) lga_gr_id = get_string('grid', row) location = (country, state, lga) if country not in countries: gr_id = country_geo_id[country] feature = get_feature_by_id(gr_id) # geometry = feature['geometry'] centroid = json.loads( feature['properties']['geometry_centroid']) e = Entity(dbm, entity_type=["Location", "Country"], location=[country], centroid=centroid, gr_id=gr_id) locations[(country, )] = e.save() countries[country] = e.id data = [(name_type.slug, country, name_type)] e.add_data(data, event_time=datetime.datetime(2011, 03, 01, tzinfo=UTC)) num_rows += 1 print "[%s]...(%s) -- %s" % (num_rows, country, e.id)
def _create_entities_and_data_records(self): ENTITY_TYPE = ["Health_Facility", "Clinic"] self.entity_type = ENTITY_TYPE FEB = datetime.datetime(2011, 02, 01, tzinfo=UTC) MARCH = datetime.datetime(2011, 03, 01, tzinfo=UTC) # Entities for State 1: Maharashtra e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'MH', 'Pune']) e.save() e.add_data(data=[("beds", 300, self.dd_types['beds']), ("meds", 20, self.dd_types['meds']), ("director", "Dr. A", self.dd_types['director']), ("patients", 10, self.dd_types['patients'])], event_time=FEB) e.add_data(data=[("beds", 500, self.dd_types['beds']), ("meds", 20, self.dd_types['meds']), ("patients", 20, self.dd_types['patients'])], event_time=MARCH) e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'MH', 'Pune']) e.save() e.add_data(data=[("beds", 100, self.dd_types['beds']), ("meds", 10, self.dd_types['meds']), ("director", "Dr. AA", self.dd_types['director']), ("patients", 50, self.dd_types['patients'])], event_time=FEB) e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 20, self.dd_types['meds']), ("patients", 20, self.dd_types['patients'])], event_time=MARCH) e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'MH', 'Mumbai']) e.save() e.add_data(data=[("beds", 100, self.dd_types['beds']), ("meds", 10, self.dd_types['meds']), ("director", "Dr. AAA", self.dd_types['director']), ("patients", 50, self.dd_types['patients'])], event_time=FEB) e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 20, self.dd_types['meds']), ("patients", 50, self.dd_types['patients'])], event_time=MARCH) # Entities for State 2: karnataka e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'Karnataka', 'Bangalore']) e.save() e.add_data(data=[("beds", 100, self.dd_types['beds']), ("meds", 250, self.dd_types['meds']), ("director", "Dr. B1", self.dd_types['director']), ("patients", 50, self.dd_types['patients'])], event_time=FEB) e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 400, self.dd_types['meds']), ("director", "Dr. B2", self.dd_types['director']), ("patients", 20, self.dd_types['patients'])], event_time=MARCH) e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'Karnataka', 'Hubli']) e.save() e.add_data(data=[("beds", 100, self.dd_types['beds']), ("meds", 250, self.dd_types['meds']), ("director", "Dr. B1", self.dd_types['director']), ("patients", 50, self.dd_types['patients'])], event_time=FEB) e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 400, self.dd_types['meds']), ("director", "Dr. B2", self.dd_types['director']), ("patients", 20, self.dd_types['patients'])], event_time=MARCH) # Entities for State 3: Kerala e = Entity(self.manager, entity_type=ENTITY_TYPE, location=['India', 'Kerala', 'Kochi']) e.save() e.add_data(data=[("beds", 200, self.dd_types['beds']), ("meds", 50, self.dd_types['meds']), ("director", "Dr. C", self.dd_types['director']), ("patients", 12, self.dd_types['patients'])], event_time=MARCH)
def CreateReporter(self): reporter = Entity(self.manager, location=["China", "Beijing"], short_code="rpt_bj", id="Reporter001", entity_type=self.reporter_type) reporter.add_data(data=[(MOBILE_NUMBER_FIELD, "1234567890", self.phone_number_type),(NAME_FIELD, "001s", self.first_name_type)]) reporter_id = reporter.save() return reporter_id, reporter
def test_should_add_entity_type_on_create(self): e = Entity(self.dbm, entity_type=["healthfacility", "clinic"]) uuid = e.save() saved = get(self.dbm, uuid) self.assertEqual(saved.type_path, ["healthfacility", "clinic"])
def test_should_add_entity_type_on_create_as_aggregation_tree(self): e = Entity(self.dbm, entity_type="health_facility") uuid = e.save() saved = get(self.dbm, uuid) self.assertEqual(saved.type_path, ["health_facility"])
def test_should_add_entity_type_on_create_as_aggregation_tree(self): e = Entity(self.dbm, entity_type="health_facility") uuid = e.save() saved = get(self.dbm, uuid) self.assertEqual(saved.type_path, ["health_facility"])
def test_should_add_entity_type_on_create(self): e = Entity(self.dbm, entity_type=["healthfacility", "clinic"]) uuid = e.save() saved = get(self.dbm, uuid) self.assertEqual(saved.type_path, ["healthfacility", "clinic"])
def create_reporter(self): r = Entity(self.manager, entity_type=["reporter"], short_code='ashwin') r.add_data((('mobile_number', '2998288332'), ('name', 'ashwin'))) r.save() return r
def _import_data(self, server, database, max_facilities_to_import=DEFAULT_FACILITIES_TO_IMPORT, max_mdg_to_import=DEFAULT_MDG_TO_IMPORT): from mangrove.datastore.database import DatabaseManager from mangrove.datastore.entity import Entity, get_entities_by_value from mangrove.datastore.datadict import DataDictType, get_datadict_type, create_datadict_type from mangrove.utils import GoogleSpreadsheetsClient from mangrove.utils.google_spreadsheets import get_string, get_number, get_boolean, get_list from mangrove.utils.spreadsheets import CsvReader from mangrove.utils.helpers import slugify from mangrove.georegistry.api import get_feature_by_id import os import datetime import json from pytz import UTC print "Loading 'NIMS Data'..." print "\tServer: %s" % server print "\tDatabase: %s" % database dbm = DatabaseManager(server=server, database=database) user_spreadsheets = GoogleSpreadsheetsClient(settings.GMAIL_USERNAME, settings.GMAIL_PASSWORD) nims_data = user_spreadsheets['NIMS Data Deux'] load_population = True load_other = True load_mdg = True load_health = True load_water = True load_education = True countries = {} states = {} locations = {} num_cgs = 0 datadict_types = {} geo_id_dict = {} cgs_type = create_datadict_type( dbm, slug='cgs', name='CGS', primitive_type='boolean' ) datadict_types['cgs'] = cgs_type.id geo_id_type = create_datadict_type( dbm, slug='geo_id', name='Geographic ID', primitive_type='string' ) datadict_types['geo_id'] = geo_id_type.id name_type = create_datadict_type( dbm, slug='name', name='Name', primitive_type='string' ) datadict_types['name'] = name_type.id mdg_type = create_datadict_type( dbm, slug='mdg', name='MDG', primitive_type='string' ) datadict_types['mdg'] = mdg_type.id country_geo_id = {} for row in nims_data['Nigeria Country ALL']: country_geo_id[row['name']] = row['grid'] state_geo_ids = {} for row in nims_data['Nigeria States ALL']: state_geo_ids[row['name']] = row['grid'] num_rows = 0 print "Importing location entities from 'Nigeria LGAs ALL' worksheet" for row in nims_data['Nigeria LGAs ALL']: country = get_string('country', row) state = get_string('state', row) lga = get_string('lga', row) cgs = get_boolean('cgs', row) geo_id = get_string('geoid', row) lga_gr_id = get_string('grid', row) location = (country, state, lga) if country not in countries: gr_id = country_geo_id[country] feature = get_feature_by_id(gr_id) # geometry = feature['geometry'] centroid = json.loads(feature['properties']['geometry_centroid']) e = Entity(dbm, entity_type=["Location", "Country"], location=[country], centroid=centroid, gr_id=gr_id) locations[(country,)] = e.save() countries[country] = e.id data = [(name_type.slug, country, name_type)] e.add_data(data, event_time=datetime.datetime(2011, 03, 01, tzinfo=UTC)) num_rows += 1 print "[%s]...(%s) -- %s" % (num_rows, country, e.id)