Skip to content

Commit

Permalink
chore: global search endpoint for workspace (#1649)
Browse files Browse the repository at this point in the history
* chore: global search endpoint

* chore: global search endpoitnt for workspace search
  • Loading branch information
pablohashescobar authored Jul 24, 2023
1 parent 6db1db5 commit afce027
Showing 1 changed file with 89 additions and 83 deletions.
172 changes: 89 additions & 83 deletions apiserver/plane/api/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -31,8 +31,8 @@ def filter_workspaces(self, query, slug, project_id):
.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})
Expand All @@ -46,8 +46,8 @@ def filter_projects(self, query, slug, project_id):
.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":
Expand All @@ -56,111 +56,118 @@ def filter_issues(self, query, slug, project_id):
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,
)

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):
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,
)

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):
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,
)

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):
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,
)

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):
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(
{
Expand Down Expand Up @@ -191,7 +198,7 @@ def get(self, request, slug, project_id):

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:
Expand Down Expand Up @@ -254,7 +261,6 @@ def get(self, request, slug, project_id):
if module == "true":
issues = issues.exclude(issue_module__isnull=False)


return Response(
issues.values(
"name",
Expand Down

0 comments on commit afce027

Please sign in to comment.