66
77from drf_yasg import openapi
88from drf_yasg .utils import swagger_auto_schema
9+ from django .conf import settings
910from django .db import utils
1011from rest_framework .views import APIView
1112from rest_framework import status
1415from tests .fixtures .example_bco import BCO_000001
1516from config .services import legacy_api_converter , response_constructor
1617from biocompute .services import BcoDraftSerializer
18+ from biocompute .selectors import retrieve_bco
19+ from prefix .selectors import user_can_draft
20+
21+
22+ hostname = settings .PUBLIC_HOSTNAME
1723
1824BCO_DRAFT_SCHEMA = openapi .Schema (
1925 type = openapi .TYPE_ARRAY ,
2531 "object_id" : openapi .Schema (
2632 type = openapi .TYPE_STRING ,
2733 description = "BCO Object ID." ,
28- example = "https://biocomputeobject.org/ TEST_000001"
34+ example = f" { hostname } / TEST_000001/DRAFT "
2935 ),
3036 "prefix" : openapi .Schema (
3137 type = openapi .TYPE_STRING ,
3238 description = "BCO Prefix to use" ,
33- example = "BCO "
39+ example = "TEST "
3440 ),
3541 "authorized_users" : openapi .Schema (
3642 type = openapi .TYPE_ARRAY ,
3743 description = "Users which can access the BCO draft." ,
38- items = openapi .Schema (type = openapi .TYPE_STRING , example = "None" )
39- ),
40- "authorized_groups" : openapi .Schema (
41- type = openapi .TYPE_ARRAY ,
42- description = "Group which can access the BCO draft." ,
43- items = openapi .Schema (type = openapi .TYPE_STRING , example = "None" )
44+ items = openapi .Schema (type = openapi .TYPE_STRING , example = "tester" )
4445 ),
4546 "contents" : openapi .Schema (
4647 type = openapi .TYPE_OBJECT ,
5354 )
5455
5556class DraftsCreateApi (APIView ):
56- """
57- Create BCO Draft [Bulk Enabled]
57+ """Create BCO Draft [Bulk Enabled]
5858
59- --------------------
59+ API endpoint for creating new BioCompute Object (BCO) drafts, with support
60+ for bulk operations.
6061
61- Creates a new BCO draft object.
62+ This endpoint allows authenticated users to create new BCO drafts
63+ individually or in bulk by submitting a list of BCO drafts. The operation
64+ can be performed for one or more drafts in a single request. Each draft is
65+ validated and processed independently, allowing for mixed response
66+ statuses (HTTP_207_MULTI_STATUS) in the case of bulk submissions.
6267 """
63-
68+
69+ permission_classes = [IsAuthenticated ,]
6470 request_body = BCO_DRAFT_SCHEMA
6571
6672 @swagger_auto_schema (
@@ -87,6 +93,30 @@ def post(self, request) -> Response:
8793
8894 for index , object in enumerate (data ):
8995 response_id = object .get ("object_id" , index )
96+ bco_prefix = object .get ("prefix" , index )
97+ prefix_permitted = user_can_draft (owner , bco_prefix )
98+
99+ if prefix_permitted is None :
100+ response_data .append (response_constructor (
101+ identifier = response_id ,
102+ status = "NOT FOUND" ,
103+ code = 404 ,
104+ message = f"Invalid prefix: { bco_prefix } ." ,
105+ ))
106+ rejected_requests = True
107+ continue
108+
109+ if prefix_permitted is False :
110+ response_data .append (response_constructor (
111+ identifier = response_id ,
112+ status = "FORBIDDEN" ,
113+ code = 400 ,
114+ message = f"User, { owner } , does not have draft permissions" \
115+ + " for prefix {bco_prefix}." ,
116+ ))
117+ rejected_requests = True
118+ continue
119+
90120 bco = BcoDraftSerializer (data = object , context = {'request' : request })
91121
92122 if bco .is_valid ():
@@ -135,3 +165,49 @@ def post(self, request) -> Response:
135165 status = status .HTTP_200_OK ,
136166 data = response_data
137167 )
168+
169+ class DraftRetrieveApi (APIView ):
170+ """Get a draft object
171+
172+ API View to Retrieve a Draft Object
173+
174+ This view allows authenticated users to retrieve the contents of a specific draft object
175+ identified by its BioCompute Object (BCO) accession number. The operation ensures that
176+ only users with appropriate permissions can access the draft contents.
177+
178+ Parameters:
179+ - bco_accession (str): A string parameter passed in the URL path that uniquely identifies
180+ the draft object to be retrieved.
181+ """
182+
183+ @swagger_auto_schema (
184+ manual_parameters = [
185+ openapi .Parameter (
186+ "bco_accession" ,
187+ openapi .IN_PATH ,
188+ description = "Object ID to be viewed." ,
189+ type = openapi .TYPE_STRING ,
190+ default = "BCO_000000"
191+ )
192+ ],
193+ responses = {
194+ 200 : "Success. Object contents returned" ,
195+ 401 : "Authentication credentials were not provided, or"
196+ " the token was invalid." ,
197+ 403 : "Forbidden. The requestor does not have appropriate permissions." ,
198+ 404 : "Not found. That draft could not be found on the server."
199+ },
200+ tags = ["BCO Management" ],
201+ )
202+
203+ def get (self , request , bco_accession ):
204+ requester = request .user
205+ print (requester )
206+ bco_instance = retrieve_bco (bco_accession , requester )
207+ if bco_instance is False :
208+ return Response (
209+ status = status .HTTP_403_FORBIDDEN ,
210+ data = {"message" : f"User, { requester } , does not have draft permissions" \
211+ + f" for { bco_accession } ." })
212+ else :
213+ return Response (status = status .HTTP_200_OK , data = bco_instance .contents )
0 commit comments