def get_commcare_users_by_filters(domain, user_filters, count_only=False): """ Returns CommCareUsers in domain per given filters. If user_filters is empty returns all users in the domain args: user_filters: a dict with below structure. {'role_id': <Role ID to filter users by>, 'search_string': <string to search users by username>} kwargs: count_only: If True, returns count of search results """ role_id = user_filters.get('role_id', None) search_string = user_filters.get('search_string', None) query = UserES().domain(domain).mobile_users() if not role_id and not search_string: if count_only: query.count() else: return get_all_commcare_users_by_domain(domain) if role_id: query = query.role_id(role_id) if search_string: query = query.search_string_query( search_string, default_fields=['first_name', 'last_name', 'username']) if count_only: return query.count() user_ids = [u['_id'] for u in query.source(['_id']).run().hits] return map(CommCareUser.wrap, iter_docs(CommCareUser.get_db(), user_ids))
def get_commcare_users_by_filters(domain, user_filters, count_only=False): """ Returns CommCareUsers in domain per given filters. If user_filters is empty returns all users in the domain args: user_filters: a dict with below structure. {'role_id': <Role ID to filter users by>, 'search_string': <string to search users by username>} kwargs: count_only: If True, returns count of search results """ role_id = user_filters.get('role_id', None) search_string = user_filters.get('search_string', None) query = UserES().domain(domain).mobile_users() if not role_id and not search_string: if count_only: query.count() else: return get_all_commcare_users_by_domain(domain) if role_id: query = query.role_id(role_id) if search_string: query = query.search_string_query(search_string, default_fields=['first_name', 'last_name', 'username']) if count_only: return query.count() user_ids = [u['_id'] for u in query.source(['_id']).run().hits] return map(CommCareUser.wrap, iter_docs(CommCareUser.get_db(), user_ids))
def print_domain_summary(self, domain): users_not_blocked = {} 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) users_not_blocked.update( get_first_conversion_status_for_emails( list(set(blocked_emails)))) if users_not_blocked: self.stdout.write( self.style.ERROR( f"\n\nFound {len(users_not_blocked)} users in " f"HubSpot who are members of the project {domain} " f"that is blocking HubSpot data:")) self.stdout.write("\nEmail\tFirst Conversion") for user, status in users_not_blocked.items(): self.stdout.write(f"{user}\t{status}") self.stdout.write('\n\n') else: self.stdout.write( self.style.SUCCESS( f"All users in project {domain} are absent on HubSpot."))
def get_commcare_users_by_filters(domain, user_filters, count_only=False): """ Returns CommCareUsers in domain per given filters. If user_filters is empty returns all users in the domain args: user_filters: a dict with below structure. {'role_id': <Role ID to filter users by>, 'search_string': <string to search users by username>, 'location_id': <Location ID to filter users by>} kwargs: count_only: If True, returns count of search results """ role_id = user_filters.get('role_id', None) search_string = user_filters.get('search_string', None) location_id = user_filters.get('location_id', None) if not any([role_id, search_string, location_id, count_only]): return get_all_commcare_users_by_domain(domain) query = UserES().domain(domain).mobile_users() if role_id: query = query.role_id(role_id) if search_string: query = query.search_string_query( search_string, default_fields=['first_name', 'last_name', 'username']) if location_id: location_ids = SQLLocation.objects.get_locations_and_children_ids( [location_id]) query = query.location(location_ids) if count_only: return query.count() user_ids = query.scroll_ids() return map(CommCareUser.wrap, iter_docs(CommCareUser.get_db(), user_ids))