def setUp(self): self.domain = Domain.get_by_name(TEST_DOMAIN) self.loc1 = Location.by_site_code(TEST_DOMAIN, 'loc1') self.loc2 = Location.by_site_code(TEST_DOMAIN, 'loc2') self.dis = Location.by_site_code(TEST_DOMAIN, 'dis1') self.user_fac1 = CommCareUser.get_by_username('stella') self.user_fac2 = CommCareUser.get_by_username('bella') self.user_dis = CommCareUser.get_by_username('trella') self.msd_user = CommCareUser.get_by_username('msd_person')
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))
def _create_stock_state(self, product, consumption): xform = XFormInstance.get('test-xform') loc = Location.by_site_code(TEST_DOMAIN, 'garms') now = datetime.datetime.utcnow() report = StockReport(form_id=xform._id, date=(now - datetime.timedelta(days=10)).replace( second=0, microsecond=0), type='balance', domain=TEST_DOMAIN) report.save() stock_transaction = StockTransaction( case_id=loc.linked_supply_point().get_id, product_id=product.get_id, sql_product=SQLProduct.objects.get(product_id=product.get_id), section_id='stock', type='stockonhand', stock_on_hand=2 * consumption, report=report) stock_transaction.save() report = StockReport(form_id=xform._id, date=now.replace(second=0, microsecond=0), type='balance', domain=TEST_DOMAIN) report.save() stock_transaction = StockTransaction( case_id=loc.linked_supply_point().get_id, product_id=product.get_id, sql_product=SQLProduct.objects.get(product_id=product.get_id), section_id='stock', type='stockonhand', stock_on_hand=consumption, report=report) stock_transaction.save()
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) else: case = None return CaseLocationTuple(case=case, location=location)
def tearDownClass(cls): MobileBackend.load_by_name(TEST_DOMAIN, TEST_BACKEND).delete() CommCareUser.get_by_username('stella').delete() CommCareUser.get_by_username('bella').delete() CommCareUser.get_by_username('trella').delete() CommCareUser.get_by_username('msd_person').delete() for product in Product.by_domain(TEST_DOMAIN): product.delete() SQLProduct.objects.all().delete() ILSGatewayConfig.for_domain(TEST_DOMAIN).delete() DocDomainMapping.objects.all().delete() Location.by_site_code(TEST_DOMAIN, 'loc1').delete() Location.by_site_code(TEST_DOMAIN, 'loc2').delete() Location.by_site_code(TEST_DOMAIN, 'dis1').delete() Location.by_site_code(TEST_DOMAIN, 'reg1').delete() Location.by_site_code(TEST_DOMAIN, 'moh1').delete() SQLLocation.objects.all().delete() generator.delete_all_subscriptions() Domain.get_by_name(TEST_DOMAIN).delete()
def _process_parent_site_code(parent_site_code, domain, location_type, parent_child_map): if not parent_site_code: return None parent_obj = Location.by_site_code(domain, parent_site_code.lower()) if parent_obj: if invalid_location_type(location_type, parent_obj, parent_child_map): raise LocationImportError( _('Invalid parent type of {0} for child type {1}').format( parent_obj.location_type, location_type ) ) else: return parent_obj._id else: raise LocationImportError(_('Parent with site code {0} does not exist in this project').format(parent_site_code))
def _process_parent_site_code(parent_site_code, domain, location_type, parent_child_map): if not parent_site_code: return None parent_obj = Location.by_site_code(domain, parent_site_code) if parent_obj: if invalid_location_type(location_type, parent_obj, parent_child_map): raise LocationImportError( _('Invalid parent type of {0} for child type {1}').format( parent_obj.location_type, location_type ) ) else: return parent_obj._id else: raise LocationImportError(_('Parent with id {0} does not exist in this project').format(parent_site_code))
def global_keyword_update(v, text, msg, text_words, open_sessions): outbound_metadata = MessageMetadata( workflow=WORKFLOW_KEYWORD, ) if v.owner_doc_type != 'CommCareUser': send_sms_to_verified_number(v, get_message(MSG_UPDATE_UNRECOGNIZED_ACTION, v), metadata=outbound_metadata) return True if len(text_words) > 1: keyword = text_words[1] if keyword.upper() == LOCATION_KEYWORD: site_code = text_words[2:] if not site_code: send_sms_to_verified_number(v, get_message(MSG_UPDATE_LOCATION_SYNTAX, v), metadata=outbound_metadata) return True site_code = site_code[0].lower() location = Location.by_site_code(v.domain, site_code) if location: v.owner.set_location(location) send_sms_to_verified_number( v, get_message(MSG_UPDATE_LOCATION_SUCCESS), metadata=outbound_metadata ) return True else: send_sms_to_verified_number( v, get_message(MSG_UPDATE_LOCATION_SITE_CODE_NOT_FOUND, v, context=[site_code]), metadata=outbound_metadata ) return True else: send_sms_to_verified_number( v, get_message(MSG_UPDATE_UNRECOGNIZED_ACTION, v, (keyword,)), metadata=outbound_metadata ) else: send_sms_to_verified_number(v, get_message(MSG_UPDATE, v), metadata=outbound_metadata) return True
def _create_stock_state(self, product, consumption): xform = XFormInstance.get('test-xform') loc = Location.by_site_code(TEST_DOMAIN, 'garms') now = datetime.datetime.utcnow() report = StockReport( form_id=xform._id, date=(now - datetime.timedelta(days=10)).replace(second=0, microsecond=0), type='balance', domain=TEST_DOMAIN ) report.save() stock_transaction = StockTransaction( case_id=loc.linked_supply_point().get_id, product_id=product.get_id, sql_product=SQLProduct.objects.get(product_id=product.get_id), section_id='stock', type='stockonhand', stock_on_hand=2 * consumption, report=report ) stock_transaction.save()
def tearDownClass(cls): delete_domain_phone_numbers(TEST_DOMAIN) if cls.sms_backend_mapping.id is not None: cls.sms_backend_mapping.delete() if cls.sms_backend.id is not None: cls.sms_backend.delete() for username in [ 'stella', 'bella', 'trella', 'msd_person', ]: user = CommCareUser.get_by_username(username) if user: user.delete() for product in Product.by_domain(TEST_DOMAIN): product.delete() SQLProduct.objects.all().delete() ils_gateway_config = ILSGatewayConfig.for_domain(TEST_DOMAIN) if ils_gateway_config: ils_gateway_config.delete() DocDomainMapping.objects.all().delete() for site_code in [ 'loc1', 'loc2', 'dis1', 'reg1', 'moh1', ]: location = Location.by_site_code(TEST_DOMAIN, site_code) if location: location.delete() SQLLocation.objects.all().delete() generator.delete_all_subscriptions() test_domain = Domain.get_by_name(TEST_DOMAIN) if test_domain: test_domain.delete() super(ILSTestScript, cls).tearDownClass()
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'), ) def _all_locations(domain): return Location.view( 'locations/hierarchy', startkey=[domain], endkey=[domain, {}], reduce=False, include_docs=True ).all() compare( [self.user.location, test_state1, test_state2, test_village1], _all_locations(self.domain.name) ) # 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) )
def _get_facility_location(self, domain, msd_code): return Location.by_site_code(domain, msd_code)
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) )
def test_location_queries(self): test_state1 = make_loc( 'teststate1', type='state', parent=self.user.locations[0] ) test_state2 = make_loc( 'teststate2', type='state', parent=self.user.locations[0] ) test_village1 = make_loc( 'testvillage1', type='village', parent=test_state1 ) test_village1.site_code = 'tv1' test_village1.save() test_village2 = make_loc( 'testvillage2', type='village', parent=test_state2 ) 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.locations[0].descendants ) # children compare( [test_state1, test_state2], self.user.locations[0].children ) # siblings compare( [test_state2], test_state1.siblings() ) # parent and parent_id self.assertEqual( self.user.locations[0]._id, test_state1.parent_id ) self.assertEqual( self.user.locations[0]._id, test_state1.parent._id ) # is_root self.assertTrue(self.user.locations[0].is_root) self.assertFalse(test_state1.is_root) # Location.root_locations compare( [self.user.locations[0]], 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.filter_by_type_count self.assertEqual( 2, Location.filter_by_type_count(self.domain.name, 'village') ) self.assertEqual( 1, Location.filter_by_type_count(self.domain.name, 'village', test_state1) ) # Location.get_in_domain test_village2.domain = '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'), ) # Location.all_locations compare( [self.user.locations[0], test_state1, test_state2, test_village1], Location.all_locations(self.domain.name) ) # 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.locations[0], test_state1, test_state2, test_village1], Location.by_domain(self.domain.name) )
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.get_children() ) # parent and parent_location_id self.assertEqual( self.user.location.location_id, test_state1.parent_location_id ) self.assertEqual( self.user.location.location_id, test_state1.parent._id ) # 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) ) create_domain('rejected') bootstrap_location_types('rejected') test_village2.domain = 'rejected' test_village2.save() self.assertEqual( {loc.location_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) )
def import_location(domain, location_type, location_data, parent_child_map=None): data = dict(location_data) provided_code = data.pop('site_code', None) parent_site_code = data.pop('parent_site_code', None) if not parent_child_map: parent_child_map = parent_child(domain) form_data = {} try: parent_id = _process_parent_site_code( parent_site_code, domain, location_type, parent_child_map ) except LocationImportError as e: return { 'id': None, 'message': _('Unable to import location {0}: {1}').format( data.pop('name'), e ) } existing = None parent = parent_id if provided_code: existing = Location.by_site_code(domain, provided_code) if existing: if existing.location_type != location_type: return { 'id': None, 'message': _("Existing location type error, type of {0} is not {1}").format( existing.name, location_type ) } parent = parent_id or existing.parent_id form_data['site_code'] = provided_code form_data['parent_id'] = parent form_data['name'] = data.pop('name') form_data['location_type'] = location_type lat, lon = data.pop('latitude', None), data.pop('longitude', None) if lat and lon: form_data['coordinates'] = '%s, %s' % (lat, lon) properties = {} consumption = [] for k, v in data.iteritems(): if k.startswith('default_'): consumption.append((k[8:], v)) else: properties[(location_type, k)] = v return submit_form( domain, parent, form_data, properties, existing, location_type, consumption )
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)