forked from github/plane
fix: global search endpoint to show entities within projects and add filtering through identifiers (#543)
This commit is contained in:
parent
3503b22dd9
commit
d6ffc3176e
@ -1152,7 +1152,7 @@ urlpatterns = [
|
||||
## End Importer
|
||||
# Search
|
||||
path(
|
||||
"workspaces/<str:slug>/search/",
|
||||
"workspaces/<str:slug>/projects/<uuid:project_id>/search/",
|
||||
GlobalSearchEndpoint.as_view(),
|
||||
name="global-search",
|
||||
),
|
||||
|
@ -1,3 +1,6 @@
|
||||
# Python imports
|
||||
import re
|
||||
|
||||
# Django imports
|
||||
from django.db.models import Q
|
||||
|
||||
@ -16,7 +19,7 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
also show related workspace if found
|
||||
"""
|
||||
|
||||
def filter_workspaces(self, query, slug):
|
||||
def filter_workspaces(self, query, slug, project_id):
|
||||
fields = ["name"]
|
||||
q = Q()
|
||||
for field in fields:
|
||||
@ -25,7 +28,7 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
q, workspace_member__member=self.request.user
|
||||
).values("name", "id", "slug")
|
||||
|
||||
def filter_projects(self, query, slug):
|
||||
def filter_projects(self, query, slug, project_id):
|
||||
fields = ["name"]
|
||||
q = Q()
|
||||
for field in fields:
|
||||
@ -36,15 +39,21 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
workspace__slug=slug,
|
||||
).values("name", "id", "identifier", "workspace__slug")
|
||||
|
||||
def filter_issues(self, query, slug):
|
||||
fields = ["name"]
|
||||
def filter_issues(self, query, slug, project_id):
|
||||
fields = ["name", "sequence_id"]
|
||||
q = Q()
|
||||
for field in fields:
|
||||
if field == "sequence_id":
|
||||
sequences = re.findall(r"\d+\.\d+|\d+", query)
|
||||
for sequence_id in sequences:
|
||||
q |= Q(**{"sequence_id": sequence_id})
|
||||
else:
|
||||
q |= Q(**{f"{field}__icontains": query})
|
||||
return Issue.objects.filter(
|
||||
q,
|
||||
project__project_projectmember__member=self.request.user,
|
||||
workspace__slug=slug,
|
||||
project_id=project_id,
|
||||
).values(
|
||||
"name",
|
||||
"id",
|
||||
@ -54,7 +63,7 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
"workspace__slug",
|
||||
)
|
||||
|
||||
def filter_cycles(self, query, slug):
|
||||
def filter_cycles(self, query, slug, project_id):
|
||||
fields = ["name"]
|
||||
q = Q()
|
||||
for field in fields:
|
||||
@ -63,6 +72,7 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
q,
|
||||
project__project_projectmember__member=self.request.user,
|
||||
workspace__slug=slug,
|
||||
project_id=project_id,
|
||||
).values(
|
||||
"name",
|
||||
"id",
|
||||
@ -70,7 +80,7 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
"workspace__slug",
|
||||
)
|
||||
|
||||
def filter_modules(self, query, slug):
|
||||
def filter_modules(self, query, slug, project_id):
|
||||
fields = ["name"]
|
||||
q = Q()
|
||||
for field in fields:
|
||||
@ -79,6 +89,7 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
q,
|
||||
project__project_projectmember__member=self.request.user,
|
||||
workspace__slug=slug,
|
||||
project_id=project_id,
|
||||
).values(
|
||||
"name",
|
||||
"id",
|
||||
@ -86,7 +97,7 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
"workspace__slug",
|
||||
)
|
||||
|
||||
def filter_pages(self, query, slug):
|
||||
def filter_pages(self, query, slug, project_id):
|
||||
fields = ["name"]
|
||||
q = Q()
|
||||
for field in fields:
|
||||
@ -95,6 +106,7 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
q,
|
||||
project__project_projectmember__member=self.request.user,
|
||||
workspace__slug=slug,
|
||||
project_id=project_id,
|
||||
).values(
|
||||
"name",
|
||||
"id",
|
||||
@ -102,7 +114,7 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
"workspace__slug",
|
||||
)
|
||||
|
||||
def filter_views(self, query, slug):
|
||||
def filter_views(self, query, slug, project_id):
|
||||
fields = ["name"]
|
||||
q = Q()
|
||||
for field in fields:
|
||||
@ -111,6 +123,7 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
q,
|
||||
project__project_projectmember__member=self.request.user,
|
||||
workspace__slug=slug,
|
||||
project_id=project_id,
|
||||
).values(
|
||||
"name",
|
||||
"id",
|
||||
@ -118,7 +131,7 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
"workspace__slug",
|
||||
)
|
||||
|
||||
def get(self, request, slug):
|
||||
def get(self, request, slug, project_id):
|
||||
try:
|
||||
query = request.query_params.get("search", False)
|
||||
if not query:
|
||||
@ -151,11 +164,11 @@ class GlobalSearchEndpoint(BaseAPIView):
|
||||
|
||||
for model in MODELS_MAPPER.keys():
|
||||
func = MODELS_MAPPER.get(model, None)
|
||||
results[model] = func(query, slug)
|
||||
results[model] = func(query, slug, project_id)
|
||||
return Response({"results": results}, status=status.HTTP_200_OK)
|
||||
|
||||
except Exception as e:
|
||||
capture_exception(e)
|
||||
print(e)
|
||||
return Response(
|
||||
{"error": "Something went wrong please try again later"},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
|
Loading…
Reference in New Issue
Block a user