def cleanup_blocked_hubspot_contacts(): """ Remove any data stored about users from blocked domains and email domains from Hubspot in case it somehow got there. :return: """ if not HUBSPOT_ENABLED: return # First delete any user information from users that are members of # blocked domains blocked_domains = get_blocked_hubspot_domains() for domain in blocked_domains: user_query = UserES().domain(domain).source(['email', 'username']) total_users = user_query.count() chunk_size = 30 # Hubspot recommends fewer than 100 emails per request num_chunks = int(math.ceil(float(total_users) / float(chunk_size))) for chunk in range(num_chunks): blocked_users = (user_query .size(chunk_size) .start(chunk * chunk_size) .run() .hits) blocked_emails = [] for user in blocked_users: username = user.get('username') user_email = user.get('email') blocked_emails.append(username) if user_email and user_email != username: blocked_emails.append(user_email) ids_to_delete = _get_contact_ids_for_emails(set(blocked_emails)) num_deleted = sum(_delete_hubspot_contact(vid) for vid in ids_to_delete) metrics_gauge( 'commcare.hubspot_data.deleted_user.blocked_domain', num_deleted, tags={ 'domain': domain, 'ids_deleted': ids_to_delete, } ) # Next delete any user info from users that have emails or usernames ending # in blocked email-domains blocked_email_domains = get_blocked_hubspot_email_domains() for email_domain in blocked_email_domains: ids_to_delete = _get_contact_ids_for_email_domain(email_domain) num_deleted = sum(_delete_hubspot_contact(vid) for vid in ids_to_delete) metrics_gauge( 'commcare.hubspot_data.deleted_user.blocked_email_domain', num_deleted, tags={ 'email_domain': email_domain, 'ids_deleted': ids_to_delete, } )
def remove_blocked_domain_contacts_from_hubspot(stdout=None): """ Removes contacts from Hubspot that are members of blocked domains. :param stdout: the stdout of a management command (if applicable) """ blocked_domains = get_blocked_hubspot_domains() for domain in blocked_domains: if stdout: stdout.write(f"\n\nChecking DOMAIN {domain}...") user_query = UserES().domain(domain).source(['email', 'username']) total_users = user_query.count() chunk_size = 30 # Hubspot recommends fewer than 100 emails per request num_chunks = int(math.ceil(float(total_users) / float(chunk_size))) for chunk in range(num_chunks): blocked_users = (user_query .size(chunk_size) .start(chunk * chunk_size) .run() .hits) blocked_emails = [] for user in blocked_users: username = user.get('username') user_email = user.get('email') blocked_emails.append(username) if user_email and user_email != username: blocked_emails.append(user_email) ids_to_delete = _get_contact_ids_to_delete(set(blocked_emails)) if stdout: stdout.write(f"Found {len(ids_to_delete)} id(s) to delete.") num_deleted = sum(_delete_hubspot_contact(vid) for vid in ids_to_delete) metrics_counter( 'commcare.hubspot_data.deleted_user.blocked_domain', num_deleted, tags={ 'domain': domain, } )