for contact in instance_1_contacts:
        if contact.name == "":
            contact.name = None
    for contact in instance_2_contacts:
        if contact.name == "":
            contact.name = None

    # Update contacts present in instance 1 but not in instance 2
    urns_unique_to_instance_1 = instance_1_contacts_lut.keys(
    ) - instance_2_contacts_lut.keys()
    for i, urn in enumerate(urns_unique_to_instance_1):
        contact = instance_1_contacts_lut[urn]
        log.info(
            f"Adding new contacts to instance 2: {i + 1}/{len(urns_unique_to_instance_1)} "
            f"(Rapid Pro UUID '{contact.uuid}' in instance 1)")
        instance_2.update_contact(contact.urns[0], contact.name,
                                  contact.fields)

    # Update contacts present in instance 2 but not in instance 1
    urns_unique_to_instance_2 = instance_2_contacts_lut.keys(
    ) - instance_1_contacts_lut.keys()
    for i, urn in enumerate(urns_unique_to_instance_2):
        contact = instance_2_contacts_lut[urn]
        log.info(
            f"Adding new contacts to instance 1: {i + 1}/{len(urns_unique_to_instance_2)} "
            f"(Rapid Pro UUID '{contact.uuid}' in instance 2)")
        instance_1.update_contact(contact.urns[0], contact.name,
                                  contact.fields)

    # Update contacts present in both instances
    urns_in_both_instances = instance_1_contacts_lut.keys(
    ) & instance_2_contacts_lut.keys()
Example #2
0
    # 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
    updated_count = 0
    for i, contact in enumerate(contacts):
        log.debug(f"Updating contact {i + 1}/{len(contacts)}...")
        if len(contact.urns) != 1:
            log.warning(
                f"Found a contact in the source instance with multiple URNS. "
                f"The RapidPro UUID is '{contact.uuid}'")
            multiple_urns_count += 1
            continue
        if contact.urns[0].startswith(
                "tel:") and not contact.urns[0].startswith("tel:+"):
            log.warning(
                f"Found a contact in the source instance with a telephone number that has no country "
                f"code; skipping. The RapidPro UUID is '{contact.uuid}'")
            telephone_with_no_country_code_count += 1
            continue
        if contact.name == "":
            contact.name = None
        target_instance.update_contact(contact.urns[0], contact.name,
                                       contact.fields)
        updated_count += 1

    log.info(
        f"Done. Copied {updated_count} contacts. Failed to copy {multiple_urns_count} contacts with multiple "
        f"URNS, and {telephone_with_no_country_code_count} contacts with a telephone number but no country code"
    )
Example #3
0
                contact.fields[field] = None

    # Update contacts present in workspace 1 but not in workspace 2
    new_contacts_in_workspace_2 = 0
    if workspaces_to_update in {"2", "both"}:
        urns_unique_to_workspace_1 = workspace_1_contacts_lut.keys(
        ) - workspace_2_contacts_lut.keys()
        for i, urn in enumerate(urns_unique_to_workspace_1):
            contact = workspace_1_contacts_lut[urn]
            log.info(
                f"Adding new contacts to {workspace_2_name}: {i + 1}/{len(urns_unique_to_workspace_1)} "
                f"(Rapid Pro UUID '{contact.uuid}' in {workspace_1_name})")
            new_contacts_in_workspace_2 += 1
            if dry_run:
                continue
            workspace_2.update_contact(contact.urns[0], contact.name,
                                       contact.fields)

    # Update contacts present in workspace 2 but not in workspace 1
    new_contacts_in_workspace_1 = 0
    if workspaces_to_update in {"1", "both"}:
        urns_unique_to_workspace_2 = workspace_2_contacts_lut.keys(
        ) - workspace_1_contacts_lut.keys()
        for i, urn in enumerate(urns_unique_to_workspace_2):
            contact = workspace_2_contacts_lut[urn]
            log.info(
                f"Adding new contacts to {workspace_1_name}: {i + 1}/{len(urns_unique_to_workspace_2)} "
                f"(Rapid Pro UUID '{contact.uuid}' in {workspace_2_name})")
            new_contacts_in_workspace_1 += 1
            if dry_run:
                continue
            workspace_1.update_contact(contact.urns[0], contact.name,