Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search-1 #1038

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions backend/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import ast
import csv
import math

from utils.search_response_by_keyword import search_response_by_keyword
from utils.pagination import paginate_queryset
from django.core.files import File
from django.db.models import Count, Q, F, Case, When
from django.forms.models import model_to_dict
Expand Down Expand Up @@ -1467,7 +1468,28 @@ def list_optimized(self, request):
projects = projects.order_by(F("published_at").desc(nulls_last=True))

projects_json = ProjectSerializerOptimized(projects, many=True)
return Response(projects_json.data, status=status.HTTP_200_OK)
key_word = request.query_params.get("keyword", None)
key_values_list = [
"project_type",
"id",
"title",
"project_stage",
"tgt_language",
"workspace_id",
]
projects_json_search = search_response_by_keyword(
key_word, key_values_list, list(projects_json.data)
)
projects_json_dict = {
project["id"]: project for project in projects_json_search
}
page = request.query_params.get("page", None)
recordes = request.query_params.get("records", 10)
projects_json_dict = paginate_queryset(projects_json_dict, page, recordes)
projects_json_response = list(projects_json_dict.values())
return JsonResponse(
projects_json_response, safe=False, status=status.HTTP_200_O
)
except Exception:
return Response(
{"message": "Please Login!"}, status=status.HTTP_400_BAD_REQUEST
Expand Down
18 changes: 18 additions & 0 deletions backend/utils/search_response_by_keyword.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.db.models import Q


def search_response_by_keyword(keyword, key_values_list, queryset):
if keyword is None:
return queryset
keyword = str(keyword).lower()
# q_objects = Q()
# for key in key_values_list:
# q_objects |= Q(**{f"{key}__icontains": keyword})
filtered_queryset = [
item
for item in queryset
if any(
keyword in str(item[key]).lower() for key in key_values_list if key in item
)
]
return filtered_queryset
Loading