def update_ai_locations(type_id, username='', password=''): client = ActivityInfoClient(username, password) updated_location = 0 for location in ai.locations.find({'ai_name': {'$regex': 'PG'}}): payload = { 'id': int(random.getrandbits(31)), 'locationTypeId': type_id, 'name': location['ai_name'], 'axe': '{}'.format(location['p_code']), 'latitude': location['latitude'], 'longitude': location['longitude'], 'workflowstatusid': 'validated' } for id, level in location['adminEntities'].items(): payload['E{}'.format(id)] = level['id'] response = client.call_command('CreateLocation', **payload) if response.status_code == requests.codes.ok: updated_location += 1 print 'Uploaded {}'.format(location['ai_name'].encode('UTF-8')) else: print 'Error for: {}'.format(location['ai_name'].encode('UTF-8')) print updated_location
def update_ai_locations(type_id, username='', password=''): client = ActivityInfoClient(username, password) updated_location = 0 for location in ai.locations.find({'ai_name': {'$regex': 'PG'}}): payload = { 'id': int(random.getrandbits(31)), 'locationTypeId': type_id, 'name': location['ai_name'], 'axe': '{}'.format(location['p_code']), 'latitude': location['latitude'], 'longitude': location['longitude'], 'workflowstatusid': 'validated' } for id, level in location['adminEntities'].items(): payload['E{}'.format(id)] = level['id'] response = client.call_command('CreateLocation', **payload) if response.status_code == requests.codes.ok: updated_location += 1 print 'Uploaded {}'.format(location['ai_name'].encode('UTF-8')) else: print 'Error for: {}'.format(location['ai_name'].encode('UTF-8')) print updated_location
def update_sites(api_key='', domain='', username='', password='', list_name='', site_type='', name_col='', code_col='', target_list=''): carto_client = CartoDBAPIKey(api_key, domain) ai_client = ActivityInfoClient(username, password) # create an index of sites by p_code existing = dict( (site['code'], dict(site, index=i)) for (i, site) in enumerate(ai_client.get_locations(target_list)) if 'code' in site) sites = carto_client.sql('select * from {}'.format(list_name)) send_message('Starting upload of {}'.format(list_name)) bad_codes = [] updated_sites = 0 for row in sites['rows']: p_code = str(row[code_col]).strip() site_name = row[name_col].encode('UTF-8') cad = ai['Cadastral Area'].find_one({'code': str(row['cad_code'])}) if cad is None: bad_codes.append(row['cad_code']) continue caz = ai['Caza'].find_one({'id': cad['parentId']}) gov = ai['Governorate'].find_one({'id': caz['parentId']}) if p_code not in existing and site_name: payload = dict(id=int(random.getrandbits(31)), locationTypeId=int(target_list), name='{}: {}'.format(site_type, site_name)[0:40], axe='{}'.format(p_code), latitude=row['latitude'], longitude=row['longitude'], workflowstatusid='validated') payload['E{}'.format(gov['levelId'])] = gov['id'] payload['E{}'.format(caz['levelId'])] = caz['id'] payload['E{}'.format(cad['levelId'])] = cad['id'] response = ai_client.call_command('CreateLocation', **payload) if response.status_code == requests.codes.no_content: updated_sites += 1 print 'Updated {}'.format(payload['name']) else: print 'Error for {}'.format(payload['name']) print 'Bad codes: {}'.format(bad_codes) print 'Updated sites: {}'.format(updated_sites) send_message('Updated {} sites'.format(updated_sites))
def update_ai_locations(type_id, username='', password=''): client = ActivityInfoClient(username, password) for location in ai.locations.find(): payload = { 'id': location['ai_id'], 'locationTypeId': type_id, 'name': location['ai_name'], 'axe': '{}: {}'.format('PCode', location['p_code']), 'latitude': location['latitude'], 'longitude': location['longitude'], 'workflowstatusid': 'validated' } for id, level in location['adminEntities'].items(): payload['E{}'.format(id)] = level['id'] response = client.call_command('CreateLocation', **payload) print response
def update_ai_locations(type_id, username='', password=''): client = ActivityInfoClient(username, password) for location in ai.locations.find(): payload = { 'id': location['ai_id'], 'locationTypeId': type_id, 'name': location['ai_name'], 'axe': '{}: {}'.format('PCode', location['p_code']), 'latitude': location['latitude'], 'longitude': location['longitude'], 'workflowstatusid': 'validated' } for id, level in location['adminEntities'].items(): payload['E{}'.format(id)] = level['id'] response = client.call_command('CreateLocation', **payload) print response
def update_sites( api_key='', domain='', username='', password='', list_name='', site_type='', name_col='', code_col='', target_list='' ): carto_client = CartoDBAPIKey(api_key, domain) ai_client = ActivityInfoClient(username, password) # create an index of sites by p_code existing = dict( (site['code'], dict(site, index=i)) for (i, site) in enumerate( ai_client.get_locations(target_list) ) if 'code' in site ) sites = carto_client.sql( 'select * from {}'.format(list_name) ) send_message('Starting upload of {}'.format(list_name)) bad_codes = [] updated_sites = 0 for row in sites['rows']: p_code = str(row[code_col]).strip() site_name = row[name_col].encode('UTF-8') cad = ai['Cadastral Area'].find_one({'code': str(row['cad_code'])}) if cad is None: bad_codes.append(row['cad_code']) continue caz = ai['Caza'].find_one({'id': cad['parentId']}) gov = ai['Governorate'].find_one({'id': caz['parentId']}) if p_code not in existing and site_name: payload = dict( id=int(random.getrandbits(31)), locationTypeId=int(target_list), name='{}: {}'.format(site_type, site_name)[0:40], axe='{}'.format(p_code), latitude=row['latitude'], longitude=row['longitude'], workflowstatusid='validated' ) payload['E{}'.format(gov['levelId'])] = gov['id'] payload['E{}'.format(caz['levelId'])] = caz['id'] payload['E{}'.format(cad['levelId'])] = cad['id'] response = ai_client.call_command('CreateLocation', **payload) if response.status_code == requests.codes.no_content: updated_sites += 1 print 'Updated {}'.format(payload['name']) else: print 'Error for {}'.format(payload['name']) print 'Bad codes: {}'.format(bad_codes) print 'Updated sites: {}'.format(updated_sites) send_message('Updated {} sites'.format(updated_sites))