コード例 #1
0
ファイル: __init__.py プロジェクト: robx/adhocracy3.mercator
    def _get_slice(self, elements: IResultSet, query: IResultSet) -> Iterable:
        """Get slice defined by `query.limit` and `query.offset`.

        :returns: IResultSet if not `query.limit`, else [IResource]
        """
        elements_slice = elements
        if query.limit:
            docids_slice = islice(elements.all(resolve=None), query.offset, query.offset + query.limit)
            elements_slice = [elements.resolver(x) for x in docids_slice]
        return elements_slice
コード例 #2
0
ファイル: __init__.py プロジェクト: andantic/adhocracy3
    def _get_slice(self, elements: IResultSet, query: IResultSet) -> Iterable:
        """Get slice defined by `query.limit` and `query.offset`.

        :returns: IResultSet if not `query.limit`, else [IResource]
        """
        elements_slice = elements
        if query.limit:
            docids_slice = islice(elements.all(resolve=None), query.offset,
                                  query.offset + query.limit)
            elements_slice = [elements.resolver(x) for x in docids_slice]
        return elements_slice
コード例 #3
0
ファイル: __init__.py プロジェクト: libscott/adhocracy3
 def _sort_elements(self, elements: IResultSet,
                    query: SearchQuery) -> IResultSet:
     if query.sort_by == '':
         pass
     elif query.sort_by == 'reference':
         if query.reverse:
             raise NotImplementedError()
         if query.limit:
             raise NotImplementedError()
         references = [x for x in query.references if x[0] is not None]
         if not references:  # we need at least one reference
             return elements
         reference = references[0]
         references_index = self.get_index('reference')
         elements_sorted = references_index.search_with_order(reference)
         elements = elements_sorted.intersect(elements)
     else:
         sort_index = self.get_index(query.sort_by)
         # TODO: We should assert the IIndexSort interface here, but
         # hypatia.field.FieldIndex is missing this interface.
         assert 'sort' in sort_index.__dir__()
         elements = elements.sort(sort_index,
                                  reverse=query.reverse,
                                  limit=query.limit or None)
     return elements
コード例 #4
0
ファイル: __init__.py プロジェクト: robx/adhocracy3.mercator
 def _sort_elements(self, elements: IResultSet, query: SearchQuery) -> IResultSet:
     sort_index = self._get_index(query.sort_by)
     if sort_index is not None:
         # TODO: We should assert the IIndexSort interface here, but
         # hypatia.field.FieldIndex is missing this interface.
         assert "sort" in sort_index.__dir__()
         elements = elements.sort(sort_index, reverse=query.reverse, limit=query.limit or None)
     return elements
コード例 #5
0
 def test_search_with_group_by_and_resolve_false(self, registry, pool, inst,
                                                 query):
     from adhocracy_core.interfaces import ISimple
     from hypatia.interfaces import IResultSet
     child = self._make_resource(registry, parent=pool, iresource=ISimple)
     result = inst.search(query._replace(group_by='interfaces',
                                         resolve=False))
     assert IResultSet.providedBy(result.group_by[ISimple])
コード例 #6
0
ファイル: __init__.py プロジェクト: andantic/adhocracy3
 def _sort_elements(self, elements: IResultSet,
                    query: SearchQuery) -> IResultSet:
     sort_index = self.get_index(query.sort_by)
     if sort_index is not None:
         # TODO: We should assert the IIndexSort interface here, but
         # hypatia.field.FieldIndex is missing this interface.
         assert 'sort' in sort_index.__dir__()
         elements = elements.sort(sort_index,
                                  reverse=query.reverse,
                                  limit=query.limit or None)
     return elements
コード例 #7
0
ファイル: __init__.py プロジェクト: pra85/adhocracy3
 def _get_frequency_of(self, elements: IResultSet, query: SearchQuery) -> dict:
     frequency_of = {}
     if query.frequency_of:
         index = self.get_index(query.frequency_of)
         for value in index.unique_values():
             value_query = index.eq(value)
             value_elements = value_query.execute(resolver=None)
             intersect = elements.intersect(value_elements)
             count = len(intersect)
             if count == 0:
                 continue
             frequency_of[value] = count
     return frequency_of
コード例 #8
0
ファイル: __init__.py プロジェクト: andantic/adhocracy3
 def _get_frequency_of(self, elements: IResultSet,
                       query: SearchQuery) -> dict:
     frequency_of = {}
     if query.frequency_of:
         index = self.get_index(query.frequency_of)
         for value in index.unique_values():
             value_query = index.eq(value)
             value_elements = value_query.execute(resolver=None)
             intersect = elements.intersect(value_elements)
             count = len(intersect)
             if count == 0:
                 continue
             frequency_of[value] = count
     return frequency_of
コード例 #9
0
ファイル: __init__.py プロジェクト: pra85/adhocracy3
 def _get_group_by(self, elements: IResultSet, query: SearchQuery) -> dict:
     group_by = {}
     if query.group_by:
         index = self.get_index(query.group_by)
         for value in index.unique_values():
             value_query = index.eq(value)
             value_elements = value_query.execute(resolver=None)
             intersect = elements.intersect(value_elements)
             if len(intersect) == 0:
                 continue
             group_by[value] = intersect
     sort_index = self.get_index(query.sort_by)
     if sort_index is not None and query.sort_by != "reference":
         for key, intersect in group_by.items():
             intersect_sorted = intersect.sort(sort_index, reverse=query.reverse, limit=query.limit or None)
             group_by[key] = intersect_sorted
     for key, intersect in group_by.items():
         group_by[key] = self._resolve(intersect.all(), query)
     return group_by
コード例 #10
0
 def _get_group_by(self, elements: IResultSet, query: SearchQuery) -> dict:
     group_by = {}
     if query.group_by:
         index = self.get_index(query.group_by)
         for value in index.unique_values():
             value_query = index.eq(value)
             value_elements = value_query.execute(resolver=None)
             intersect = elements.intersect(value_elements)
             if len(intersect) == 0:
                 continue
             group_by[value] = intersect
     sort_index = self.get_index(query.sort_by)
     if sort_index is not None and query.sort_by != 'reference':
         for key, intersect in group_by.items():
             intersect_sorted = intersect.sort(sort_index,
                                               reverse=query.reverse,
                                               limit=query.limit or None)
             group_by[key] = intersect_sorted
     for key, intersect in group_by.items():
         group_by[key] = self._resolve(intersect.all(), query)
     return group_by
コード例 #11
0
 def _sort_elements(self, elements: IResultSet,
                    query: SearchQuery) -> IResultSet:
     if query.sort_by == '':
         pass
     elif query.sort_by == 'reference':
         if query.reverse:
             raise NotImplementedError()
         if query.limit:
             raise NotImplementedError()
         references = [x for x in query.references if x[0] is not None]
         if not references:  # we need at least one reference
             return elements
         reference = self._get_query_value(references[0])
         references_index = self.get_index('reference')
         elements_sorted = references_index.search_with_order(reference)
         elements = elements_sorted.intersect(elements)
     else:
         sort_index = self.get_index(query.sort_by)
         # TODO: We should assert the IIndexSort interface here, but
         # hypatia.field.FieldIndex is missing this interface.
         assert 'sort' in sort_index.__dir__()
         elements = elements.sort(sort_index, reverse=query.reverse)
     return elements
コード例 #12
0
 def test_search_default_query(self, registry, pool, inst, query):
     from hypatia.interfaces import IResultSet
     child = self._make_resource(registry, parent=pool)
     result = inst.search(query)
     assert IResultSet.providedBy(result.elements)
     assert list(result.elements) == [child]