Repository: kartoza/prj.app

Issue:
Cannot upload a CSV with attendees list
The CSV is formatted as requested:

![Screenshot_20200229_092926](https://user-images.githubusercontent.com/1951107/75604993-57289500-5ad6-11ea-925b-f17cd3479369.png)



Discussion:
Header row had blank spaces, after removing them upload went ok.
Can you attach the broken one here please that we can trap for these kind of errors.Thanks!
@timlinux 

the wrong CSV was like:

name ,lastname ,email 
john,doe,jhondoe@domain.org
jane,doe,janedoe@domain.org

the accepted one:

name,lastname,email
john,doe,jhondoe@domain.org
jane,doe,janedoe@domain.org



Resolving patch:
diff --git a/django_project/certification/views/attendee.py b/django_project/certification/views/attendee.py
--- a/django_project/certification/views/attendee.py
+++ b/django_project/certification/views/attendee.py
@@ -179,47 +179,58 @@ def post(self, request, *args, **kwargs):
                 reader = csv.DictReader(
                     io.StringIO(attendees_file.read().decode('utf-8'))
                 )
+                fieldnames = reader.fieldnames
                 attendee_count = 0
                 course_attendee_count = 0
+                existing_attendee_count = 0
                 for row in reader:
                     # We should have logic here to first see if the attendee
                     # already exists and if they do, just add them to the
                     # course
-                    attendee = Attendee(
-                        firstname=row['First Name'],
-                        surname=row['Surname'],
-                        email=row['Email'],
-                        certifying_organisation=self.certifying_organisation,
-                        author=self.request.user,
-                    )
                     try:
+                        attendee = Attendee.objects.get(
+                            firstname=row[fieldnames[0]],
+                            surname=row[fieldnames[1]],
+                            email=row[fieldnames[2]],
+                            certifying_organisation=
+                            self.certifying_organisation,
+                        )
+                    except Attendee.DoesNotExist:
+                        attendee = Attendee(
+                            firstname=row[fieldnames[0]],
+                            surname=row[fieldnames[1]],
+                            email=row[fieldnames[2]],
+                            certifying_organisation=
+                            self.certifying_organisation,
+                            author=self.request.user
+                        )
                         attendee.save()
                         attendee_count += 1
-                    except:  # noqa
-                        #  Could not save - probably they exist already
-                        attendee = None
-
-                    if not attendee:
-                        # put more checks in case attendee
-                        # does not already exist
-                        continue
-
-                    course_attendee = CourseAttendee(
-                        attendee=attendee,
-                        course=course,
-                        author=self.request.user,
-                    )
+
                     try:
+                        course_attendee = CourseAttendee.objects.get(
+                            attendee=attendee,
+                            course=course,
+                        )
+                    except CourseAttendee.DoesNotExist:
+                        course_attendee = CourseAttendee(
+                            attendee=attendee,
+                            course=course,
+                            author=self.request.user
+                        )
                         course_attendee.save()
                         course_attendee_count += 1
-                    except:  # noqa
-                        #  They are probably already associated with a course
-                        pass
+                    else:
+                        existing_attendee_count += 1
 
                 self.form_valid_message = (
-                    '%i new attendees were created, and %i attendees were '
-                    'added to the course: % s' % (
-                        attendee_count, course_attendee_count, self.course)
+                    'From the csv: {} attendee already exist in this course, '
+                    '{} new attendees were created, and {} attendees were '
+                    'added to the course: {}'.format(
+                        existing_attendee_count,
+                        attendee_count,
+                        course_attendee_count,
+                        self.course)
                 )
 
                 self.form_invalid_message = (


Test patch: