Skip to content

Commit 8e85ef0

Browse files
committed
Added PublishedRetrieveApi class
Changes to be committed: modified: biocompute/apis.py modified: biocompute/selectors.py modified: config/urls.py
1 parent 4ffe7cb commit 8e85ef0

File tree

3 files changed

+104
-17
lines changed

3 files changed

+104
-17
lines changed

biocompute/apis.py

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class DraftsCreateApi(APIView):
6969
request_body = BCO_DRAFT_SCHEMA
7070

7171
@swagger_auto_schema(
72+
operation_id="api_objects_drafts_create",
7273
request_body=request_body,
7374
responses={
7475
200: "All requests were accepted.",
@@ -183,6 +184,7 @@ class DraftRetrieveApi(APIView):
183184
"""
184185

185186
@swagger_auto_schema(
187+
operation_id="api_get_draft",
186188
manual_parameters=[
187189
openapi.Parameter(
188190
"bco_accession",
@@ -211,6 +213,83 @@ def get(self, request, bco_accession):
211213
status=status.HTTP_403_FORBIDDEN,
212214
data={"message": f"User, {requester}, does not have draft permissions"\
213215
+ f" for {bco_accession}."})
214-
else:
215-
bco_counter_increment(bco_instance)
216-
return Response(status=status.HTTP_200_OK, data=bco_instance.contents)
216+
if bco_instance is None:
217+
return Response(
218+
status=status.HTTP_404_NOT_FOUND,
219+
data={"message": f"{bco_accession}/DRAFT, could "\
220+
+ "not be found on the server."
221+
}
222+
)
223+
224+
bco_counter_increment(bco_instance)
225+
return Response(status=status.HTTP_200_OK, data=bco_instance.contents)
226+
227+
class PublishedRetrieveApi(APIView):
228+
"""Get Published BCO
229+
230+
API view for retrieving a specific version of a published BioCompute
231+
Object (BCO).
232+
233+
Retrieve the contents of a published BCO by specifying its accession
234+
number and version. Authentication is not required to access most
235+
published BCOs, reflecting the public nature of these objects. If
236+
the prefix is not public than the user's ability to view this BCO
237+
is verified.
238+
239+
Parameters:
240+
- `bco_accession`:
241+
Specifies the accession number of the BCO to be retrieved.
242+
243+
- `bco_version`:
244+
Specifies the version of the BCO to be retrieved.
245+
"""
246+
247+
@swagger_auto_schema(
248+
operation_id="api_get_published",
249+
manual_parameters=[
250+
openapi.Parameter(
251+
"bco_accession",
252+
openapi.IN_PATH,
253+
description="BCO accession to be viewed.",
254+
type=openapi.TYPE_STRING,
255+
default="BCO_000000"
256+
),
257+
openapi.Parameter(
258+
"bco_version",
259+
openapi.IN_PATH,
260+
description="BCO version to be viewed.",
261+
type=openapi.TYPE_STRING,
262+
default="1.0"
263+
)
264+
],
265+
responses={
266+
200: "Success. Object contents returned",
267+
401: "Authentication credentials were not provided, or"
268+
" the token was invalid.",
269+
403: "Forbidden. The requestor does not have appropriate permissions.",
270+
404: "Not found. That BCO could not be found on the server."
271+
},
272+
tags=["BCO Management"],
273+
)
274+
275+
def get(self, request, bco_accession, bco_version):
276+
requester = request.user
277+
print(requester)
278+
bco_instance = retrieve_bco(bco_accession, requester, bco_version)
279+
if bco_instance is False:
280+
return Response(
281+
status=status.HTTP_403_FORBIDDEN,
282+
data={"message": f"User, {requester}, does not have draft permissions"\
283+
+ f" for {bco_accession}."})
284+
285+
if bco_instance is None:
286+
return Response(
287+
status=status.HTTP_404_NOT_FOUND,
288+
data={"message": f"{bco_accession}/{bco_version}, could "\
289+
+ "not be found on the server."
290+
}
291+
)
292+
293+
bco_counter_increment(bco_instance)
294+
return Response(status=status.HTTP_200_OK, data=bco_instance.contents)
295+

biocompute/selectors.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,31 @@
1010
from biocompute.models import Bco
1111
from prefix.selectors import user_can_view
1212

13-
def retrieve_bco(bco_accession: str, user: User) -> bool:
13+
def retrieve_bco(bco_accession:str, user:User, bco_version:str=None) -> bool:
1414
"""Retrieve BCO
1515
16-
Determines if a user can view a specific BioCompute Object (BCO).
17-
1816
This function checks whether a given user has the permission to view a BCO
19-
identified by its accession number. It performs several checks:
17+
identified by its accession number and, optionally, its version. It
18+
performs several checks:
2019
21-
1. Verifies if the BCO exists. If not, returns `None`.
22-
2. Checks if the user is explicitly authorized to view this specific BCO.
23-
3. If not directly authorized, it then checks if the user has general 'view' permissions
24-
for the prefix associated with the BCO.
20+
1. Checks if the user has general 'view' permissions for the prefix
21+
associated with the BCO.
22+
2. Verifies if the BCO exists. If not, returns `None`.
23+
3. Checks if the user is explicitly authorized to view this specific BCO.
2524
2625
"""
2726

2827
hostname = settings.PUBLIC_HOSTNAME
29-
object_id = f"{hostname}/{bco_accession}/DRAFT"
28+
29+
if bco_version is None:
30+
object_id = f"{hostname}/{bco_accession}/DRAFT"
31+
else:
32+
object_id = f"{hostname}/{bco_accession}/{bco_version}"
33+
3034
prefix_name = bco_accession.split("_")[0]
35+
view_permission = user_can_view(prefix_name, user)
36+
if view_permission is False:
37+
return False
3138

3239
try:
3340
bco_instance = Bco.objects.get(object_id=object_id)
@@ -36,9 +43,5 @@ def retrieve_bco(bco_accession: str, user: User) -> bool:
3643

3744
if user in bco_instance.authorized_users.all():
3845
return bco_instance
39-
40-
view_permission = user_can_view(prefix_name, user)
41-
if view_permission is False:
42-
return False
4346

4447
return bco_instance

config/urls.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from drf_yasg import openapi
1111
from rest_framework import permissions
1212
from rest_framework_jwt.views import obtain_jwt_token, verify_jwt_token
13-
from biocompute.apis import DraftRetrieveApi
13+
from biocompute.apis import DraftRetrieveApi, PublishedRetrieveApi
1414

1515
# Load the server config file.
1616
server_config = configparser.ConfigParser()
@@ -58,4 +58,9 @@
5858
path("api/", include("biocompute.urls")),
5959
path("api/", include("prefix.urls")),
6060
path("<str:bco_accession>/DRAFT", DraftRetrieveApi.as_view()),
61+
path(
62+
"<str:bco_accession>/<str:bco_version>",
63+
PublishedRetrieveApi.as_view()
64+
),
65+
# path("<str:object_id_root>", ObjectIdRootObjectId.as_view()),
6166
]

0 commit comments

Comments
 (0)