# Initialise the two instances log.info("Downloading the access token for instance 1...") instance_1_token = google_cloud_utils.download_blob_to_string( google_cloud_credentials_file_path, instance_1_credentials_url).strip() instance_1 = RapidProClient(instance_1_domain, instance_1_token) log.info("Downloading the target instance access token...") instance_2_token = google_cloud_utils.download_blob_to_string( google_cloud_credentials_file_path, instance_2_credentials_url).strip() instance_2 = RapidProClient(instance_2_domain, instance_2_token) # Synchronise the contact fields log.info("Synchronising contact fields...") instance_1_fields = instance_1.get_fields() instance_2_fields = instance_2.get_fields() for field in instance_1_fields: if field.key not in {f.key for f in instance_2_fields}: instance_2.create_field(field.label) for field in instance_2_fields: if field.key not in {f.key for f in instance_1_fields}: instance_1.create_field(field.label) log.info("Contact fields synchronised") # Synchronise the contacts log.info("Synchronising contacts...") instance_1_contacts = instance_1.get_raw_contacts() instance_2_contacts = instance_2.get_raw_contacts() def filter_valid_contacts(contacts):
# Initialise the source/target instances log.info("Downloading the source instance access token...") source_token = google_cloud_utils.download_blob_to_string( google_cloud_credentials_file_path, source_credentials_url).strip() source_instance = RapidProClient(source_domain, source_token) log.info("Downloading the target instance access token...") target_token = google_cloud_utils.download_blob_to_string( google_cloud_credentials_file_path, target_credentials_url).strip() target_instance = RapidProClient(target_domain, target_token) # For each contact field in the source instance, create a matching contact field in the target instance if it # does not already exist log.info("Copying contact fields...") source_fields = source_instance.get_fields() target_field_keys = {f.key for f in target_instance.get_fields()} for field in source_fields: if field.key not in target_field_keys: target_instance.create_field(field.label) log.info("Contact fields copied") log.info("Fetching all contacts from the source instance...") contacts = source_instance.get_raw_contacts() log.info(f"Fetched {len(contacts)} contacts") log.info("Updating contacts in the target instance...") # Update each contact's name and fields. # Language, groups, blocked, and stopped properties are not touched. multiple_urns_count = 0 telephone_with_no_country_code_count = 0
workspace_1 = RapidProClient(workspace_1_domain, workspace_1_token) workspace_1_name = workspace_1.get_workspace_name() log.info(f"Done. workspace 1 is called {workspace_1_name}") log.info("Downloading the access token for workspace 2...") workspace_2_token = google_cloud_utils.download_blob_to_string( google_cloud_credentials_file_path, workspace_2_credentials_url).strip() workspace_2 = RapidProClient(workspace_2_domain, workspace_2_token) workspace_2_name = workspace_2.get_workspace_name() log.info(f"Done. workspace 2 is called {workspace_2_name}") # Download the data from Rapid Pro log.info("Downloading contact fields...") log.info(f"Downloading all fields from {workspace_1_name}...") workspace_1_fields = workspace_1.get_fields() log.info(f"Downloading all fields from {workspace_2_name}...") workspace_2_fields = workspace_2.get_fields() # Synchronise the contacts log.info("Downloading contacts...") IOUtils.ensure_dirs_exist(raw_data_log_directory) log.info(f"Downloading all contacts from {workspace_1_name}...") with open(f"{raw_data_log_directory}/{workspace_1_name}_raw_contacts.json", "w") as f: workspace_1_contacts = workspace_1.get_raw_contacts( raw_export_log_file=f) log.info(f"Downloading all contacts from {workspace_2_name}...") with open(f"{raw_data_log_directory}/{workspace_2_name}_raw_contacts.json", "w") as f: workspace_2_contacts = workspace_2.get_raw_contacts(