예제 #1
0
def get_all_submissions_of_challenge(request, challenge_pk, challenge_phase_pk):
    """
    Returns all the submissions for a particular challenge
    """
    # To check for the corresponding challenge from challenge_pk.
    challenge = get_challenge_model(challenge_pk)

    # To check for the corresponding challenge phase from the challenge_phase_pk and challenge.
    try:
        challenge_phase = ChallengePhase.objects.get(pk=challenge_phase_pk, challenge=challenge)
    except ChallengePhase.DoesNotExist:
        response_data = {'error': 'Challenge Phase {} does not exist'.format(challenge_phase_pk)}
        return Response(response_data, status=status.HTTP_404_NOT_FOUND)

    # To check for the user as a host of the challenge from the request and challenge_pk.
    if is_user_a_host_of_challenge(user=request.user, challenge_pk=challenge_pk):

        # Filter submissions on the basis of challenge for host for now. Later on, the support for query
        # parameters like challenge phase, date is to be added.

        submissions = Submission.objects.filter(challenge_phase__challenge=challenge).order_by('-submitted_at')
        paginator, result_page = paginated_queryset(submissions, request)
        try:
            serializer = ChallengeSubmissionManagementSerializer(result_page, many=True, context={'request': request})
            response_data = serializer.data
            return paginator.get_paginated_response(response_data)
        except:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    # To check for the user as a participant of the challenge from the request and challenge_pk.
    elif has_user_participated_in_challenge(user=request.user, challenge_id=challenge_pk):

        # get participant team object for the user for a particular challenge.
        participant_team_pk = get_participant_team_id_of_user_for_a_challenge(
                request.user, challenge_pk)

        # Filter submissions on the basis of challenge phase for a participant.
        submissions = Submission.objects.filter(participant_team=participant_team_pk,
                                                challenge_phase=challenge_phase).order_by('-submitted_at')
        paginator, result_page = paginated_queryset(submissions, request)
        try:
            serializer = SubmissionSerializer(result_page, many=True, context={'request': request})
            response_data = serializer.data
            return paginator.get_paginated_response(response_data)
        except:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    # when user is neither host not participant of the challenge.
    else:
        response_data = {'error': 'You are neither host nor participant of the challenge!'}
        return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
예제 #2
0
def download_all_submissions(request, challenge_pk, challenge_phase_pk,
                             file_type):

    # To check for the corresponding challenge from challenge_pk.
    challenge = get_challenge_model(challenge_pk)

    # To check for the corresponding challenge phase from the challenge_phase_pk and challenge.
    try:
        challenge_phase = ChallengePhase.objects.get(pk=challenge_phase_pk,
                                                     challenge=challenge)
    except ChallengePhase.DoesNotExist:
        response_data = {
            'error':
            'Challenge Phase {} does not exist'.format(challenge_phase_pk)
        }
        return Response(response_data, status=status.HTTP_404_NOT_FOUND)

    if file_type == 'csv':
        if is_user_a_host_of_challenge(user=request.user,
                                       challenge_pk=challenge_pk):
            submissions = Submission.objects.filter(
                challenge_phase__challenge=challenge).order_by('-submitted_at')
            submissions = ChallengeSubmissionManagementSerializer(
                submissions, many=True, context={'request': request})
            response = HttpResponse(content_type='text/csv')
            response[
                'Content-Disposition'] = 'attachment; filename=all_submissions.csv'
            writer = csv.writer(response)
            writer.writerow([
                'id',
                'Team Name',
                'Team Members',
                'Team Members Email Id',
                'Challenge Phase',
                'Status',
                'Created By',
                'Execution Time(sec.)',
                'Submission Number',
                'Submitted File',
                'Stdout File',
                'Stderr File',
                'Submitted At',
                'Submission Result File',
                'Submission Metadata File',
            ])
            for submission in submissions.data:
                writer.writerow([
                    submission['id'],
                    submission['participant_team'],
                    ",".join(username['username'] for username in
                             submission['participant_team_members']),
                    ",".join(
                        email['email']
                        for email in submission['participant_team_members']),
                    submission['challenge_phase'],
                    submission['status'],
                    submission['created_by'],
                    submission['execution_time'],
                    submission['submission_number'],
                    submission['input_file'],
                    submission['stdout_file'],
                    submission['stderr_file'],
                    submission['created_at'],
                    submission['submission_result_file'],
                    submission['submission_metadata_file'],
                ])
            return response

        elif has_user_participated_in_challenge(user=request.user,
                                                challenge_id=challenge_pk):

            # get participant team object for the user for a particular challenge.
            participant_team_pk = get_participant_team_id_of_user_for_a_challenge(
                request.user, challenge_pk)

            # Filter submissions on the basis of challenge phase for a participant.
            submissions = Submission.objects.filter(
                participant_team=participant_team_pk,
                challenge_phase=challenge_phase).order_by('-submitted_at')
            submissions = ChallengeSubmissionManagementSerializer(
                submissions, many=True, context={'request': request})
            response = HttpResponse(content_type='text/csv')
            response[
                'Content-Disposition'] = 'attachment; filename=all_submissions.csv'
            writer = csv.writer(response)
            writer.writerow([
                'Team Name',
                'Method Name',
                'Status',
                'Execution Time(sec.)',
                'Submitted File',
                'Result File',
                'Stdout File',
                'Stderr File',
                'Submitted At',
            ])
            for submission in submissions.data:
                writer.writerow([
                    submission['participant_team'],
                    submission['method_name'],
                    submission['status'],
                    submission['execution_time'],
                    submission['input_file'],
                    submission['submission_result_file'],
                    submission['stdout_file'],
                    submission['stderr_file'],
                    submission['created_at'],
                ])
            return response
        else:
            response_data = {
                'error':
                'You are neither host nor participant of the challenge!'
            }
            return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
    else:
        response_data = {'error': 'The file type requested is not valid!'}
        return Response(response_data, status=status.HTTP_400_BAD_REQUEST)