|
19 | 19 | user_can_publish_draft, |
20 | 20 | user_can_publish_prefix, |
21 | 21 | prefix_from_object_id, |
| 22 | + get_authorized_bcos |
22 | 23 | ) |
| 24 | +from biocompute.services import convert_to_ldh |
23 | 25 | from config.services import ( |
24 | 26 | legacy_api_converter, |
25 | 27 | bulk_response_constructor, |
@@ -910,27 +912,71 @@ def post(self, request): |
910 | 912 | accepted_requests = True |
911 | 913 | data = request.data |
912 | 914 |
|
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"] |
925 | 918 |
|
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] |
932 | 946 |
|
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 | + ) |
0 commit comments