Skip to content

Commit

Permalink
Added PublishedRetrieveApi class
Browse files Browse the repository at this point in the history
Changes to be committed:
	modified:   biocompute/apis.py
	modified:   biocompute/selectors.py
	modified:   config/urls.py
  • Loading branch information
HadleyKing committed Apr 4, 2024
1 parent 4ffe7cb commit 8e85ef0
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 17 deletions.
85 changes: 82 additions & 3 deletions biocompute/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class DraftsCreateApi(APIView):
request_body = BCO_DRAFT_SCHEMA

@swagger_auto_schema(
operation_id="api_objects_drafts_create",
request_body=request_body,
responses={
200: "All requests were accepted.",
Expand Down Expand Up @@ -183,6 +184,7 @@ class DraftRetrieveApi(APIView):
"""

@swagger_auto_schema(
operation_id="api_get_draft",
manual_parameters=[
openapi.Parameter(
"bco_accession",
Expand Down Expand Up @@ -211,6 +213,83 @@ def get(self, request, bco_accession):
status=status.HTTP_403_FORBIDDEN,
data={"message": f"User, {requester}, does not have draft permissions"\
+ f" for {bco_accession}."})
else:
bco_counter_increment(bco_instance)
return Response(status=status.HTTP_200_OK, data=bco_instance.contents)
if bco_instance is None:
return Response(
status=status.HTTP_404_NOT_FOUND,
data={"message": f"{bco_accession}/DRAFT, could "\
+ "not be found on the server."
}
)

bco_counter_increment(bco_instance)
return Response(status=status.HTTP_200_OK, data=bco_instance.contents)

class PublishedRetrieveApi(APIView):
"""Get Published BCO
API view for retrieving a specific version of a published BioCompute
Object (BCO).
Retrieve the contents of a published BCO by specifying its accession
number and version. Authentication is not required to access most
published BCOs, reflecting the public nature of these objects. If
the prefix is not public than the user's ability to view this BCO
is verified.
Parameters:
- `bco_accession`:
Specifies the accession number of the BCO to be retrieved.
- `bco_version`:
Specifies the version of the BCO to be retrieved.
"""

@swagger_auto_schema(
operation_id="api_get_published",
manual_parameters=[
openapi.Parameter(
"bco_accession",
openapi.IN_PATH,
description="BCO accession to be viewed.",
type=openapi.TYPE_STRING,
default="BCO_000000"
),
openapi.Parameter(
"bco_version",
openapi.IN_PATH,
description="BCO version to be viewed.",
type=openapi.TYPE_STRING,
default="1.0"
)
],
responses={
200: "Success. Object contents returned",
401: "Authentication credentials were not provided, or"
" the token was invalid.",
403: "Forbidden. The requestor does not have appropriate permissions.",
404: "Not found. That BCO could not be found on the server."
},
tags=["BCO Management"],
)

def get(self, request, bco_accession, bco_version):
requester = request.user
print(requester)
bco_instance = retrieve_bco(bco_accession, requester, bco_version)
if bco_instance is False:
return Response(
status=status.HTTP_403_FORBIDDEN,
data={"message": f"User, {requester}, does not have draft permissions"\
+ f" for {bco_accession}."})

if bco_instance is None:
return Response(
status=status.HTTP_404_NOT_FOUND,
data={"message": f"{bco_accession}/{bco_version}, could "\
+ "not be found on the server."
}
)

bco_counter_increment(bco_instance)
return Response(status=status.HTTP_200_OK, data=bco_instance.contents)

29 changes: 16 additions & 13 deletions biocompute/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,31 @@
from biocompute.models import Bco
from prefix.selectors import user_can_view

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

hostname = settings.PUBLIC_HOSTNAME
object_id = f"{hostname}/{bco_accession}/DRAFT"

if bco_version is None:
object_id = f"{hostname}/{bco_accession}/DRAFT"
else:
object_id = f"{hostname}/{bco_accession}/{bco_version}"

prefix_name = bco_accession.split("_")[0]
view_permission = user_can_view(prefix_name, user)
if view_permission is False:
return False

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

if user in bco_instance.authorized_users.all():
return bco_instance

view_permission = user_can_view(prefix_name, user)
if view_permission is False:
return False

return bco_instance
7 changes: 6 additions & 1 deletion config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from drf_yasg import openapi
from rest_framework import permissions
from rest_framework_jwt.views import obtain_jwt_token, verify_jwt_token
from biocompute.apis import DraftRetrieveApi
from biocompute.apis import DraftRetrieveApi, PublishedRetrieveApi

# Load the server config file.
server_config = configparser.ConfigParser()
Expand Down Expand Up @@ -58,4 +58,9 @@
path("api/", include("biocompute.urls")),
path("api/", include("prefix.urls")),
path("<str:bco_accession>/DRAFT", DraftRetrieveApi.as_view()),
path(
"<str:bco_accession>/<str:bco_version>",
PublishedRetrieveApi.as_view()
),
# path("<str:object_id_root>", ObjectIdRootObjectId.as_view()),
]

0 comments on commit 8e85ef0

Please sign in to comment.