11# search/apis.py
2+
23import json
4+ from biocompute .models import Bco
5+ from django .db .models import Q
36from drf_yasg import openapi
47from drf_yasg .utils import swagger_auto_schema
58from rest_framework import status
69from rest_framework .permissions import AllowAny
710from rest_framework .response import Response
811from rest_framework .views import APIView
9- from search .selectors import search_db , controled_list
12+ from search .selectors import controled_list , RETURN_VALUES
13+ from search .selectors import RETURN_VALUES as return_values
1014from itertools import chain
1115
1216class SearchObjectsAPI (APIView ):
1317 """
1418 Search the BCODB
1519
1620 -------------------
21+ Provides an API endpoint for querying BioCompute Objects (BCOs) based on
22+ various attributes. This endpoint supports multiple query parameters for
23+ flexible search capabilities.
1724
18- Endpoint for use of query string based search.
19- Four parameters are defined by this API:
20- 1. contents: Search in the contents of the BCO
21- 2. prefix: BCO Prefix to search
22- 3. owner_user: Search by BCO owner
23- 4. object_id: BCO object_id to search for
24-
25- Shell
25+ Example usage with curl:
2626 ```shell
27- curl -X GET "http://localhost:8000/api/objects/?contents=review&prefix=BCO&owner_user=bco_api_user &object_id=DRAFT " -H "accept: application/json"
27+ curl -X GET "http://localhost:8000/api/objects/?contents=review&prefix=BCO&owner=tester &object_id=BCO " -H "accept: application/json"
2828 ```
29+
30+ This API view is accessible to any user without authentication requirements.
2931 """
3032
3133 permission_classes = [AllowAny ]
32- auth = openapi .Parameter ('test' , openapi .IN_QUERY , description = "test manual param" , type = openapi .TYPE_BOOLEAN )
3334
3435 @swagger_auto_schema (
36+ operation_id = "api_objects_search" ,
3537 manual_parameters = [
38+ openapi .Parameter ('object_id' ,
39+ openapi .IN_QUERY ,
40+ description = "Search BCO Object Identifier, and primary key." ,
41+ type = openapi .TYPE_STRING
42+ ),
3643 openapi .Parameter ('contents' ,
3744 openapi .IN_QUERY ,
38- description = "Search in the contents of the BCO" ,
45+ description = "Search in the BCO JSON contents." ,
3946 type = openapi .TYPE_STRING
4047 ),
4148 openapi .Parameter ('prefix' ,
4249 openapi .IN_QUERY ,
43- description = "BCO Prefix to search" ,
50+ description = "BCO Prefix to search for. " ,
4451 type = openapi .TYPE_STRING
4552 ),
46- openapi .Parameter ('owner_user ' ,
53+ openapi .Parameter ('owner ' ,
4754 openapi .IN_QUERY ,
48- description = "Search by BCO owner " ,
55+ description = "Search by User Name that 'owns' the object " ,
4956 type = openapi .TYPE_STRING
5057 ),
51- openapi .Parameter ('object_id ' ,
58+ openapi .Parameter ('authorized_users ' ,
5259 openapi .IN_QUERY ,
53- description = "BCO object_id to search for" ,
60+ description = "Search by users who have access to the BCO" ,
61+ type = openapi .TYPE_STRING
62+ ),
63+ openapi .Parameter ('state' ,
64+ openapi .IN_QUERY ,
65+ description = "State of object. REFERENCED, PUBLISHED, DRAFT, and" \
66+ + "DELETE are currently accepted values" ,
67+ type = openapi .TYPE_STRING ,
68+ default = "published"
69+ ),
70+ openapi .Parameter ('score' ,
71+ openapi .IN_QUERY ,
72+ description = "Score assigned to BCO at the time of publishing." \
73+ + " Draft objects will not have a score." ,
74+ type = openapi .TYPE_STRING
75+ ),
76+ openapi .Parameter ('last_update' ,
77+ openapi .IN_QUERY ,
78+ description = "Date Time object for the last database change to this" \
79+ + " object" ,
80+ type = openapi .TYPE_STRING
81+ ),
82+ openapi .Parameter ('access_count' ,
83+ openapi .IN_QUERY ,
84+ description = "Then number of times this object has been downloaded or" \
85+ + " viewed." ,
5486 type = openapi .TYPE_STRING
5587 )
5688 ],
5789 responses = {
5890 200 : "Search successfull"
5991 },
60- tags = ["BCO Search " ],
92+ tags = ["BCO Management " ],
6193 )
6294
6395 def get (self , request ) -> Response :
64- return_values = [
65- "contents" ,
66- "last_update" ,
67- "object_class" ,
68- "object_id" ,
69- "owner_group" ,
70- "owner_user" ,
71- "prefix" ,
72- "schema" ,
73- "state" ,
74- ]
75- search = dict (request .GET )
76- result = controled_list (request .user )
77- for query , value in search .items ():
78- for item in value :
79- if query == 'owner_user' :
80- filter = f'{ query } '
81- else :
82- filter = f'{ query } __icontains'
83- result = search_db (filter , item , result )
84- search_result = chain (result .values (* return_values ))
85- return Response (status = status .HTTP_200_OK , data = {search_result })
96+ viewable_bcos = controled_list (request .user )
97+
98+ query = Q ()
99+
100+ for field in return_values :
101+ values = request .GET .getlist (field )
102+ if values :
103+ field_query = Q ()
104+ for value in values :
105+ field_query |= Q (** {f'{ field } __icontains' : value })
106+ query &= field_query
107+
108+ return_bco = viewable_bcos .filter (query )
109+ bco_data = chain (return_bco .values (* return_values ))
110+ return Response (status = status .HTTP_200_OK , data = bco_data )
86111
112+ class DepreciatedSearchObjectsAPI (SearchObjectsAPI ):
113+ swagger_schema = None
0 commit comments