Skip to content

Commit 79239e4

Browse files
authored
Add API for LDH (#347)
#333 Changes to be committed: modified: biocompute/apis.py modified: biocompute/selectors.py modified: biocompute/services.py modified: biocompute/urls.py
1 parent d36d502 commit 79239e4

File tree

4 files changed

+123
-24
lines changed

4 files changed

+123
-24
lines changed

biocompute/apis.py

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
user_can_publish_draft,
2020
user_can_publish_prefix,
2121
prefix_from_object_id,
22+
get_authorized_bcos
2223
)
24+
from biocompute.services import convert_to_ldh
2325
from config.services import (
2426
legacy_api_converter,
2527
bulk_response_constructor,
@@ -910,27 +912,71 @@ def post(self, request):
910912
accepted_requests = True
911913
data = request.data
912914

913-
try:
914-
915-
for index, comparison in enumerate(data):
916-
new_bco, old_bco = comparison
917-
identifier = new_bco["object_id"]+ " vs " + old_bco["object_id"]
918-
919-
result = DeepDiff(new_bco, old_bco)
920-
parsed_results = {
921-
'dictionary_item_removed': list(result['dictionary_item_removed']),
922-
'values_changed': list(result['values_changed']),
923-
'iterable_item_removed': list(result['iterable_item_removed'])
924-
}
915+
for index, comparison in enumerate(data):
916+
new_bco, old_bco = comparison
917+
identifier = new_bco["object_id"]+ " vs " + old_bco["object_id"]
925918

926-
response_data.append(bulk_response_constructor(
927-
identifier = identifier,
928-
status="SUCCESS",
929-
code=200,
930-
data=parsed_results
931-
))
919+
response_data.append(bulk_response_constructor(
920+
identifier = identifier,
921+
status="SUCCESS",
922+
code=200,
923+
data=DeepDiff(new_bco, old_bco).to_json()
924+
))
925+
926+
status_code = response_status(accepted_requests, rejected_requests)
927+
return Response(status=status_code, data=response_data)
928+
929+
930+
class ConverToLDH(APIView):
931+
"""Convert BCO to LDH Format
932+
933+
-------------------
934+
Provides an API endpoint for converting BioCompute Objects (BCOs) to a LDH
935+
compatable format.
936+
937+
Example usage with curl:
938+
```shell
939+
curl -X GET "http://localhost:8000/api/objects/?contents=review&prefix=BCO&owner=tester&object_id=BCO" -H "accept: application/json"
940+
```
941+
942+
This API view is accessible to any user without authentication requirements.
943+
"""
944+
authentication_classes = []
945+
permission_classes = [AllowAny]
932946

933-
status_code = response_status(accepted_requests, rejected_requests)
934-
return Response(status=status_code, data=response_data)
935-
except Exception:
936-
return Response(status=status.HTTP_400_BAD_REQUEST, data={})
947+
@swagger_auto_schema(
948+
operation_id="convert_to_ldh",
949+
manual_parameters=[
950+
openapi.Parameter("object_id",
951+
openapi.IN_QUERY,
952+
description="Object ID to be converted.",
953+
type=openapi.TYPE_STRING,
954+
default="http://127.0.0.1:8000/BCO_000000/1.0"
955+
)
956+
],
957+
responses={
958+
200: "Success. Object contents converted",
959+
404: "Not found. That BCO could not be found on the server or The "\
960+
"requestor does not have appropriate permissions."
961+
},
962+
tags=["BCO Management"],
963+
)
964+
965+
def get(self, request):
966+
authorized_bcos = get_authorized_bcos(request.user.id)
967+
968+
object_id = request.GET["object_id"]
969+
if object_id in authorized_bcos:
970+
data = convert_to_ldh(
971+
object_id=object_id,
972+
username=request.user.username
973+
)
974+
975+
return Response(status=status.HTTP_200_OK, data=data)
976+
else:
977+
return Response(
978+
status=status.HTTP_404_NOT_FOUND,
979+
data={"message": f"{object_id}, could not be found "\
980+
+ "on the server or user does not have permissions."
981+
}
982+
)

biocompute/selectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def get_authorized_bcos(user: User):
237237
238238
Returns:
239239
- QuerySet:
240-
A Django QuerySet containing the BCOs the user is authorized to access.
240+
A Django QuerySet containing the BCO object_ids the user is authorized to access.
241241
"""
242242

243243
bcos = Bco.objects.filter(

biocompute/services.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python3
22
# biocopmute/services.py
33

4+
import datetime
5+
import os
6+
import glob
47
import copy
58
import json
69
import jsonref
@@ -654,3 +657,51 @@ def bco_score(bco_instance: Bco) -> Bco:
654657
bco_instance.score = base_score
655658

656659
return bco_instance
660+
661+
def convert_to_ldh(object_id, username):
662+
"""
663+
"""
664+
665+
data = Bco.objects.get(object_id=object_id).contents
666+
contents = {}
667+
contents['entType'] = 'BioCompute Object'
668+
contents['entIri'] = data['object_id']
669+
contents['entId'] = data['object_id'].split('/')[-2]
670+
contents['modified'] = datetime.datetime.utcnow().isoformat()+'Z'
671+
contents['modifier'] = username
672+
contents['entContent'] = {}
673+
674+
675+
# contents['id'] = get_id()
676+
# contents['IdFor'] = get_id_for()
677+
contents['entAliases'] = [contents['entId'], contents['entIri'], contents['entType']]
678+
679+
680+
try:
681+
contents['entContent']['provenance'] = data['provenance_domain']
682+
except KeyError:
683+
pass
684+
try:
685+
contents['entContent']['usability'] = data['usability_domain']
686+
except KeyError:
687+
pass
688+
try:
689+
contents['entContent']['description'] = data['description_domain']
690+
except KeyError:
691+
pass
692+
try:
693+
contents['entContent']['execution'] = data['execution_domain']
694+
except KeyError:
695+
pass
696+
try:
697+
contents['entContent']['io'] = data['io_domain']
698+
except KeyError:
699+
pass
700+
try:
701+
contents['entContent']['parametric'] = data['parametric_domain']
702+
except KeyError:
703+
pass
704+
705+
print(contents)
706+
return contents
707+

biocompute/urls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
"""BioCompute URLs
33
"""
44

5-
from django.urls import path
5+
from django.urls import path, re_path
66
from biocompute.apis import (
77
DraftsCreateApi,
88
DraftsModifyApi,
99
DraftsPublishApi,
1010
PublishBcoApi,
1111
ValidateBcoApi,
1212
CompareBcoApi,
13+
ConverToLDH,
1314
)
1415

1516
urlpatterns = [
@@ -19,4 +20,5 @@
1920
path("objects/validate/", ValidateBcoApi.as_view()),
2021
path("objects/publish/", PublishBcoApi.as_view()),
2122
path("objects/compare/", CompareBcoApi.as_view()),
23+
re_path("objects/convert_to_ldh/$", ConverToLDH.as_view()),
2224
]

0 commit comments

Comments
 (0)