Commit 434d3f85 authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

limit search results

parent e50538c5
Loading
Loading
Loading
Loading
+29 −8
Original line number Diff line number Diff line
@@ -305,10 +305,13 @@ def collections(
        only_published = True
        if not parent.public_access and parent_id:
            raise ServiceException(CAN_ONLY_SHOW_PUBLIC_COLLECTIONS_CONTENT)
    try:
        limit_from = int(limit_from)
        limit_to = int(limit_to)
        if limit_to < limit_from:
        return []
            raise ServiceException(WRONG_ARGUMENT)
    except ValueError:
        raise ServiceException(WRONG_ARGUMENT)
    data = []
    if flat_list:
        query_set: Union[Iterator[Collection], QuerySet] = Collection.objects.filter(
@@ -851,7 +854,9 @@ def delete_resource(user: User, resource_id: int) -> bool:


@_rpc_groups(["Search"])
def simple_search(user: User, query: str, project_id: int) -> Dict[str, List]:
def simple_search(
    user: User, query: str, project_id: int, limit_from: int = 0, limit_to: int = 2000
) -> Dict[str, List]:
    """
    Performs a simple search on resources and collections, based on their titles.

@@ -889,6 +894,13 @@ def simple_search(user: User, query: str, project_id: int) -> Dict[str, List]:
    }
    ```
    """
    try:
        limit_from = int(limit_from)
        limit_to = int(limit_to)
        if limit_to < limit_from:
            raise ServiceException(WRONG_ARGUMENT)
    except ValueError:
        raise ServiceException(WRONG_ARGUMENT)
    project = Project.objects.filter(pk=project_id).first()
    _check_project_permission(user, project, PERM_COLLECTION_READ)
    _check_project_permission(user, project, PERM_RESOURCE_READ)
@@ -901,7 +913,7 @@ def simple_search(user: User, query: str, project_id: int) -> Dict[str, List]:
    resources_set: Union[Iterator[Resource], QuerySet] = Resource.objects.filter(
        title__icontains=query, deleted_at__isnull=True, ptr_project=project
    )
    for resource_instance in resources_set.distinct():
    for resource_instance in resources_set.distinct()[limit_from:limit_to]:
        results["resources"].append(
            serializers.resource(resource_instance, include_metas=False)
        )
@@ -934,6 +946,8 @@ def advanced_search(
    project_id: int,
    include_metas: bool = False,
    collection_id: int = None,
    limit_from: int = 0,
    limit_to: int = 2000,
) -> Dict[str, List]:
    """
    Performs a complex search using terms such as 'contains', 'is', 'does_not_contain'.
@@ -972,6 +986,13 @@ def advanced_search(
    }
    ```
    """
    try:
        limit_from = int(limit_from)
        limit_to = int(limit_to)
        if limit_to < limit_from:
            raise ServiceException(WRONG_ARGUMENT)
    except ValueError:
        raise ServiceException(WRONG_ARGUMENT)
    if len(search_terms) > SEARCH_TERMS_LIMIT:
        raise ServiceException(TOO_MANY_SEARCH_TERMS)
    project = Project.objects.filter(pk=project_id).first()
@@ -1032,7 +1053,7 @@ def advanced_search(
            resources_set = resources_set.exclude(
                tags__uid__in=search_term["exclude_tags"]
            )
    for resource_instance in resources_set.distinct():
    for resource_instance in resources_set.distinct()[limit_from, limit_to]:
        results["resources"].append(
            serializers.resource(resource_instance, include_metas=include_metas)
        )
@@ -1090,7 +1111,7 @@ def advanced_search(
            collections_set = collections_set.exclude(
                tags__uid__in=search_term["exclude_tags"]
            )
    for collection_instance in collections_set.distinct():
    for collection_instance in collections_set.distinct()[limit_from, limit_to]:
        results["collections"].append(serializers.collection(collection_instance))

    return results