def user_query(self): user_query = UserES().domain(self.domain).filter(mobile_users()) locations_id = self.locations_id if locations_id: user_query = user_query.location(locations_id) elif not self.request.couch_user.has_permission( self.domain, 'access_all_locations'): # EnikshayLocationFilter.get_value should always return a # location_id for restricted users raise PermissionDenied() return user_query
def _get_es_query(domain, user_filters): role_id = user_filters.get('role_id', None) search_string = user_filters.get('search_string', None) location_id = user_filters.get('location_id', None) query = UserES().domain(domain).mobile_users().remove_default_filter('active') 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) return query
def _get_es_query(domain, user_type, user_filters): role_id = user_filters.get('role_id', None) search_string = user_filters.get('search_string', None) location_id = user_filters.get('location_id', None) # The following two filters applies only to MOBILE_USER_TYPE selected_location_only = user_filters.get('selected_location_only', False) user_active_status = user_filters.get('user_active_status', None) if user_active_status is None: # Show all users in domain - will always be true for WEB_USER_TYPE query = UserES().domain(domain).remove_default_filter('active') elif user_active_status: # Active users filtered by default query = UserES().domain(domain) else: query = UserES().domain(domain).show_only_inactive() if user_type == MOBILE_USER_TYPE: query = query.mobile_users() if user_type == WEB_USER_TYPE: query = query.web_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']) location_ids = [] if 'web_user_assigned_location_ids' in user_filters.keys(): location_ids = SQLLocation.objects.get_locations_and_children_ids( user_filters['web_user_assigned_location_ids']) elif location_id: if selected_location_only: # This block will never execute for WEB_USER_TYPE location_ids = [location_id] else: location_ids = SQLLocation.objects.get_locations_and_children_ids( [location_id]) if location_ids: query = query.location(location_ids) return query
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))
def user_query(self): user_query = UserES().domain(self.domain).filter(mobile_users()) locations_id = self.locations_id if locations_id: user_query = user_query.location(locations_id) return user_query