def get_user_ids_for_user_types(self, admin, unknown, web, demo, commtrack, active=False, deactivated=False): """ referenced from CaseListMixin to fetch user_ids for selected user type :param admin: if admin users to be included :param unknown: if unknown users to be included :param demo: if demo users to be included :param commtrack: if commtrack users to be included :return: user_ids for selected user types """ from corehq.apps.es import filters, users as user_es if not any([admin, unknown, web, demo, commtrack, active, deactivated ]): return [] user_filters = [ filter_ for include, filter_ in [ (admin, user_es.admin_users()), (unknown, filters.OR(user_es.unknown_users())), (web, user_es.web_users()), (demo, user_es.demo_users()), # Sets the is_active filter status correctly for if either active or deactivated users are selected (active ^ deactivated, user_es.is_active(active)), ] if include ] if not user_filters: return [] query = (user_es.UserES().domain( self.domain_object.name).OR(*user_filters).remove_default_filter( 'not_deleted').remove_default_filter('active').fields([])) user_ids = query.run().doc_ids if commtrack: user_ids.append("commtrack-system") if demo: user_ids.append("demo_user_group_id") user_ids.append("demo_user") return user_ids
def get_user_ids_for_user_types(self, admin, unknown, web, demo, commtrack, active=False, deactivated=False): """ referenced from CaseListMixin to fetch user_ids for selected user type :param admin: if admin users to be included :param unknown: if unknown users to be included :param demo: if demo users to be included :param commtrack: if commtrack users to be included :return: user_ids for selected user types """ from corehq.apps.es import filters, users as user_es if not any([admin, unknown, web, demo, commtrack, active, deactivated]): return [] user_filters = [filter_ for include, filter_ in [ (admin, user_es.admin_users()), (unknown, filters.OR(user_es.unknown_users())), (web, user_es.web_users()), (demo, user_es.demo_users()), # Sets the is_active filter status correctly for if either active or deactivated users are selected (active ^ deactivated, user_es.is_active(active)), ] if include] if not user_filters: return [] query = (user_es.UserES() .domain(self.domain_object.name) .OR(*user_filters) .remove_default_filter('not_deleted') .remove_default_filter('active') .fields([])) user_ids = query.run().doc_ids if commtrack: user_ids.append("commtrack-system") if demo: user_ids.append("demo_user_group_id") user_ids.append("demo_user") return user_ids
def user_es_query(cls, domain, mobile_user_and_group_slugs, request_user): # The queryset returned by this method is location-safe q = cls._base_user_es_query(domain, request_user) q = customize_user_query(request_user, domain, q) if ( ExpandedMobileWorkerFilter.no_filters_selected(mobile_user_and_group_slugs) and request_user.has_permission(domain, 'access_all_locations') ): return q.show_inactive() user_ids = cls.selected_user_ids(mobile_user_and_group_slugs) user_types = cls.selected_user_types(mobile_user_and_group_slugs) group_ids = cls.selected_group_ids(mobile_user_and_group_slugs) location_ids = cls.selected_location_ids(mobile_user_and_group_slugs) user_type_filters = [] has_user_ids = bool(user_ids) if has_user_ids: # if userid are passed then remove default active filter # and move it with mobile worker filter q = q.remove_default_filter('active') has_user_ids = True if HQUserType.DEACTIVATED in user_types: deactivated_mbwf = filters.AND(user_es.is_active(False), user_es.mobile_users()) user_type_filters.append(deactivated_mbwf) if HQUserType.ACTIVE in user_types: activated_mbwf = filters.AND(user_es.is_active(), user_es.mobile_users()) user_type_filters.append(activated_mbwf) elif HQUserType.ACTIVE in user_types and HQUserType.DEACTIVATED in user_types: q = q.show_inactive() elif HQUserType.DEACTIVATED in user_types: q = q.show_only_inactive() if HQUserType.ADMIN in user_types: user_type_filters.append(user_es.admin_users()) if HQUserType.UNKNOWN in user_types: user_type_filters.append(user_es.unknown_users()) if HQUserType.WEB in user_types: user_type_filters.append(user_es.web_users()) if HQUserType.DEMO_USER in user_types: user_type_filters.append(user_es.demo_users()) if not request_user.has_permission(domain, 'access_all_locations'): cls._verify_users_are_accessible(domain, request_user, user_ids) return q.OR( filters.term("_id", user_ids), user_es.location(list(SQLLocation.active_objects .get_locations_and_children(location_ids) .accessible_to_user(domain, request_user) .location_ids())), ) if HQUserType.ACTIVE in user_types or HQUserType.DEACTIVATED in user_types: if has_user_ids: return q.OR(*user_type_filters, filters.OR(filters.term("_id", user_ids))) else: return q.OR(*user_type_filters, user_es.mobile_users()) # return matching user types and exact matches location_ids = list(SQLLocation.active_objects .get_locations_and_children(location_ids) .location_ids()) group_id_filter = filters.term("__group_ids", group_ids) if FILTER_ON_GROUPS_AND_LOCATIONS.enabled(domain) and group_ids and location_ids: group_and_location_filter = filters.AND( group_id_filter, user_es.location(location_ids), ) else: group_and_location_filter = filters.OR( group_id_filter, user_es.location(location_ids), ) id_filter = filters.OR( filters.term("_id", user_ids), group_and_location_filter, ) if user_type_filters: return q.OR( id_filter, group_and_location_filter, filters.OR(*user_type_filters), ) return q.filter(id_filter)