(stdout=None)
Removes contacts from Hubspot that are members of blocked domains.
    :param stdout: the stdout of a management command (if applicable)
Removes contacts from Hubspot that are members of blocked domains.
    :param stdout: the stdout of a management command (if applicable)
Removes contacts from Hubspot that are members of blocked domains . : param stdout : the stdout of a management command ( if applicable )
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,
                }
            )
def remove_blocked_domain_contacts_from_hubspot ( stdout = None ) : 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 , } )