def get_folder_content(folder, user, add_query=None, **args): _filters = deepcopy(getattr(folder, 'filters', [])) objects = [] if _filters: query = None if add_query: query = QUERY_OPERATORS['and'](query, add_query) objects = find_entities( user=user, add_query=query, filters=_filters, filter_op='or', **args) oids = [get_oid(c) for c in folder.contents if can_access(user, c)] if args: contents = find_entities( user=user, intersect=oids, **args) oids = contents.ids if not isinstance(contents, list) else contents if isinstance(objects, list): objectmap = find_objectmap(folder) objects = ResultSet(oids, len(oids), objectmap.object_for) else: # ResultSet objects.ids = list(objects.ids) objects.ids.extend(oids) objects.numids += len(oids) return objects
def test_search_with_sort_by_references(self, registry, pool, inst, query): """Use the first reference (not back reference) to sort.""" from hypatia.util import ResultSet from adhocracy_core.interfaces import Reference from adhocracy_core.interfaces import ISheet child = self._make_resource(registry, parent=pool) child2 = self._make_resource(registry, parent=pool) search_result = ResultSet((child.__oid__, child2.__oid__), 2, None) inst._search_elements = Mock(return_value=search_result) references_result = ResultSet((child2.__oid__, child.__oid__), 2, None) reference_index = inst['adhocracy']['reference'] reference_index.search_with_order = Mock( return_value=references_result) reference = Reference(child, ISheet, 'field', None) back_reference = Reference(None, ISheet, 'field', child2) result = inst.search( query._replace(interfaces=IPool, references=(back_reference, reference), sort_by='reference')) reference_index.search_with_order.assert_called_once_with(reference) assert list(result.elements) == [child2, child]
def _search_elements(self, query) -> IResultSet: if not self.values(): # child catalogs/indexes are not created yet return ResultSet(set(), 0, None) indexes = self._combine_indexes( query, self._get_references_index_query(query), [self._get_path_index_query(query)], [self._get_interfaces_index_query(query)], self._get_indexes_index_query(query), [self._get_private_visibility_index_query(query)], [self._get_allowed_index_query(query)], ) elements = self._execute_query(indexes) return elements
def _search_elements(self, query) -> IResultSet: interfaces_index = self.get_index('interfaces') if interfaces_index is None: # pragma: no branch return ResultSet(set(), 0, None) interfaces_value = self._get_query_value(query.interfaces) if not interfaces_value: interfaces_value = (IResource, ) interfaces_comparator = self._get_query_comparator(query.interfaces) if interfaces_comparator is None: interfaces_value = normalize_to_tuple(interfaces_value) index_query = interfaces_index.all(interfaces_value) else: index_comparator = getattr(interfaces_index, interfaces_comparator) index_query = index_comparator(interfaces_value) if query.root is not None: depth = query.depth or None path_index = self.get_index('path') index_query &= path_index.eq(query.root, depth=depth, include_origin=False) if query.indexes: for index_name, value in query.indexes.items(): index = self.get_index(index_name) comparator = self._get_query_comparator(value) if comparator is None: index_comparator = index.eq else: index_comparator = getattr(index, comparator) index_value = self._get_query_value(value) index_query &= index_comparator(index_value) if query.only_visible: visibility_index = self.get_index('private_visibility') index_query &= visibility_index.eq('visible') if query.allows: allowed_index = self.get_index('allowed') principals, permission = query.allows index_query &= allowed_index.allows(principals, permission) elements = index_query.execute(resolver=None) if query.references: index = self.get_index('reference') for reference in query.references: referencence_elements = index.search_with_order(reference) referencence_elements.resolver = elements.resolver elements = referencence_elements.intersect(elements) return elements
def _execute_query(self, indexes) -> IResultSet: """Combine all query `indexes` with `&` and execute the query. If `indexes` is empty or it starts with a query from the `allows` index an empty result is returned. The allows index can only be used as a filter so you need a query that returns a search result first. """ has_indexes = len(indexes) > 0 is_starting_with_allows = has_indexes and isinstance( indexes[0], AllowsComparator) if has_indexes and not is_starting_with_allows: index_query = indexes[0] for idx in indexes[1:]: index_query &= idx elements = index_query.execute(resolver=lambda x: x) else: elements = ResultSet(set(), 0, None) return elements
def search_with_order(self, reference: Reference) -> ResultSet: """"Search target or source resources ids of `reference` with order.""" oids = [x for x in self._search_target_or_source_ids(reference)] result = ResultSet(oids, len(oids), None) return result
def search_with_order(self, reference: Reference) -> ResultSet: """"Search target or source resources ids of `reference` with order.""" query = {'reference': reference, 'traverse': False} oids = [x for x in self._search_target_or_source_ids(query)] result = ResultSet(oids, len(oids), None) return result