|
14 | 14 | from rest_framework.response import Response |
15 | 15 | from tests.fixtures.example_bco import BCO_000001 |
16 | 16 | from config.services import legacy_api_converter, response_constructor |
17 | | -from biocompute.services import BcoDraftSerializer, bco_counter_increment |
18 | | -from biocompute.selectors import retrieve_bco |
| 17 | +from biocompute.services import BcoDraftSerializer, bco_counter_increment, ModifyBcoDraftSerializer |
| 18 | +from biocompute.selectors import retrieve_bco, user_can_modify_bco |
19 | 19 | from prefix.selectors import user_can_draft |
20 | 20 |
|
21 | 21 | hostname = settings.PUBLIC_HOSTNAME |
@@ -66,11 +66,10 @@ class DraftsCreateApi(APIView): |
66 | 66 | """ |
67 | 67 |
|
68 | 68 | permission_classes = [IsAuthenticated,] |
69 | | - request_body = BCO_DRAFT_SCHEMA |
70 | 69 |
|
71 | 70 | @swagger_auto_schema( |
72 | 71 | operation_id="api_objects_drafts_create", |
73 | | - request_body=request_body, |
| 72 | + request_body=BCO_DRAFT_SCHEMA, |
74 | 73 | responses={ |
75 | 74 | 200: "All requests were accepted.", |
76 | 75 | 207: "Some requests failed and some succeeded. Each object submitted" |
@@ -166,6 +165,138 @@ def post(self, request) -> Response: |
166 | 165 | data=response_data |
167 | 166 | ) |
168 | 167 |
|
| 168 | +class DraftsModifyApi(APIView): |
| 169 | + """Modify BCO Draft [Bulk Enabled] |
| 170 | +
|
| 171 | + API endpoint for modifying BioCompute Object (BCO) drafts, with support |
| 172 | + for bulk operations. |
| 173 | +
|
| 174 | + This endpoint allows authenticated users to modify existing BCO drafts |
| 175 | + individually or in bulk by submitting a list of BCO drafts. The operation |
| 176 | + can be performed for one or more drafts in a single request. Each draft is |
| 177 | + validated and processed independently, allowing for mixed response |
| 178 | + statuses (HTTP_207_MULTI_STATUS) in the case of bulk submissions. |
| 179 | + """ |
| 180 | + |
| 181 | + permission_classes = [IsAuthenticated,] |
| 182 | + |
| 183 | + @swagger_auto_schema( |
| 184 | + operation_id="api_objects_drafts_modify", |
| 185 | + request_body=openapi.Schema( |
| 186 | + type=openapi.TYPE_ARRAY, |
| 187 | + title="Modify BCO Draft Schema", |
| 188 | + items=openapi.Schema( |
| 189 | + type=openapi.TYPE_OBJECT, |
| 190 | + required=[], |
| 191 | + properties={ |
| 192 | + "authorized_users": openapi.Schema( |
| 193 | + type=openapi.TYPE_ARRAY, |
| 194 | + description="Users which can access the BCO draft.", |
| 195 | + items=openapi.Schema(type=openapi.TYPE_STRING, example="tester") |
| 196 | + ), |
| 197 | + "contents": openapi.Schema( |
| 198 | + type=openapi.TYPE_OBJECT, |
| 199 | + description="Contents of the BCO.", |
| 200 | + example=BCO_000001 |
| 201 | + ), |
| 202 | + }, |
| 203 | + ), |
| 204 | + description="BCO Drafts to create.", |
| 205 | + ), |
| 206 | + responses={ |
| 207 | + 200: "All requests were accepted.", |
| 208 | + 207: "Some requests failed and some succeeded. Each object submitted" |
| 209 | + " will have it's own response object with it's own status" |
| 210 | + " code and message.\n", |
| 211 | + 400: "All requests were rejected.", |
| 212 | + 403: "Invalid token.", |
| 213 | + }, |
| 214 | + tags=["BCO Management"], |
| 215 | + ) |
| 216 | + |
| 217 | + def post(self, request) -> Response: |
| 218 | + response_data = [] |
| 219 | + requester = request.user |
| 220 | + data = request.data |
| 221 | + rejected_requests = False |
| 222 | + accepted_requests = False |
| 223 | + if 'POST_api_objects_drafts_modify' in request.data: |
| 224 | + data = legacy_api_converter(request.data) |
| 225 | + |
| 226 | + for index, object in enumerate(data): |
| 227 | + response_id = object.get("object_id", index) |
| 228 | + modify_permitted = user_can_modify_bco(response_id, requester) |
| 229 | + |
| 230 | + if modify_permitted is None: |
| 231 | + response_data.append(response_constructor( |
| 232 | + identifier=response_id, |
| 233 | + status = "NOT FOUND", |
| 234 | + code= 404, |
| 235 | + message= f"Invalid BCO: {response_id}.", |
| 236 | + )) |
| 237 | + rejected_requests = True |
| 238 | + continue |
| 239 | + |
| 240 | + if modify_permitted is False: |
| 241 | + response_data.append(response_constructor( |
| 242 | + identifier=response_id, |
| 243 | + status = "FORBIDDEN", |
| 244 | + code= 400, |
| 245 | + message= f"User, {requester}, does not have draft permissions"\ |
| 246 | + + f" for BCO {response_id}.", |
| 247 | + )) |
| 248 | + rejected_requests = True |
| 249 | + continue |
| 250 | + |
| 251 | + bco = ModifyBcoDraftSerializer(data=object) |
| 252 | + |
| 253 | + if bco.is_valid(): |
| 254 | + try: |
| 255 | + bco.update(bco.validated_data) |
| 256 | + response_data.append(response_constructor( |
| 257 | + identifier=response_id, |
| 258 | + status = "SUCCESS", |
| 259 | + code= 200, |
| 260 | + message= f"BCO {response_id} updated", |
| 261 | + )) |
| 262 | + accepted_requests = True |
| 263 | + |
| 264 | + except Exception as err: |
| 265 | + response_data.append(response_constructor( |
| 266 | + identifier=response_id, |
| 267 | + status = "SERVER ERROR", |
| 268 | + code= 500, |
| 269 | + message= f"BCO {response_id} failed", |
| 270 | + )) |
| 271 | + |
| 272 | + else: |
| 273 | + response_data.append(response_constructor( |
| 274 | + identifier=response_id, |
| 275 | + status = "REJECTED", |
| 276 | + code= 400, |
| 277 | + message= f"BCO {response_id} rejected", |
| 278 | + data=bco.errors |
| 279 | + )) |
| 280 | + rejected_requests = True |
| 281 | + |
| 282 | + if accepted_requests is False and rejected_requests == True: |
| 283 | + return Response( |
| 284 | + status=status.HTTP_400_BAD_REQUEST, |
| 285 | + data=response_data |
| 286 | + ) |
| 287 | + |
| 288 | + if accepted_requests is True and rejected_requests is True: |
| 289 | + return Response( |
| 290 | + status=status.HTTP_207_MULTI_STATUS, |
| 291 | + data=response_data |
| 292 | + ) |
| 293 | + |
| 294 | + if accepted_requests is True and rejected_requests is False: |
| 295 | + return Response( |
| 296 | + status=status.HTTP_200_OK, |
| 297 | + data=response_data |
| 298 | + ) |
| 299 | + |
169 | 300 | class DraftRetrieveApi(APIView): |
170 | 301 | """Get a draft object |
171 | 302 |
|
|
0 commit comments