|
| 1 | +from rest_api.task.models import JediTask |
| 2 | +from rest_framework import status |
| 3 | +from rest_framework.response import Response |
| 4 | +from rest_framework.views import APIView |
| 5 | + |
| 6 | + |
| 7 | +class QuickSearchAPIView(APIView): |
| 8 | + """ |
| 9 | + Global search endpoint for lightweight system-wide routing. |
| 10 | +
|
| 11 | + This view serves as a fast triage system, routing queries by format (e.g., numeric IDs) to minimize database search overhead. |
| 12 | + """ |
| 13 | + |
| 14 | + def get(self, request, *args, **kwargs) -> Response: |
| 15 | + """ |
| 16 | + Handles global search queries. |
| 17 | +
|
| 18 | + Checks if the query 'q' is a numeric ID and performs fast primary key |
| 19 | + lookups across Tasks and Datasets. |
| 20 | +
|
| 21 | + Args: |
| 22 | + request (Request): DRF request containing query params. |
| 23 | + *args: Variable length argument list. |
| 24 | + **kwargs: Arbitrary keyword arguments. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + Response: A DRF Response object containing: |
| 28 | + - A list of matching result dictionaries with fields: `title`, `type`, and `id` (on HTTP 200 OK). |
| 29 | + - An empty list if no query parameter is provided (on HTTP 400 Bad Request). |
| 30 | + - An error message if a string query is passed (on HTTP 501 Not Implemented). |
| 31 | + """ |
| 32 | + query = request.query_params.get("q", "").strip() |
| 33 | + if not query: |
| 34 | + return Response([], status=status.HTTP_400_BAD_REQUEST) |
| 35 | + |
| 36 | + results = [] |
| 37 | + if query.isdigit(): |
| 38 | + target_id = int(query) |
| 39 | + |
| 40 | + # check if the target_id exists in Jobs, Tasks, or Files |
| 41 | + if JediTask.objects.filter(jeditaskid=target_id).exists(): |
| 42 | + results.append({"title": f"Task #{target_id}", "type": "task", "id": f"{target_id}"}) |
| 43 | + |
| 44 | + else: |
| 45 | + return Response({"error": "String search is not implemented yet."}, status=status.HTTP_501_NOT_IMPLEMENTED) |
| 46 | + |
| 47 | + return Response(results, status=status.HTTP_200_OK) |
0 commit comments