fix: global search endpoint to show entities within projects and add filtering through identifiers (#543)

This commit is contained in:
pablohashescobar 2023-03-27 23:34:04 +05:30 committed by GitHub
parent 3503b22dd9
commit d6ffc3176e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 13 deletions

View File

@ -1152,7 +1152,7 @@ urlpatterns = [
## End Importer ## End Importer
# Search # Search
path( path(
"workspaces/<str:slug>/search/", "workspaces/<str:slug>/projects/<uuid:project_id>/search/",
GlobalSearchEndpoint.as_view(), GlobalSearchEndpoint.as_view(),
name="global-search", name="global-search",
), ),

View File

@ -1,3 +1,6 @@
# Python imports
import re
# Django imports # Django imports
from django.db.models import Q from django.db.models import Q
@ -16,7 +19,7 @@ class GlobalSearchEndpoint(BaseAPIView):
also show related workspace if found also show related workspace if found
""" """
def filter_workspaces(self, query, slug): def filter_workspaces(self, query, slug, project_id):
fields = ["name"] fields = ["name"]
q = Q() q = Q()
for field in fields: for field in fields:
@ -25,7 +28,7 @@ class GlobalSearchEndpoint(BaseAPIView):
q, workspace_member__member=self.request.user q, workspace_member__member=self.request.user
).values("name", "id", "slug") ).values("name", "id", "slug")
def filter_projects(self, query, slug): def filter_projects(self, query, slug, project_id):
fields = ["name"] fields = ["name"]
q = Q() q = Q()
for field in fields: for field in fields:
@ -36,15 +39,21 @@ class GlobalSearchEndpoint(BaseAPIView):
workspace__slug=slug, workspace__slug=slug,
).values("name", "id", "identifier", "workspace__slug") ).values("name", "id", "identifier", "workspace__slug")
def filter_issues(self, query, slug): def filter_issues(self, query, slug, project_id):
fields = ["name"] fields = ["name", "sequence_id"]
q = Q() q = Q()
for field in fields: for field in fields:
q |= Q(**{f"{field}__icontains": query}) 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( return Issue.objects.filter(
q, q,
project__project_projectmember__member=self.request.user, project__project_projectmember__member=self.request.user,
workspace__slug=slug, workspace__slug=slug,
project_id=project_id,
).values( ).values(
"name", "name",
"id", "id",
@ -54,7 +63,7 @@ class GlobalSearchEndpoint(BaseAPIView):
"workspace__slug", "workspace__slug",
) )
def filter_cycles(self, query, slug): def filter_cycles(self, query, slug, project_id):
fields = ["name"] fields = ["name"]
q = Q() q = Q()
for field in fields: for field in fields:
@ -63,6 +72,7 @@ class GlobalSearchEndpoint(BaseAPIView):
q, q,
project__project_projectmember__member=self.request.user, project__project_projectmember__member=self.request.user,
workspace__slug=slug, workspace__slug=slug,
project_id=project_id,
).values( ).values(
"name", "name",
"id", "id",
@ -70,7 +80,7 @@ class GlobalSearchEndpoint(BaseAPIView):
"workspace__slug", "workspace__slug",
) )
def filter_modules(self, query, slug): def filter_modules(self, query, slug, project_id):
fields = ["name"] fields = ["name"]
q = Q() q = Q()
for field in fields: for field in fields:
@ -79,6 +89,7 @@ class GlobalSearchEndpoint(BaseAPIView):
q, q,
project__project_projectmember__member=self.request.user, project__project_projectmember__member=self.request.user,
workspace__slug=slug, workspace__slug=slug,
project_id=project_id,
).values( ).values(
"name", "name",
"id", "id",
@ -86,7 +97,7 @@ class GlobalSearchEndpoint(BaseAPIView):
"workspace__slug", "workspace__slug",
) )
def filter_pages(self, query, slug): def filter_pages(self, query, slug, project_id):
fields = ["name"] fields = ["name"]
q = Q() q = Q()
for field in fields: for field in fields:
@ -95,6 +106,7 @@ class GlobalSearchEndpoint(BaseAPIView):
q, q,
project__project_projectmember__member=self.request.user, project__project_projectmember__member=self.request.user,
workspace__slug=slug, workspace__slug=slug,
project_id=project_id,
).values( ).values(
"name", "name",
"id", "id",
@ -102,7 +114,7 @@ class GlobalSearchEndpoint(BaseAPIView):
"workspace__slug", "workspace__slug",
) )
def filter_views(self, query, slug): def filter_views(self, query, slug, project_id):
fields = ["name"] fields = ["name"]
q = Q() q = Q()
for field in fields: for field in fields:
@ -111,6 +123,7 @@ class GlobalSearchEndpoint(BaseAPIView):
q, q,
project__project_projectmember__member=self.request.user, project__project_projectmember__member=self.request.user,
workspace__slug=slug, workspace__slug=slug,
project_id=project_id,
).values( ).values(
"name", "name",
"id", "id",
@ -118,7 +131,7 @@ class GlobalSearchEndpoint(BaseAPIView):
"workspace__slug", "workspace__slug",
) )
def get(self, request, slug): def get(self, request, slug, project_id):
try: try:
query = request.query_params.get("search", False) query = request.query_params.get("search", False)
if not query: if not query:
@ -151,11 +164,11 @@ class GlobalSearchEndpoint(BaseAPIView):
for model in MODELS_MAPPER.keys(): for model in MODELS_MAPPER.keys():
func = MODELS_MAPPER.get(model, None) 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) return Response({"results": results}, status=status.HTTP_200_OK)
except Exception as e: except Exception as e:
capture_exception(e) print(e)
return Response( return Response(
{"error": "Something went wrong please try again later"}, {"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST, status=status.HTTP_400_BAD_REQUEST,