Skip to content

Commit a4b747a

Browse files
committed
print(
1 parent 39ad79a commit a4b747a

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

biocompute/apis.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,25 +147,26 @@ def post(self, request) -> Response:
147147
rejected_requests = True
148148
continue
149149

150-
bco = BcoDraftSerializer(data=object, context={'request': request})
151-
152-
if bco.is_valid():
150+
serialized_bco = BcoDraftSerializer(data=object, context={'request': request})
151+
if serialized_bco.is_valid():
153152
try:
154-
bco.create(bco.validated_data)
153+
bco_instance = serialized_bco.create(serialized_bco.validated_data)
154+
response_id = bco_instance.object_id
155+
score = bco_instance.score
155156
response_data.append(response_constructor(
156-
identifier=bco['object_id'].value,
157+
identifier=response_id,
157158
status = "SUCCESS",
158159
code= 200,
159-
message= f"BCO {bco['object_id'].value} created",
160+
message= f"BCO {response_id} created with a score of {score}",
160161
))
161162
accepted_requests = True
162163

163164
except Exception as err:
164165
response_data.append(response_constructor(
165-
identifier=bco['object_id'].value,
166+
identifier=serialized_bco['object_id'].value,
166167
status = "SERVER ERROR",
167168
code= 500,
168-
message= f"BCO {bco['object_id'].value} failed",
169+
message= f"BCO {serialized_bco['object_id'].value} failed",
169170
))
170171

171172
else:
@@ -174,7 +175,7 @@ def post(self, request) -> Response:
174175
status = "REJECTED",
175176
code= 400,
176177
message= f"BCO {response_id} rejected",
177-
data=bco.errors
178+
data=serialized_bco.errors
178179
))
179180
rejected_requests = True
180181

@@ -424,6 +425,7 @@ def post(self, request) -> Response:
424425
)
425426
]
426427
)
428+
427429
for index, object in enumerate(data):
428430
response_id = object.get("object_id", index)
429431
modify_permitted = user_can_modify_bco(response_id, requester)
@@ -449,16 +451,17 @@ def post(self, request) -> Response:
449451
rejected_requests = True
450452
continue
451453

452-
bco = ModifyBcoDraftSerializer(data=object)
454+
serialized_bco = ModifyBcoDraftSerializer(data=object)
453455

454-
if bco.is_valid():
456+
if serialized_bco.is_valid():
455457
try:
456-
bco.update(bco.validated_data)
458+
bco_instance = serialized_bco.update(serialized_bco.validated_data)
459+
score = bco_instance.score
457460
response_data.append(response_constructor(
458461
identifier=response_id,
459462
status = "SUCCESS",
460463
code= 200,
461-
message= f"BCO {response_id} updated",
464+
message= f"BCO {response_id} updated with a sore of {score}",
462465
))
463466
accepted_requests = True
464467

@@ -544,7 +547,7 @@ def post(self, request):
544547
validator = BcoValidator()
545548
response_data = []
546549
rejected_requests = False
547-
accepted_requests = False
550+
accepted_requests = True
548551
data = request.data
549552
if 'POST_validate_bco' in request.data:
550553
data = legacy_api_converter(data=request.data)

biocompute/services.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,23 @@ def parse_and_validate(self, bco):
9797
"""
9898

9999
identifier = bco.get("object_id", "Unknown")
100-
results = {identifier: {'number_of_errors': 0, 'error_detail': []}}
100+
results = {
101+
identifier: {
102+
'number_of_errors': 0,
103+
'error_detail': [],
104+
'score': 0,
105+
}
106+
}
101107

102108
# Validate against the base schema
103109
base_schema = self.load_schema(bco['spec_version'])
104110
base_errors = self.validate_json(base_schema, bco)
105111
results[identifier]['error_detail'].extend(base_errors)
106112
results[identifier]['number_of_errors'] += len(base_errors)
107113

114+
if "usability_domain" in bco:
115+
results[identifier]['score'] = sum(len(s) for s in bco['usability_domain'])
116+
108117
# Validate against extension schemas, if any
109118
for extension in bco.get("extension_domain", []):
110119
extension_schema_uri = extension.get("extension_schema")
@@ -287,6 +296,7 @@ def update(self, validated_data):
287296
)
288297
etag = generate_etag(bco_contents)
289298
bco_instance.contents['etag'] = etag
299+
score = bco_score(bco_instance=bco_instance)
290300
bco_instance.save()
291301
if authorized_usernames:
292302
authorized_users = User.objects.filter(
@@ -405,8 +415,7 @@ def create(self, validated_data):
405415
bco_contents = deepcopy(bco_instance.contents)
406416
etag = generate_etag(bco_contents)
407417
bco_instance.contents['etag'] = etag
408-
bco_instance.save()
409-
418+
score = bco_score(bco_instance=bco_instance)
410419
if authorized_usernames:
411420
authorized_users = User.objects.filter(
412421
username__in=authorized_usernames
@@ -595,11 +604,18 @@ def bco_score(bco_instance: Bco) -> Bco:
595604
"""
596605

597606
contents = bco_instance.contents
607+
608+
if "usability_domain" not in contents:
609+
bco_instance.score = 0
610+
return bco_instance
611+
598612
try:
599613
usability_domain_length = sum(len(s) for s in contents['usability_domain'])
600614
score = {"usability_domain_length": usability_domain_length}
601615
except TypeError:
602616
score = {"usability_domain_length": 0}
617+
usability_domain_length = 0
618+
603619
bco_instance.score = usability_domain_length
604620

605-
return bco_instance
621+
return bco_instance

0 commit comments

Comments
 (0)