def get_or_update_challenge_phase_split(request, challenge_phase_split_pk): """ Returns or Updates challenge phase split """ challenge_phase_split = get_challenge_phase_split_model(challenge_phase_split_pk) if request.method == 'PATCH': serializer = ZipChallengePhaseSplitSerializer(challenge_phase_split, data=request.data, partial=True) if serializer.is_valid(): serializer.save() response_data = serializer.data return Response(response_data, status=status.HTTP_200_OK) return Response(serializer.erros, status=status.HTTP_400_BAD_REQUEST) if request.method == 'GET': serializer = ZipChallengePhaseSplitSerializer(challenge_phase_split) response_data = serializer.data return Response(response_data, status=status.HTTP_200_OK)
def update_submission(request, challenge_pk): """ API endpoint to update submission realted attributes """ challenge_phase_split_pk = request.data['challenge_phase_split'] submission_pk = request.data['submission'] submission_status = request.data['submission_status'].lower() stdout_content = request.data['stdout'] stderr_content = request.data['stderr'] submission_result = request.data['result'] challenge_phase_split = get_challenge_phase_split_model( challenge_phase_split_pk) submission = get_submission_model(submission_pk) successful_submission = True if submission_status == Submission.FINISHED else False if submission_status not in [ Submission.FAILED, Submission.CANCELLED, Submission.FINISHED ]: response_data = {'Sorry, submission status is invalid'} return Response(response_data, status=status.HTTP_400_BAD_REQUEST) if successful_submission: leaderboard_metrics = challenge_phase_split.leaderboard.schema.get( 'labels') predictions = ast.literal_eval(request.data['result']) missing_metrics = [] malformed_metrics = [] for metric, value in predictions.items(): if metric not in leaderboard_metrics: missing_metrics.append(metric) if not (isinstance(value, float) or isinstance(value, int)): malformed_metrics.append((metric, type(value))) if len(missing_metrics): response_data = { 'Following metrics are missing in the leaderboard data: {}'. format(missing_metrics) } return Response(response_data, status=status.HTTP_400_BAD_REQUEST) if len(malformed_metrics): response_data = { 'Values for following metrics are not of float/int: {}'.format( malformed_metrics) } return Response(response_data, status=status.HTTP_400_BAD_REQUEST) serializer = CreateLeaderboardDataSerializer( data=request.data.copy(), context={ 'challenge_phase_split': challenge_phase_split, 'submission': submission, 'request': request, }) if serializer.is_valid(): serializer.save() else: return Response(serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE) submission.status = submission_status submission.completed_at = timezone.now() submission.stdout_file.save('stdout.txt', ContentFile(stdout_content)) submission.stderr_file.save('stderr.txt', ContentFile(stderr_content)) submission.submission_result_file.save('submission_result.json', ContentFile(submission_result)) submission.save() response_data = { 'succes': 'Submission result has been successfully updated' } return Response(response_data, status=status.HTTP_200_OK)