<commit_before>from django.core.mail import send_mass_mail, send_mail
from django.db import IntegrityError
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import generics
from rest_framework.response import Response
from django.http import JsonResponse
from rest_framework.views import APIView
from rest_framework import status
from rest_framework.parsers import MultiPartParser
from django.http import HttpResponse, Http404
from django.core import serializers
import csv
from django.contrib.auth.models import Group
from io import TextIOWrapper
from django.conf import settings
import re

from .models import *
from .serializers import VolunteerSerializer, EventSerializer, AttendeeSerializer
#from permissions import *

# Api Call that Deals With Creating New Event


def EventListPost(self, request, *args, **kwargs):


    serializer_class = EventSerializer
    name = request.data.__getitem__('name')
    req_name = re.sub('[^A-Za-z0-9 ]+', '', name)
    req_date = request.data.__getitem__('date')
    req_csv  = request.FILES.get('csv')

    f = TextIOWrapper(req_csv.file, encoding=request.encoding)
    #reader = csv.reader(f, delimiter=',')


    event = Event.objects.create(name=req_name, date=req_date, csv=None)

    new_attendees = []
    captains = []

    try:
        has_header = csv.Sniffer().has_header(f.read(1024))
        f.seek(0)  # rewind
        reader = csv.reader(f, delimiter=',')

        if has_header:
            next(reader)  # skip header row
        else:
            event.delete()
            return JsonResponse({'error': "error with finding csv header"})

        for row in reader:

            if (len(row) != 10):
                Attendee.objects.filter(event=event).delete()
                event.delete()
                return JsonResponse({'error': "Error with CSV: Missing Row"})

            volunteer = Volunteer.objects.create(first_name=row[0],last_name= row[1],city=row[2],state=row[3],
                                                phone=row[4], email=row[5].lower())
            statusCode = 0
            if row[7] == "NO SHOW":
                statusCode = 0
            elif row[7] == "CANCEL":
                statusCode = 1
            elif row[7] == "OK":
                statusCode = 2

            if row[6] == "YES":
                attendee = Attendee.objects.create(volunteer=volunteer, team_captain=None,
                                                   event=event, status=statusCode, assignment_id=row[8],
                                                   job_descrip=row[9])
                captains.append(attendee)
                new_attendees.append(attendee)

            elif row[6] == "NO":
                attendee = Attendee.objects.create(volunteer=volunteer, team_captain=None,
                                                   event=event, status=statusCode, assignment_id=row[8],
                                                   job_descrip=row[9])
                new_attendees.append(attendee)

            # error check to make sure that CSV only YES or NO for CAPTAIN Field
            else:
                Attendee.objects.filter(event=event).delete()
                event.delete()
                return JsonResponse({'error': "Error with CSV, Team Captain field is not 'YES' or 'NO'"})

        #check to make sure that no two team captains have the same assignment id
        for cap in captains:
            for c in captains:
                if cap != c:
                    if cap.assignment_id == c.assignment_id:
                        Attendee.objects.filter(event=event).delete()
                        event.delete()
                        return JsonResponse({'error': 'Error With CSV, 2 Two team captains with same assignment id: ' + c.assignment_id })

        # match attendee with their team captain
        # ensure that each assignment id has a captain associated with it
        for a in new_attendees:
            found = False
            for cap in captains:
                if cap.assignment_id == a.assignment_id:
                    found = True
                    a.team_captain = cap.volunteer
                    a.save()
            if not found:
                Attendee.objects.filter(event=event).delete()
                event.delete()
                return JsonResponse({'error':'Error With CSV, no team captain assigned to group ' + a.assignment_id})

        event.csv = req_csv
        event.save()
        serializer = serializer_class(event, context={'request':request})

        return Response(serializer.data)

    # Generic CSV ERROR
    except csv.Error:
        Attendee.objects.filter(event=event).delete()
        event.delete()
        return JsonResponse({'error': 'Error with CSV'})



def GenerateReportGet(self, request, event):
    event = Event.objects.get(pk=self.kwargs['event'])
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='text/csv')
    file_name = "report_" + event.name
    response['Content-Disposition'] = 'attachment; filename="' + file_name + ".csv" + '"'

    writer = csv.writer(response)
    writer.writerow(['First Name', 'Last Name', 'City', 'State', 'Phone', 'Email', 'Captain', 'Status', 'AssignmentID', 'Job Description'])

    for a in Attendee.objects.filter(event=event):
        print(a)
        if a.volunteer.team_captain == a.volunteer:
            is_cap = "YES"
        else:
            is_cap = "NO"
        writer.writerow([a.volunteer.first_name, a.volunteer.last_name, a.volunteer.city, a.volunteer.state,
                         a.volunteer.phone, a.volunteer.email, is_cap, a.status, a.assignment_id, a.job_descrip])

    return response

# API Call that Returns List of attendees based upon the event
# and the team captain object

def FilteredAttendeeListGet(self):
    event = self.kwargs['event']
    captain = self.kwargs['teamcap']

    queryset = Attendee.objects.filter(event=event, team_captain=captain)
    return queryset

# API Call that returns a specific Event after name search
# passed in string of event name

def SearchEventGet(self, request, name):
    serializer_class = EventSerializer

    lookup_field = 'name'
    name = self.kwargs['name']

    try:<commit_msg>Fixed error where csv had status codes as integers<commit_after>from django.core.mail import send_mass_mail, send_mail
from django.db import IntegrityError
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import generics
from rest_framework.response import Response
from django.http import JsonResponse
from rest_framework.views import APIView
from rest_framework import status
from rest_framework.parsers import MultiPartParser
from django.http import HttpResponse, Http404
from django.core import serializers
import csv
from django.contrib.auth.models import Group
from io import TextIOWrapper
from django.conf import settings
import re

from .models import *
from .serializers import VolunteerSerializer, EventSerializer, AttendeeSerializer
#from permissions import *

# Api Call that Deals With Creating New Event


def EventListPost(self, request, *args, **kwargs):


    serializer_class = EventSerializer
    name = request.data.__getitem__('name')
    req_name = re.sub('[^A-Za-z0-9 ]+', '', name)
    req_date = request.data.__getitem__('date')
    req_csv  = request.FILES.get('csv')

    f = TextIOWrapper(req_csv.file, encoding=request.encoding)
    #reader = csv.reader(f, delimiter=',')


    event = Event.objects.create(name=req_name, date=req_date, csv=None)

    new_attendees = []
    captains = []

    try:
        has_header = csv.Sniffer().has_header(f.read(1024))
        f.seek(0)  # rewind
        reader = csv.reader(f, delimiter=',')

        if has_header:
            next(reader)  # skip header row
        else:
            event.delete()
            return JsonResponse({'error': "error with finding csv header"})

        for row in reader:

            if (len(row) != 10):
                Attendee.objects.filter(event=event).delete()
                event.delete()
                return JsonResponse({'error': "Error with CSV: Missing Row"})

            volunteer = Volunteer.objects.create(first_name=row[0],last_name= row[1],city=row[2],state=row[3],
                                                phone=row[4], email=row[5].lower())
            statusCode = 0
            if row[7] == "NO SHOW":
                statusCode = 0
            elif row[7] == "CANCEL":
                statusCode = 1
            elif row[7] == "OK":
                statusCode = 2

            if row[6] == "YES":
                attendee = Attendee.objects.create(volunteer=volunteer, team_captain=None,
                                                   event=event, status=statusCode, assignment_id=row[8],
                                                   job_descrip=row[9])
                captains.append(attendee)
                new_attendees.append(attendee)

            elif row[6] == "NO":
                attendee = Attendee.objects.create(volunteer=volunteer, team_captain=None,
                                                   event=event, status=statusCode, assignment_id=row[8],
                                                   job_descrip=row[9])
                new_attendees.append(attendee)

            # error check to make sure that CSV only YES or NO for CAPTAIN Field
            else:
                Attendee.objects.filter(event=event).delete()
                event.delete()
                return JsonResponse({'error': "Error with CSV, Team Captain field is not 'YES' or 'NO'"})

        #check to make sure that no two team captains have the same assignment id
        for cap in captains:
            for c in captains:
                if cap != c:
                    if cap.assignment_id == c.assignment_id:
                        Attendee.objects.filter(event=event).delete()
                        event.delete()
                        return JsonResponse({'error': 'Error With CSV, 2 Two team captains with same assignment id: ' + c.assignment_id })

        # match attendee with their team captain
        # ensure that each assignment id has a captain associated with it
        for a in new_attendees:
            found = False
            for cap in captains:
                if cap.assignment_id == a.assignment_id:
                    found = True
                    a.team_captain = cap.volunteer
                    a.save()
            if not found:
                Attendee.objects.filter(event=event).delete()
                event.delete()
                return JsonResponse({'error':'Error With CSV, no team captain assigned to group ' + a.assignment_id})

        event.csv = req_csv
        event.save()
        serializer = serializer_class(event, context={'request':request})

        return Response(serializer.data)

    # Generic CSV ERROR
    except csv.Error:
        Attendee.objects.filter(event=event).delete()
        event.delete()
        return JsonResponse({'error': 'Error with CSV'})



def GenerateReportGet(self, request, event):
    event = Event.objects.get(pk=self.kwargs['event'])
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='text/csv')
    file_name = "report_" + event.name
    response['Content-Disposition'] = 'attachment; filename="' + file_name + ".csv" + '"'

    writer = csv.writer(response)
    writer.writerow(['First Name', 'Last Name', 'City', 'State', 'Phone', 'Email', 'Captain', 'Status', 'AssignmentID', 'Job Description'])

    for a in Attendee.objects.filter(event=event):
        if a.status == 0:
            statusCode = "NO SHOW"
        elif a.status == 1:
            statusCode = "CANCEL"
        elif a.status == 2:
            statusCode = "OK"
        if a.volunteer.team_captain == a.volunteer:
            is_cap = "YES"
        else:
            is_cap = "NO"
        writer.writerow([a.volunteer.first_name, a.volunteer.last_name, a.volunteer.city, a.volunteer.state,
                         a.volunteer.phone, a.volunteer.email, is_cap, statusCode, a.assignment_id, a.job_descrip])

    return response

# API Call that Returns List of attendees based upon the event
# and the team captain object

def FilteredAttendeeListGet(self):
    event = self.kwargs['event']
    captain = self.kwargs['teamcap']

    queryset = Attendee.objects.filter(event=event, team_captain=captain)
    return queryset

# API Call that returns a specific Event after name search
# passed in string of event name

def SearchEventGet(self, request, name):
    serializer_class = EventSerializer

    lookup_field = 'name'
    name = self.kwargs['name']

    try:
{'fraction_non_alphanumeric': 0.09912948004078112, 'fraction_numerical': 0.005254489843933809, 'mean_word_length': 2.2072434607645874, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2255183', 'n_tokens_mistral': 3652, 'n_tokens_neox': 3440, 'n_words': 940}