Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for LDH #347

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 68 additions & 22 deletions biocompute/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
user_can_publish_draft,
user_can_publish_prefix,
prefix_from_object_id,
get_authorized_bcos
)
from biocompute.services import convert_to_ldh
from config.services import (
legacy_api_converter,
bulk_response_constructor,
Expand Down Expand Up @@ -910,27 +912,71 @@ def post(self, request):
accepted_requests = True
data = request.data

try:

for index, comparison in enumerate(data):
new_bco, old_bco = comparison
identifier = new_bco["object_id"]+ " vs " + old_bco["object_id"]

result = DeepDiff(new_bco, old_bco)
parsed_results = {
'dictionary_item_removed': list(result['dictionary_item_removed']),
'values_changed': list(result['values_changed']),
'iterable_item_removed': list(result['iterable_item_removed'])
}
for index, comparison in enumerate(data):
new_bco, old_bco = comparison
identifier = new_bco["object_id"]+ " vs " + old_bco["object_id"]

response_data.append(bulk_response_constructor(
identifier = identifier,
status="SUCCESS",
code=200,
data=parsed_results
))
response_data.append(bulk_response_constructor(
identifier = identifier,
status="SUCCESS",
code=200,
data=DeepDiff(new_bco, old_bco).to_json()
))

status_code = response_status(accepted_requests, rejected_requests)
return Response(status=status_code, data=response_data)


class ConverToLDH(APIView):
"""Convert BCO to LDH Format

-------------------
Provides an API endpoint for converting BioCompute Objects (BCOs) to a LDH
compatable format.

Example usage with curl:
```shell
curl -X GET "http://localhost:8000/api/objects/?contents=review&prefix=BCO&owner=tester&object_id=BCO" -H "accept: application/json"
```

This API view is accessible to any user without authentication requirements.
"""
authentication_classes = []
permission_classes = [AllowAny]

status_code = response_status(accepted_requests, rejected_requests)
return Response(status=status_code, data=response_data)
except Exception:
return Response(status=status.HTTP_400_BAD_REQUEST, data={})
@swagger_auto_schema(
operation_id="convert_to_ldh",
manual_parameters=[
openapi.Parameter("object_id",
openapi.IN_QUERY,
description="Object ID to be converted.",
type=openapi.TYPE_STRING,
default="http://127.0.0.1:8000/BCO_000000/1.0"
)
],
responses={
200: "Success. Object contents converted",
404: "Not found. That BCO could not be found on the server or The "\
"requestor does not have appropriate permissions."
},
tags=["BCO Management"],
)

def get(self, request):
authorized_bcos = get_authorized_bcos(request.user.id)

object_id = request.GET["object_id"]
if object_id in authorized_bcos:
data = convert_to_ldh(
object_id=object_id,
username=request.user.username
)

return Response(status=status.HTTP_200_OK, data=data)
else:
return Response(
status=status.HTTP_404_NOT_FOUND,
data={"message": f"{object_id}, could not be found "\
+ "on the server or user does not have permissions."
}
)
2 changes: 1 addition & 1 deletion biocompute/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def get_authorized_bcos(user: User):

Returns:
- QuerySet:
A Django QuerySet containing the BCOs the user is authorized to access.
A Django QuerySet containing the BCO object_ids the user is authorized to access.
"""

bcos = Bco.objects.filter(
Expand Down
51 changes: 51 additions & 0 deletions biocompute/services.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env python3
# biocopmute/services.py

import datetime
import os
import glob
import copy
import json
import jsonref
Expand Down Expand Up @@ -654,3 +657,51 @@ def bco_score(bco_instance: Bco) -> Bco:
bco_instance.score = base_score

return bco_instance

def convert_to_ldh(object_id, username):
"""
"""

data = Bco.objects.get(object_id=object_id).contents
contents = {}
contents['entType'] = 'BioCompute Object'
contents['entIri'] = data['object_id']
contents['entId'] = data['object_id'].split('/')[-2]
contents['modified'] = datetime.datetime.utcnow().isoformat()+'Z'
contents['modifier'] = username
contents['entContent'] = {}


# contents['id'] = get_id()
# contents['IdFor'] = get_id_for()
contents['entAliases'] = [contents['entId'], contents['entIri'], contents['entType']]


try:
contents['entContent']['provenance'] = data['provenance_domain']
except KeyError:
pass
try:
contents['entContent']['usability'] = data['usability_domain']
except KeyError:
pass
try:
contents['entContent']['description'] = data['description_domain']
except KeyError:
pass
try:
contents['entContent']['execution'] = data['execution_domain']
except KeyError:
pass
try:
contents['entContent']['io'] = data['io_domain']
except KeyError:
pass
try:
contents['entContent']['parametric'] = data['parametric_domain']
except KeyError:
pass

print(contents)
return contents

4 changes: 3 additions & 1 deletion biocompute/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
"""BioCompute URLs
"""

from django.urls import path
from django.urls import path, re_path
from biocompute.apis import (
DraftsCreateApi,
DraftsModifyApi,
DraftsPublishApi,
PublishBcoApi,
ValidateBcoApi,
CompareBcoApi,
ConverToLDH,
)

urlpatterns = [
Expand All @@ -19,4 +20,5 @@
path("objects/validate/", ValidateBcoApi.as_view()),
path("objects/publish/", PublishBcoApi.as_view()),
path("objects/compare/", CompareBcoApi.as_view()),
re_path("objects/convert_to_ldh/$", ConverToLDH.as_view()),
]
Loading