From afce027bf3017387f373bb1c7a3734a06ed1249f Mon Sep 17 00:00:00 2001 From: Nikhil <118773738+pablohashescobar@users.noreply.github.com> Date: Mon, 24 Jul 2023 17:39:29 +0530 Subject: [PATCH] chore: global search endpoint for workspace (#1649) * chore: global search endpoint * chore: global search endpoitnt for workspace search --- apiserver/plane/api/views/search.py | 172 ++++++++++++++-------------- 1 file changed, 89 insertions(+), 83 deletions(-) diff --git a/apiserver/plane/api/views/search.py b/apiserver/plane/api/views/search.py index b7ad654e2..d3bcbc532 100644 --- a/apiserver/plane/api/views/search.py +++ b/apiserver/plane/api/views/search.py @@ -20,7 +20,7 @@ class GlobalSearchEndpoint(BaseAPIView): also show related workspace if found """ - def filter_workspaces(self, query, slug, project_id): + def filter_workspaces(self, query, slug, project_id, workspace_search): fields = ["name"] q = Q() for field in fields: @@ -31,8 +31,8 @@ class GlobalSearchEndpoint(BaseAPIView): .values("name", "id", "slug") ) - def filter_projects(self, query, slug, project_id): - fields = ["name"] + def filter_projects(self, query, slug, project_id, workspace_search): + fields = ["name", "identifier"] q = Q() for field in fields: q |= Q(**{f"{field}__icontains": query}) @@ -46,8 +46,8 @@ class GlobalSearchEndpoint(BaseAPIView): .values("name", "id", "identifier", "workspace__slug") ) - def filter_issues(self, query, slug, project_id): - fields = ["name", "sequence_id"] + def filter_issues(self, query, slug, project_id, workspace_search): + fields = ["name", "sequence_id", "project__identifier"] q = Q() for field in fields: if field == "sequence_id": @@ -56,111 +56,118 @@ class GlobalSearchEndpoint(BaseAPIView): q |= Q(**{"sequence_id": sequence_id}) else: q |= Q(**{f"{field}__icontains": query}) - return ( - Issue.issue_objects.filter( - q, - project__project_projectmember__member=self.request.user, - workspace__slug=slug, - project_id=project_id, - ) - .distinct() - .values( - "name", - "id", - "sequence_id", - "project__identifier", - "project_id", - "workspace__slug", - ) + + issues = Issue.issue_objects.filter( + q, + project__project_projectmember__member=self.request.user, + workspace__slug=slug, ) - def filter_cycles(self, query, slug, project_id): + if workspace_search == "false": + issues = issues.filter(project_id=project_id) + + return issues.distinct().values( + "name", + "id", + "sequence_id", + "project__identifier", + "project_id", + "workspace__slug", + ) + + def filter_cycles(self, query, slug, project_id, workspace_search): fields = ["name"] q = Q() for field in fields: q |= Q(**{f"{field}__icontains": query}) - return ( - Cycle.objects.filter( - q, - project__project_projectmember__member=self.request.user, - workspace__slug=slug, - project_id=project_id, - ) - .distinct() - .values( - "name", - "id", - "project_id", - "workspace__slug", - ) + + cycles = Cycle.objects.filter( + q, + project__project_projectmember__member=self.request.user, + workspace__slug=slug, ) - def filter_modules(self, query, slug, project_id): + if workspace_search == "false": + cycles = cycles.filter(project_id=project_id) + + return cycles.distinct().values( + "name", + "id", + "project_id", + "workspace__slug", + ) + + def filter_modules(self, query, slug, project_id, workspace_search): fields = ["name"] q = Q() for field in fields: q |= Q(**{f"{field}__icontains": query}) - return ( - Module.objects.filter( - q, - project__project_projectmember__member=self.request.user, - workspace__slug=slug, - project_id=project_id, - ) - .distinct() - .values( - "name", - "id", - "project_id", - "workspace__slug", - ) + + modules = Module.objects.filter( + q, + project__project_projectmember__member=self.request.user, + workspace__slug=slug, ) - def filter_pages(self, query, slug, project_id): + if workspace_search == "false": + modules = modules.filter(project_id=project_id) + + return modules.distinct().values( + "name", + "id", + "project_id", + "workspace__slug", + ) + + def filter_pages(self, query, slug, project_id, workspace_search): fields = ["name"] q = Q() for field in fields: q |= Q(**{f"{field}__icontains": query}) - return ( - Page.objects.filter( - q, - project__project_projectmember__member=self.request.user, - workspace__slug=slug, - project_id=project_id, - ) - .distinct() - .values( - "name", - "id", - "project_id", - "workspace__slug", - ) + + pages = Page.objects.filter( + q, + project__project_projectmember__member=self.request.user, + workspace__slug=slug, ) - def filter_views(self, query, slug, project_id): + if workspace_search == "false": + pages = pages.filter(project_id=project_id) + + return pages.distinct().values( + "name", + "id", + "project_id", + "workspace__slug", + ) + + def filter_views(self, query, slug, project_id, workspace_search): fields = ["name"] q = Q() for field in fields: q |= Q(**{f"{field}__icontains": query}) - return ( - IssueView.objects.filter( - q, - project__project_projectmember__member=self.request.user, - workspace__slug=slug, - project_id=project_id, - ) - .distinct() - .values( - "name", - "id", - "project_id", - "workspace__slug", - ) + + issue_views = IssueView.objects.filter( + q, + project__project_projectmember__member=self.request.user, + workspace__slug=slug, + ) + + if workspace_search == "false": + issue_views = issue_views.filter(project_id=project_id) + + return issue_views.distinct().values( + "name", + "id", + "project_id", + "workspace__slug", ) def get(self, request, slug, project_id): try: query = request.query_params.get("search", False) + workspace_search = request.query_params.get("workspace_search", "false") + if not query: return Response( { @@ -191,7 +198,7 @@ class GlobalSearchEndpoint(BaseAPIView): for model in MODELS_MAPPER.keys(): func = MODELS_MAPPER.get(model, None) - results[model] = func(query, slug, project_id) + results[model] = func(query, slug, project_id, workspace_search) return Response({"results": results}, status=status.HTTP_200_OK) except Exception as e: @@ -254,7 +261,6 @@ class IssueSearchEndpoint(BaseAPIView): if module == "true": issues = issues.exclude(issue_module__isnull=False) - return Response( issues.values( "name",