From a4b747a7b4d05b06a022026903ff5c51bfc428a8 Mon Sep 17 00:00:00 2001 From: hadleyking Date: Wed, 8 May 2024 10:47:18 -0400 Subject: [PATCH] print( --- biocompute/apis.py | 31 +++++++++++++++++-------------- biocompute/services.py | 24 ++++++++++++++++++++---- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/biocompute/apis.py b/biocompute/apis.py index ca99f25..608cf69 100644 --- a/biocompute/apis.py +++ b/biocompute/apis.py @@ -147,25 +147,26 @@ def post(self, request) -> Response: rejected_requests = True continue - bco = BcoDraftSerializer(data=object, context={'request': request}) - - if bco.is_valid(): + serialized_bco = BcoDraftSerializer(data=object, context={'request': request}) + if serialized_bco.is_valid(): try: - bco.create(bco.validated_data) + bco_instance = serialized_bco.create(serialized_bco.validated_data) + response_id = bco_instance.object_id + score = bco_instance.score response_data.append(response_constructor( - identifier=bco['object_id'].value, + identifier=response_id, status = "SUCCESS", code= 200, - message= f"BCO {bco['object_id'].value} created", + message= f"BCO {response_id} created with a score of {score}", )) accepted_requests = True except Exception as err: response_data.append(response_constructor( - identifier=bco['object_id'].value, + identifier=serialized_bco['object_id'].value, status = "SERVER ERROR", code= 500, - message= f"BCO {bco['object_id'].value} failed", + message= f"BCO {serialized_bco['object_id'].value} failed", )) else: @@ -174,7 +175,7 @@ def post(self, request) -> Response: status = "REJECTED", code= 400, message= f"BCO {response_id} rejected", - data=bco.errors + data=serialized_bco.errors )) rejected_requests = True @@ -424,6 +425,7 @@ def post(self, request) -> Response: ) ] ) + for index, object in enumerate(data): response_id = object.get("object_id", index) modify_permitted = user_can_modify_bco(response_id, requester) @@ -449,16 +451,17 @@ def post(self, request) -> Response: rejected_requests = True continue - bco = ModifyBcoDraftSerializer(data=object) + serialized_bco = ModifyBcoDraftSerializer(data=object) - if bco.is_valid(): + if serialized_bco.is_valid(): try: - bco.update(bco.validated_data) + bco_instance = serialized_bco.update(serialized_bco.validated_data) + score = bco_instance.score response_data.append(response_constructor( identifier=response_id, status = "SUCCESS", code= 200, - message= f"BCO {response_id} updated", + message= f"BCO {response_id} updated with a sore of {score}", )) accepted_requests = True @@ -544,7 +547,7 @@ def post(self, request): validator = BcoValidator() response_data = [] rejected_requests = False - accepted_requests = False + accepted_requests = True data = request.data if 'POST_validate_bco' in request.data: data = legacy_api_converter(data=request.data) diff --git a/biocompute/services.py b/biocompute/services.py index c828749..1512502 100644 --- a/biocompute/services.py +++ b/biocompute/services.py @@ -97,7 +97,13 @@ def parse_and_validate(self, bco): """ identifier = bco.get("object_id", "Unknown") - results = {identifier: {'number_of_errors': 0, 'error_detail': []}} + results = { + identifier: { + 'number_of_errors': 0, + 'error_detail': [], + 'score': 0, + } + } # Validate against the base schema base_schema = self.load_schema(bco['spec_version']) @@ -105,6 +111,9 @@ def parse_and_validate(self, bco): results[identifier]['error_detail'].extend(base_errors) results[identifier]['number_of_errors'] += len(base_errors) + if "usability_domain" in bco: + results[identifier]['score'] = sum(len(s) for s in bco['usability_domain']) + # Validate against extension schemas, if any for extension in bco.get("extension_domain", []): extension_schema_uri = extension.get("extension_schema") @@ -287,6 +296,7 @@ def update(self, validated_data): ) etag = generate_etag(bco_contents) bco_instance.contents['etag'] = etag + score = bco_score(bco_instance=bco_instance) bco_instance.save() if authorized_usernames: authorized_users = User.objects.filter( @@ -405,8 +415,7 @@ def create(self, validated_data): bco_contents = deepcopy(bco_instance.contents) etag = generate_etag(bco_contents) bco_instance.contents['etag'] = etag - bco_instance.save() - + score = bco_score(bco_instance=bco_instance) if authorized_usernames: authorized_users = User.objects.filter( username__in=authorized_usernames @@ -595,11 +604,18 @@ def bco_score(bco_instance: Bco) -> Bco: """ contents = bco_instance.contents + + if "usability_domain" not in contents: + bco_instance.score = 0 + return bco_instance + try: usability_domain_length = sum(len(s) for s in contents['usability_domain']) score = {"usability_domain_length": usability_domain_length} except TypeError: score = {"usability_domain_length": 0} + usability_domain_length = 0 + bco_instance.score = usability_domain_length - return bco_instance \ No newline at end of file + return bco_instance