diff --git a/biocompute/apis.py b/biocompute/apis.py index 82653c8..958281e 100644 --- a/biocompute/apis.py +++ b/biocompute/apis.py @@ -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, @@ -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." + } + ) diff --git a/biocompute/selectors.py b/biocompute/selectors.py index 96bd087..b6d8329 100644 --- a/biocompute/selectors.py +++ b/biocompute/selectors.py @@ -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( diff --git a/biocompute/services.py b/biocompute/services.py index 01192e7..90a3d35 100644 --- a/biocompute/services.py +++ b/biocompute/services.py @@ -1,6 +1,9 @@ #!/usr/bin/env python3 # biocopmute/services.py +import datetime +import os +import glob import copy import json import jsonref @@ -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 + diff --git a/biocompute/urls.py b/biocompute/urls.py index 7ce171c..bce8dbf 100644 --- a/biocompute/urls.py +++ b/biocompute/urls.py @@ -2,7 +2,7 @@ """BioCompute URLs """ -from django.urls import path +from django.urls import path, re_path from biocompute.apis import ( DraftsCreateApi, DraftsModifyApi, @@ -10,6 +10,7 @@ PublishBcoApi, ValidateBcoApi, CompareBcoApi, + ConverToLDH, ) urlpatterns = [ @@ -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()), ] \ No newline at end of file