2525 bulk_response_constructor ,
2626 response_status ,
2727)
28+ from deepdiff import DeepDiff
2829from drf_yasg import openapi
2930from drf_yasg .utils import swagger_auto_schema
3031from django .conf import settings
3435from rest_framework .views import APIView
3536from rest_framework .permissions import IsAuthenticated , AllowAny
3637from rest_framework .response import Response
37- from tests .fixtures .testing_bcos import BCO_000001_DRAFT
38+ from tests .fixtures .testing_bcos import BCO_000001_DRAFT , BCO_000000_DRAFT
3839
3940hostname = settings .PUBLIC_HOSTNAME
4041BASE_DIR = settings .BASE_DIR
@@ -198,7 +199,7 @@ class PublishBcoApi(APIView):
198199 """
199200
200201 permission_classes = [IsAuthenticated ]
201- # swagger_schema = None
202+ swagger_schema = None
202203 #TODO: Add Swaggar docs
203204 # schema = jsonref.load_uri(
204205 # f"file://{BASE_DIR}/config/IEEE/2791object.json"
@@ -860,3 +861,86 @@ def get(self, request, bco_accession, bco_version):
860861
861862 bco_counter_increment (bco_instance )
862863 return Response (status = status .HTTP_200_OK , data = bco_instance .contents )
864+
865+ class CompareBcoApi (APIView ):
866+ """Bulk Compare BCOs [Bulk Enabled]
867+
868+ --------------------
869+
870+ Bulk operation to compare BCOs.
871+
872+ ```JSON
873+ [
874+ {...BCO CONTENTS...},
875+ {...BCO CONTENTS...}
876+ ]
877+
878+ """
879+
880+ authentication_classes = []
881+ permission_classes = [AllowAny ]
882+
883+ @swagger_auto_schema (
884+ operation_id = "api_bco_compare" ,
885+ request_body = openapi .Schema (
886+ type = openapi .TYPE_ARRAY ,
887+ title = "Bulk Compare BCOs" ,
888+ items = openapi .Schema (
889+ type = openapi .TYPE_ARRAY ,
890+ example = [BCO_000000_DRAFT , BCO_000001_DRAFT ],
891+ items = openapi .Schema (
892+ type = openapi .TYPE_OBJECT ,
893+ required = ["contents" ],
894+ description = "Contents of the BCO." ,
895+ )
896+ ),
897+ description = "Compare one BCO against another." ,
898+ ),
899+ responses = {
900+ 200 : "All BCO comparisons are successful." ,
901+ 207 : "Some or all BCO comparisons failed. Each object submitted"
902+ " will have it's own response object with it's own status"
903+ " message:\n " ,
904+ 400 : "Bad request."
905+ },
906+ tags = ["BCO Management" ],
907+ )
908+ def post (self , request ):
909+ validator = BcoValidator ()
910+ response_data = []
911+ rejected_requests = False
912+ accepted_requests = True
913+ data = request .data
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+ # new_results = validator.parse_and_validate(bco=new_bco)
920+ # old_results = validator.parse_and_validate(bco=old_bco)
921+ # import pdb; pdb.set_trace()
922+ # new_identifier, new_results = new_results.popitem()
923+ # old_identifier, old_results = bco_results.popitem()
924+
925+ # if results["number_of_errors"] > 0:
926+ # rejected_requests = True
927+ # bco_status = "FAILED"
928+ # status_code = 400
929+ # message = "BCO not valid"
930+
931+ # else:
932+ # accepted_requests = True
933+ # bco_status = "SUCCESS"
934+ # status_code = 200
935+ # message = "BCO valid"
936+
937+ response_data .append (bulk_response_constructor (
938+ identifier = identifier ,
939+ status = "SUCCESS" ,
940+ code = 200 ,
941+ # message=message,
942+ data = DeepDiff (new_bco , old_bco ).to_json ()
943+ ))
944+
945+ status_code = response_status (accepted_requests , rejected_requests )
946+ return Response (status = status_code , data = response_data )
0 commit comments