Esempio n. 1
0
def add_participant_team_to_challenge(request, challenge_pk,
                                      participant_team_pk):

    try:
        challenge = Challenge.objects.get(pk=challenge_pk)
    except Challenge.DoesNotExist:
        response_data = {'error': 'Challenge does not exist'}
        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)

    try:
        participant_team = ParticipantTeam.objects.get(pk=participant_team_pk)
    except ParticipantTeam.DoesNotExist:
        response_data = {'error': 'ParticipantTeam does not exist'}
        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)

    # check to disallow the user if he is a Challenge Host for this challenge

    challenge_host_team_pk = challenge.creator.pk
    challenge_host_team_user_ids = set(
        ChallengeHost.objects.select_related('user').filter(
            team_name__id=challenge_host_team_pk).values_list('user',
                                                              flat=True))

    participant_team_user_ids = set(
        Participant.objects.select_related('user').filter(
            team__id=participant_team_pk).values_list('user', flat=True))

    if challenge_host_team_user_ids & participant_team_user_ids:
        response_data = {
            'error': 'Sorry, You cannot participate in your own challenge!',
            'challenge_id': int(challenge_pk),
            'participant_team_id': int(participant_team_pk)
        }
        return Response(response_data, status=status.HTTP_406_NOT_ACCEPTABLE)

    for user in participant_team_user_ids:
        if has_user_participated_in_challenge(user, challenge_pk):
            response_data = {
                'error':
                'Sorry, other team member(s) have already participated in the Challenge.'
                ' Please participate with a different team!',
                'challenge_id':
                int(challenge_pk),
                'participant_team_id':
                int(participant_team_pk)
            }
            return Response(response_data,
                            status=status.HTTP_406_NOT_ACCEPTABLE)

    if participant_team.challenge_set.filter(id=challenge_pk).exists():
        response_data = {
            'error': 'Team already exists',
            'challenge_id': int(challenge_pk),
            'participant_team_id': int(participant_team_pk)
        }
        return Response(response_data, status=status.HTTP_200_OK)
    else:
        challenge.participant_teams.add(participant_team)
        return Response(status=status.HTTP_201_CREATED)
Esempio n. 2
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)
Esempio n. 3
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)