def get_user_ids(self): res = (UserES() .OR(filters.AND(user_filters.mobile_users(), filters.non_null('location_id')), filters.AND(user_filters.web_users(), filters.non_null('domain_memberships.location_id'))) .exclude_source() .run()) return list(res.doc_ids)
def consume_params(self, raw_params): value = raw_params.pop(self.param, None) if value: return filters.OR( filters.AND(filters.NOT(filters.missing(self.param)), filters.range_filter(self.param, **value)), filters.AND(filters.missing(self.param), filters.range_filter("received_on", **value)))
def login_as_user_query( domain, couch_user, search_string, limit, offset): ''' Takes in various parameters to determine which users to populate the login as screen. :param domain: String domain :param couch_user: The CouchUser that is using the Login As feature :param search_string: The query that filters the users returned. Filters based on the `search_fields` as well as any fields defined in `user_data_fields`. :param limit: The max amount of users returned. :param offset: From where to start the query. :returns: An EsQuery instance. ''' search_fields = ["base_username", "last_name", "first_name", "phone_numbers"] user_es = ( UserES() .domain(domain) .start(offset) .size(limit) .sort('username.exact') .search_string_query(search_string, search_fields) ) if not couch_user.has_permission(domain, 'access_all_locations'): loc_ids = SQLLocation.objects.accessible_to_user( domain, couch_user ).location_ids() user_es = user_es.location(list(loc_ids)) if _limit_login_as(couch_user, domain): user_filters = [ filters.AND( filters.term('user_data_es.key', 'login_as_user'), filters.term('user_data_es.value', couch_user.username), ) ] if couch_user.has_permission(domain, 'access_default_login_as_user'): user_filters.append( filters.AND( filters.term('user_data_es.key', 'login_as_user'), filters.term('user_data_es.value', 'default'), ) ) user_es = user_es.filter( queries.nested( 'user_data_es', filters.OR( *user_filters ) ) ) return user_es.mobile_users()
def login_as_user_query(domain, couch_user, search_string, limit, offset, user_data_fields=None): ''' Takes in various parameters to determine which users to populate the login as screen. :param domain: String domain :param couch_user: The CouchUser that is using the Login As feature :param search_string: The query that filters the users returned. Filters based on the `search_fields` as well as any fields defined in `user_data_fields`. :param limit: The max amount of users returned. :param offset: From where to start the query. :param user_data_fields: A list of custom user data fields that should also be searched by the `search_string` :returns: An EsQuery instance. ''' search_fields = [ "base_username", "last_name", "first_name", "phone_numbers" ] should_criteria_query = [ queries.search_string_query(search_string, search_fields), ] if user_data_fields: or_criteria = [] for field in user_data_fields: or_criteria.append( filters.AND( filters.term('user_data_es.key', field), filters.term('user_data_es.value', search_string), ), ) should_criteria_query.append( queries.nested_filter('user_data_es', filters.OR(*or_criteria))) user_es = ( UserES().domain(domain).start(offset).size(limit). sort('username.exact').set_query( queries.BOOL_CLAUSE( queries.SHOULD_CLAUSE( should_criteria_query, # It should either match on the search fields like username or it # should match on the custom user data fields. If this were 2, then # it would require the search string to match both on the search fields and # the custom user data fields. minimum_should_match=1, ), ))) if not couch_user.has_permission(domain, 'access_all_locations'): loc_ids = SQLLocation.objects.accessible_to_user( domain, couch_user).location_ids() user_es = user_es.location(list(loc_ids)) return user_es.mobile_users()
def _form_filter(form): app_id = form.get('app_id', None) if app_id and app_id != MISSING_APP_ID: return es_filters.AND( form_es.app(app_id), form_es.xmlns(form['xmlns']) ) return form_es.xmlns(form['xmlns'])
def _get_cursor_query(domain, params, last_date, last_id): query = _get_query(domain, params) return query.filter( filters.OR( filters.AND( filters.term('@indexed_on', last_date), filters.range_filter('_id', gt=last_id), ), case_search.indexed_on(gt=last_date), ) )
def get_nested_terms_filter(prop, terms): filters = [] def make_filter(term): return es_filters.term(prop, term) for term in _get_terms_list(terms): if len(term) == 1: filters.append(make_filter(term[0])) elif len(term) > 1: filters.append(es_filters.AND(*(make_filter(t) for t in term))) return es_filters.OR(*filters)
def get_domains_to_update_es_filter(): """ Returns ES filter to filter domains that are never updated or domains that haven't been updated since a week or domains that have been updated within last week but have new form submissions in the last day. """ last_week = datetime.utcnow() - timedelta(days=7) more_than_a_week_ago = filters.date_range('cp_last_updated', lt=last_week) less_than_a_week_ago = filters.date_range('cp_last_updated', gte=last_week) not_updated = filters.missing('cp_last_updated') domains_submitted_today = (FormES().submitted( gte=datetime.utcnow() - timedelta(days=1)).terms_aggregation( 'domain', 'domain').size(0).run().aggregations.domain.keys) return filters.OR( not_updated, more_than_a_week_ago, filters.AND(less_than_a_week_ago, domains_submitted_today))
def test_not_and_rewrite(self): json_output = { "query": { "bool": { "filter": [ { "bool": { "must_not": { "bool": { "filter": [ { "term": { "type": "A" } }, { "term": { "type": "B" } } ] } } } }, { "match_all": {} } ], "must": { "match_all": {} } } }, "size": SIZE_LIMIT } query = HQESQuery('cases').filter( filters.NOT( filters.AND(filters.term('type', 'A'), filters.term('type', 'B')) ) ) self.checkQuery(query, json_output)
def case_filter(self): now = datetime.datetime.utcnow() fromdate = now - timedelta(days=42) _filters = BaseHNBCReport.base_filters(self) _filters.append(filters.term('pp_case_filter.#value', '1')) _filters.append(filters.range(gte=json_format_date(fromdate))) status = self.request_params.get('PNC_status', '') if status: if status == 'On Time': for i in range(1, 8): _filters.append(filters.term('case_pp_%s_done.#value' % i, 'yes')) else: or_stmt = [] for i in range(1, 8): or_stmt.append(filters.not_term('case_pp_%s_done.#value' % i, 'yes')) if or_stmt: _filters.append(filters.OR(*or_stmt)) return filters.AND(*_filters)
def user_es_query(cls, domain, mobile_user_and_group_slugs, request_user): # The queryset returned by this method is location-safe q = user_es.UserES().domain(domain, allow_mirroring=True) if ExpandedMobileWorkerFilter.no_filters_selected( mobile_user_and_group_slugs): 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 = [] 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 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 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: # return all users with selected user_types user_type_filters.append(user_es.mobile_users()) return q.OR(*user_type_filters) # 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)
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)
def _base_filters(self, episode_id): return filters.AND( filters.term('episode_id', episode_id), filters.term('adherence_value', DOSE_KNOWN_INDICATORS))
def login_as_user_filter(value): return filters.AND( filters.term('user_data_es.key', 'login_as_user'), filters.term('user_data_es.value', value), )
def to_es_filter(self): return esfilters.AND(*[f.to_es_filter() for f in self.operand_filters])
def _get_parent_case_ids_matching_subcase_query(subcase_query, context): """Get a list of case IDs for cases that have a subcase with the given index identifier and matching the subcase predicate filter. Only cases with `[>,=] case_count_gt` subcases will be returned. """ # TODO: validate that the subcase filter doesn't contain any ancestor filtering from corehq.apps.case_search.filter_dsl import ( MAX_RELATED_CASES, build_filter_from_ast, ) if subcase_query.subcase_filter: subcase_filter = build_filter_from_ast(subcase_query.subcase_filter, context) else: subcase_filter = filters.match_all() index_identifier_filter = filters.term('indices.identifier', subcase_query.index_identifier) index_query = queries.nested( 'indices', queries.filtered( queries.match_all(), filters.AND( index_identifier_filter, filters.NOT(filters.term('indices.referenced_id', '')) # exclude deleted indices ) ) ) es_query = ( CaseSearchES().domain(context.domain) .filter(index_query) .filter(subcase_filter) .aggregation( aggregations.NestedAggregation( 'indices', 'indices', ).aggregation( aggregations.FilterAggregation( 'matching_indices', index_identifier_filter ).aggregation( aggregations.TermsAggregation( 'referenced_id', 'indices.referenced_id' ) ) ) ) ) if es_query.count() > MAX_RELATED_CASES: from ..exceptions import TooManyRelatedCasesError raise TooManyRelatedCasesError( _("The related case lookup you are trying to perform would return too many cases"), serialize(subcase_query.subcase_filter) ) counts_by_parent_id = es_query.run().aggregations.indices.matching_indices.referenced_id.counts_by_bucket() if subcase_query.op == '>' and subcase_query.count <= 0: return list(counts_by_parent_id) return [ case_id for case_id, count in counts_by_parent_id.items() if subcase_query.filter_count(count) ]