def bootstrap_user(loc, username=TEST_USER, domain=TEST_DOMAIN, phone_number=TEST_NUMBER, password=TEST_PASSWORD, backend=TEST_BACKEND, first_name='', last_name='', home_loc=None, user_data=None, language=None ): user_data = user_data or {} user = CommCareUser.create( domain, username, password, phone_numbers=[phone_number], user_data=user_data, first_name=first_name, last_name=last_name ) if language: user.language = language if home_loc == loc.site_code: interface = SupplyInterface(domain) if not interface.get_by_location(loc): interface.create_from_location(domain, loc) user.set_location(loc) user.save_verified_number(domain, phone_number, verified=True, backend_id=backend) user.save() return CommCareUser.wrap(user.to_json())
def _create_supply_point_from_location(self, supply_point, location): interface = SupplyInterface(location.domain) if not interface.get_by_location(location): return interface.get_or_create_by_location(Loc(_id=location.get_id, name=supply_point.name, external_id=str(supply_point.id), domain=self.domain))
def test_delete_closes_sp_cases(self): accessor = SupplyInterface(self.domain) loc = make_loc('ghost_nation', domain=self.domain).sql_location sp = loc.linked_supply_point() self.assertFalse(sp.closed) loc.full_delete() sp = accessor.get_supply_point(sp.case_id) self.assertTrue(sp.closed)
def _reopen_or_create_supply_point(location): from .helpers import update_supply_point_from_location supply_point = SupplyInterface(location.domain).get_closed_and_open_by_location_id_and_domain( location.domain, location.location_id ) if supply_point: if supply_point and supply_point.closed: transactions = supply_point.get_closing_transactions() for transaction in transactions: transaction.form.archive(user_id=const.COMMTRACK_USERNAME) update_supply_point_from_location(supply_point, location) return supply_point else: return SupplyInterface.create_from_location(location.domain, location)
def setUp(self): super(SupplyPointDBAccessorsTest, self).setUp() self.domain = 'supply-point-dbaccessors' self.project = bootstrap_domain(self.domain) self.interface = SupplyInterface(self.domain) self.locations = [ make_loc('1234', name='ben', domain=self.domain), make_loc('1235', name='ben', domain=self.domain), make_loc('1236', name='ben', domain=self.domain), ]
def _reopen_or_create_supply_point(location): from .dbaccessors import get_supply_point_by_location_id supply_point = get_supply_point_by_location_id(location.domain, location.location_id) if supply_point: if supply_point and supply_point.closed: for action in supply_point.actions: if action.action_type == 'close': action.xform.archive(user_id=const.COMMTRACK_USERNAME) supply_point.update_from_location(location) return supply_point else: return SupplyInterface.create_from_location(location.domain, location)
def get_consumption(self, loc): if (not self.include_consumption or loc.location_type_name in self.administrative_types or not self.consumption_dict): return {} if loc.location_id in self.supply_point_map: sp_id = self.supply_point_map[loc.location_id] else: # this only happens if the supply point case did # not already exist sp_id = SupplyInterface( self.domain).get_or_create_by_location(loc).location_id return { p.code: get_loaded_default_monthly_consumption( self.consumption_dict, self.domain, p._id, loc.location_type_name, sp_id) or '' for p in self.products }
def get_message_for_location(self, location): supply_point = SupplyInterface( location.domain).get_by_location(location) if not supply_point: return on_time_products, missing_products = report_status( location.sql_location, days_until_late=DAYS_UNTIL_LATE) if not on_time_products: return SECOND_STOCK_ON_HAND_REMINDER, {} elif missing_products: products_names = ', '.join( [product.name for product in missing_products]) return SECOND_INCOMPLETE_SOH_REMINDER, {'products': products_names} return None, {}
def _reopen_or_create_supply_point(location): from .helpers import update_supply_point_from_location from .dbaccessors import get_supply_point_by_location_id supply_point = get_supply_point_by_location_id(location.domain, location.location_id) if supply_point: if supply_point and supply_point.closed: form_ids = CaseAccessors(supply_point.domain).get_case_xform_ids(supply_point.case_id) form_accessor = FormAccessors(supply_point.domain) for form_id in form_ids: form = form_accessor.get_form(form_id) closes_case = any(map( lambda case_update: case_update.closes_case(), get_case_updates(form), )) if closes_case: form.archive(user_id=const.COMMTRACK_USERNAME) update_supply_point_from_location(supply_point, location) return supply_point else: return SupplyInterface.create_from_location(location.domain, location)
def __init__(self, domain, verified_contact, location=None): self.domain = domain self.verified_contact = verified_contact self.location = location self.case = None u = verified_contact.owner if domain.commtrack_enabled: # if user is not actually a user, we let someone else process if not isinstance(u, CouchUser): raise NotAUserClassError if not self.location: self.location = u.location if self.location: self.case = SupplyInterface(domain.name).get_by_location(self.location) self.commtrack_settings = domain.commtrack_settings
def make_supply_point(domain, location): # a supply point is currently just a case with a special type case_id = uuid.uuid4().hex user_id = const.get_commtrack_user_id(domain) owner_id = location.location_id kwargs = { 'external_id': location.external_id } if location.external_id else {} caseblock = CaseBlock(case_id=case_id, create=True, case_name=location.name, user_id=user_id, owner_id=owner_id, case_type=const.SUPPLY_POINT_CASE_TYPE, update={ 'location_id': location.location_id, }, **kwargs) _submit_commtrack_caseblock(domain, caseblock, "make_supply_point") return SupplyInterface(domain).get_supply_point(case_id)
class SupplyPointDBAccessorsTest(TestCase): def setUp(self): super(SupplyPointDBAccessorsTest, self).setUp() self.domain = 'supply-point-dbaccessors' self.project = bootstrap_domain(self.domain) self.interface = SupplyInterface(self.domain) self.locations = [ make_loc('1234', name='ben', domain=self.domain), make_loc('1235', name='ben', domain=self.domain), make_loc('1236', name='ben', domain=self.domain), ] def tearDown(self): for location in self.locations: location.delete() delete_all_cases() self.project.delete() super(SupplyPointDBAccessorsTest, self).tearDown() def test_get_supply_point_ids_in_domain_by_location(self): actual = get_supply_point_ids_in_domain_by_location(self.domain) expected = { location.location_id: location.linked_supply_point().case_id for location in self.locations } self.assertEqual(actual, expected) @run_with_all_backends def test_get_supply_point_by_location_id(self): actual = self.interface.get_closed_and_open_by_location_id_and_domain( self.domain, self.locations[0].location_id ) expected = self.locations[0].linked_supply_point() self.assertEqual(type(actual), type(expected)) self.assertEqual(actual.to_json(), expected.to_json())
def setUp(self): super(LocationsTest, self).setUp() self.accessor = SupplyInterface(self.domain.name) self.user = self.users[0]
class LocationsTest(CommTrackTest): user_definitions = [FIXED_USER] def setUp(self): super(LocationsTest, self).setUp() self.accessor = SupplyInterface(self.domain.name) self.user = self.users[0] @run_with_all_backends def test_sync(self): test_state = make_loc( 'teststate', type='state', parent=self.user.location ) test_village = make_loc( 'testvillage', type='village', parent=test_state ) try: sql_village = SQLLocation.objects.get( name='testvillage', domain=self.domain.name, ) self.assertEqual(sql_village.name, test_village.name) self.assertEqual(sql_village.domain, test_village.domain) except SQLLocation.DoesNotExist: self.fail("Synced SQL object does not exist") @run_with_all_backends 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 ) @run_with_all_backends def test_archive_flips_sp_cases(self): loc = make_loc('someloc') sp = loc.linked_supply_point() self.assertFalse(sp.closed) loc.archive() sp = loc.linked_supply_point() self.assertTrue(sp.closed) loc.unarchive() sp = loc.linked_supply_point() self.assertFalse(sp.closed) @run_with_all_backends 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 ) @run_with_all_backends def test_delete_closes_sp_cases(self): loc = make_loc('test_loc') sp = loc.linked_supply_point() self.assertFalse(sp.closed) loc.full_delete() sp = self.accessor.get_supply_point(sp.case_id) self.assertTrue(sp.closed)
def location_sync(self, ilsgateway_location): def get_or_create_msd_zone(region): msd_name = _get_msd_name(region.name) msd_code = MSDZONE_MAP[msd_name][0] try: sql_msd_loc = SQLLocation.objects.get( domain=self.domain, site_code=msd_code ) msd_location = Loc.get(sql_msd_loc.location_id) except SQLLocation.DoesNotExist: msd_location = Loc(parent=loc_parent) msd_location.domain = self.domain msd_location.name = msd_name msd_location.location_type = 'MSDZONE' msd_location.site_code = MSDZONE_MAP[msd_name][0] msd_location.save() return msd_location try: sql_loc = SQLLocation.objects.get( domain=self.domain, external_id=int(ilsgateway_location.id) ) location = Loc.get(sql_loc.location_id) except SQLLocation.DoesNotExist: location = None except SQLLocation.MultipleObjectsReturned: return if not location: if ilsgateway_location.id in EXCLUDED_REGIONS: return if ilsgateway_location.parent_id: try: sql_loc_parent = SQLLocation.objects.get( domain=self.domain, external_id=ilsgateway_location.parent_id ) loc_parent = sql_loc_parent.couch_location except SQLLocation.DoesNotExist: new_parent = self.endpoint.get_location(ilsgateway_location.parent_id) loc_parent = self.location_sync(Location(new_parent)) if not loc_parent: return if ilsgateway_location.type == 'REGION': location = Loc(parent=get_or_create_msd_zone(ilsgateway_location)) else: location = Loc(parent=loc_parent) else: location = Loc() location.lineage = [] location.domain = self.domain location.name = ilsgateway_location.name if ilsgateway_location.groups: location.metadata = {'group': ilsgateway_location.groups[0]} if ilsgateway_location.latitude: location.latitude = float(ilsgateway_location.latitude) if ilsgateway_location.longitude: location.longitude = float(ilsgateway_location.longitude) location.location_type = ilsgateway_location.type location.site_code = ilsgateway_location.code location.external_id = unicode(ilsgateway_location.id) location.save() interface = SupplyInterface(self.domain) if ilsgateway_location.type == 'FACILITY': if not interface.get_by_location(location): interface.create_from_location(self.domain, location) location.save() else: sql_location = location.sql_location if not sql_location.supply_point_id: location.save() else: location_dict = { 'name': ilsgateway_location.name, 'latitude': float(ilsgateway_location.latitude) if ilsgateway_location.latitude else None, 'longitude': float(ilsgateway_location.longitude) if ilsgateway_location.longitude else None, 'location_type': ilsgateway_location.type, 'site_code': ilsgateway_location.code.lower(), 'external_id': str(ilsgateway_location.id), 'metadata': {} } if ilsgateway_location.groups: location_dict['metadata']['group'] = ilsgateway_location.groups[0] case = SupplyInterface(self.domain).get_by_location(location) if apply_updates(location, location_dict): location.save() if case: update_supply_point_from_location(case, location) else: SupplyInterface.create_from_location(self.domain, location) location_parent = location.parent if ilsgateway_location.type == 'FACILITY' and ilsgateway_location.parent_id and location_parent \ and location_parent.external_id != str(ilsgateway_location.parent_id): new_parent = self.endpoint.get_location(ilsgateway_location.parent_id) new_parent = self.location_sync(Location(new_parent)) location.lineage = get_lineage_from_location_id(new_parent.get_id) location.save() location.previous_parents = [location_parent.get_id] location_edited_receiver(None, location, moved=True) return location
def get_supply_point_and_location(domain, site_code): location = Location.by_site_code(domain, site_code) if location: case = SupplyInterface(domain).get_by_location(location)
class LocationsTest(CommTrackTest): user_definitions = [FIXED_USER] def setUp(self): super(LocationsTest, self).setUp() self.accessor = SupplyInterface(self.domain.name) self.user = self.users[0] @run_with_all_backends def test_sync(self): test_state = make_loc('teststate', type='state', parent=self.user.location) test_village = make_loc('testvillage', type='village', parent=test_state) try: sql_village = SQLLocation.objects.get( name='testvillage', domain=self.domain.name, ) self.assertEqual(sql_village.name, test_village.name) self.assertEqual(sql_village.domain, test_village.domain) except SQLLocation.DoesNotExist: self.fail("Synced SQL object does not exist") @run_with_all_backends 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) @run_with_all_backends def test_archive_flips_sp_cases(self): loc = make_loc('someloc').sql_location sp = loc.linked_supply_point() self.assertFalse(sp.closed) loc.archive() sp = loc.linked_supply_point() self.assertTrue(sp.closed) loc.unarchive() sp = loc.linked_supply_point() self.assertFalse(sp.closed) @run_with_all_backends 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) @run_with_all_backends def test_delete_closes_sp_cases(self): loc = make_loc('test_loc').sql_location sp = loc.linked_supply_point() self.assertFalse(sp.closed) loc.full_delete() sp = self.accessor.get_supply_point(sp.case_id) self.assertTrue(sp.closed)