Skip to content

Commit

Permalink
print(
Browse files Browse the repository at this point in the history
  • Loading branch information
HadleyKing committed May 8, 2024
1 parent 39ad79a commit a4b747a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
31 changes: 17 additions & 14 deletions biocompute/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
24 changes: 20 additions & 4 deletions biocompute/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,23 @@ 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'])
base_errors = self.validate_json(base_schema, 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")
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
return bco_instance

0 comments on commit a4b747a

Please sign in to comment.