diff --git a/backend/projects/views.py b/backend/projects/views.py index 13ccae6c6..8db8b2572 100644 --- a/backend/projects/views.py +++ b/backend/projects/views.py @@ -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 @@ -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 diff --git a/backend/utils/search_response_by_keyword.py b/backend/utils/search_response_by_keyword.py new file mode 100644 index 000000000..d5a1ac40f --- /dev/null +++ b/backend/utils/search_response_by_keyword.py @@ -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