def get_user_ids_for_user_types(self, admin, unknown, demo, commtrack): """ 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, demo]): return [] user_filters = [filter_ for include, filter_ in [ (admin, user_es.admin_users()), (unknown, filters.OR(user_es.unknown_users(), user_es.web_users())), (demo, user_es.demo_users()), ] if include] query = (user_es.UserES() .domain(self.domain_object.name) .OR(*user_filters) .show_inactive() .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, request): user_ids = cls.selected_user_ids(request) user_types = cls.selected_user_types(request) group_ids = cls.selected_group_ids(request) 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()) user_type_filters.append(user_es.web_users()) if HQUserType.DEMO_USER in user_types: user_type_filters.append(user_es.demo_users()) q = user_es.UserES().domain(domain) if HQUserType.REGISTERED in user_types: # return all users with selected user_types user_type_filters.append(user_es.mobile_users()) return q.OR(*user_type_filters) else: # return matching user types and exact matches id_filter = filters.OR( filters.term("_id", user_ids), filters.term("__group_ids", group_ids), ) if user_type_filters: return q.OR( id_filter, filters.OR(*user_type_filters), ) else: return q.filter(id_filter)
def case_owners(self): # Get user ids for each user that match the demo_user, admin, Unknown Users, or All Mobile Workers filters user_types = EMWF.selected_user_types(self.request) 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()) 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.REGISTERED in user_types: user_type_filters.append(user_es.mobile_users()) if len(user_type_filters) > 0: special_q = user_es.UserES().domain(self.domain).OR(*user_type_filters).show_inactive() special_user_ids = special_q.run().doc_ids else: special_user_ids = [] # Get user ids for each user that was specifically selected selected_user_ids = EMWF.selected_user_ids(self.request) # Get group ids for each group that was specified selected_reporting_group_ids = EMWF.selected_reporting_group_ids(self.request) selected_sharing_group_ids = EMWF.selected_sharing_group_ids(self.request) # Get user ids for each user in specified reporting groups report_group_q = HQESQuery(index="groups").domain(self.domain)\ .doc_type("Group")\ .filter(filters.term("_id", selected_reporting_group_ids))\ .fields(["users"]) user_lists = [group["users"] for group in report_group_q.run().hits] selected_reporting_group_users = list(set().union(*user_lists)) # Get ids for each sharing group that contains a user from selected_reporting_group_users OR a user that was specifically selected share_group_q = HQESQuery(index="groups").domain(self.domain)\ .doc_type("Group")\ .filter(filters.term("case_sharing", True))\ .filter(filters.term("users", selected_reporting_group_users+selected_user_ids+special_user_ids))\ .fields([]) sharing_group_ids = share_group_q.run().doc_ids owner_ids = list(set().union( special_user_ids, selected_user_ids, selected_sharing_group_ids, selected_reporting_group_users, sharing_group_ids )) if HQUserType.COMMTRACK in user_types: owner_ids.append("commtrack-system") if HQUserType.DEMO_USER in user_types: owner_ids.append("demo_user_group_id") owner_ids += self.location_sharing_owner_ids() owner_ids += self.location_reporting_owner_ids() return owner_ids
def case_owners(self): # Get user ids for each user that match the demo_user, admin, Unknown Users, or All Mobile Workers filters user_types = EMWF.selected_user_types(self.request) 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()) 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.REGISTERED in user_types: user_type_filters.append(user_es.mobile_users()) if len(user_type_filters) > 0: special_q = user_es.UserES().domain( self.domain).OR(*user_type_filters) special_user_ids = special_q.run().doc_ids else: special_user_ids = [] # Get user ids for each user that was specifically selected selected_user_ids = EMWF.selected_user_ids(self.request) # Get group ids for each group that was specified selected_reporting_group_ids = EMWF.selected_reporting_group_ids( self.request) selected_sharing_group_ids = EMWF.selected_sharing_group_ids( self.request) # Get user ids for each user in specified reporting groups report_group_q = HQESQuery(index="groups").domain(self.domain)\ .doc_type("Group")\ .filter(filters.term("_id", selected_reporting_group_ids))\ .fields(["users"]) user_lists = [group["users"] for group in report_group_q.run().hits] selected_reporting_group_users = list(set().union(*user_lists)) # Get ids for each sharing group that contains a user from selected_reporting_group_users OR a user that was specifically selected share_group_q = HQESQuery(index="groups").domain(self.domain)\ .doc_type("Group")\ .filter(filters.term("case_sharing", True))\ .filter(filters.term("users", selected_reporting_group_users+selected_user_ids+special_user_ids))\ .fields([]) sharing_group_ids = share_group_q.run().doc_ids owner_ids = list(set().union(special_user_ids, selected_user_ids, selected_sharing_group_ids, selected_reporting_group_users, sharing_group_ids)) if HQUserType.COMMTRACK in EMWF.selected_user_types(self.request): owner_ids.append("commtrack-system") return owner_ids
def user_es_query(cls, domain, mobile_user_and_group_slugs, request_user): # The queryset returned by this method is location-safe 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()) user_type_filters.append(user_es.web_users()) if HQUserType.DEMO_USER in user_types: user_type_filters.append(user_es.demo_users()) q = user_es.UserES().domain(domain) 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())), ) elif 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) else: # return matching user types and exact matches location_ids = list(SQLLocation.active_objects .get_locations_and_children(location_ids) .location_ids()) id_filter = filters.OR( filters.term("_id", user_ids), filters.term("__group_ids", group_ids), user_es.location(location_ids), ) if user_type_filters: return q.OR( id_filter, filters.OR(*user_type_filters), ) else: return q.filter(id_filter)
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_special_owner_ids(domain, admin, unknown, web, demo, commtrack): if not any([admin, unknown, web, demo, commtrack]): 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()), ] if include ] owner_ids = (user_es.UserES().domain(domain).OR(*user_filters).get_ids()) if commtrack: owner_ids.append("commtrack-system") if demo: owner_ids.append("demo_user_group_id") owner_ids.append("demo_user") return owner_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 get_special_owner_ids(self, admin, unknown, demo, commtrack): if not any([admin, unknown, demo]): return [] user_filters = [filter_ for include, filter_ in [ (admin, user_es.admin_users()), (unknown, filters.OR(user_es.unknown_users(), user_es.web_users())), (demo, user_es.demo_users()), ] if include] owner_ids = (user_es.UserES() .domain(self.domain) .OR(*user_filters) .show_inactive() .get_ids()) if commtrack: owner_ids.append("commtrack-system") if demo: owner_ids.append("demo_user_group_id") owner_ids.append("demo_user") return owner_ids
def get_special_owner_ids(self, admin, unknown, demo, commtrack): if not any([admin, unknown, demo]): return [] user_filters = [ filter_ for include, filter_ in [ (admin, user_es.admin_users()), (unknown, filters.OR(user_es.unknown_users(), user_es.web_users())), (demo, user_es.demo_users()), ] if include ] query = (user_es.UserES().domain( self.domain).OR(*user_filters).show_inactive().fields([])) owner_ids = query.run().doc_ids if commtrack: owner_ids.append("commtrack-system") if demo: owner_ids.append("demo_user_group_id") owner_ids.append("demo_user") return owner_ids
def user_es_query(cls, domain, mobile_user_and_group_slugs): 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()) user_type_filters.append(user_es.web_users()) if HQUserType.DEMO_USER in user_types: user_type_filters.append(user_es.demo_users()) q = user_es.UserES().domain(domain) if HQUserType.REGISTERED in user_types: # return all users with selected user_types user_type_filters.append(user_es.mobile_users()) return q.OR(*user_type_filters) else: # return matching user types and exact matches location_ids = list(SQLLocation.active_objects .get_locations_and_children(location_ids) .location_ids()) id_filter = filters.OR( filters.term("_id", user_ids), filters.term("__group_ids", group_ids), user_es.location(location_ids), ) if user_type_filters: return q.OR( id_filter, filters.OR(*user_type_filters), ) else: 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 = 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 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) 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)