From b2893e8da39199cadc1801647f01ba20dee3f65d Mon Sep 17 00:00:00 2001 From: Jim Laney Date: Thu, 30 May 2024 10:16:12 -0700 Subject: [PATCH] actually save student affiliations --- compass/dao/csv.py | 5 +++-- compass/views/api/student.py | 30 ++++++++++++++++++------------ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/compass/dao/csv.py b/compass/dao/csv.py index 36b8cb85..05aa4d93 100644 --- a/compass/dao/csv.py +++ b/compass/dao/csv.py @@ -88,7 +88,6 @@ def persons_from_file(self, fileobj): row.get(*self.student_identifiers)) if student_number is None: - persons.append({'error': 'Missing student number'}) continue try: @@ -98,6 +97,8 @@ def persons_from_file(self, fileobj): persons.append(person_dict) except PersonNotFoundException: persons.append({ - 'error': f'Student number {student_number} not found'}) + 'student_number': student_number, + 'error': f'Student number not found' + }) return persons diff --git a/compass/views/api/student.py b/compass/views/api/student.py index 1a5e82fd..536bb837 100644 --- a/compass/views/api/student.py +++ b/compass/views/api/student.py @@ -295,18 +295,24 @@ def post(self, request, *args, **kwargs): return self.response_badrequest(f"Invalid CSV file: {ex.error}") for p in person_data: - if hasattr(p, "system_key"): - student, _ = Student.objects.get_or_create( - system_key=p["system_key"]) - sa, _ = StudentAffiliation.objects.get_or_create( - student=student, affiliation=affiliation) - sa.date = current_datetime_utc().date() - sa.cohorts.clear() - sa.cohorts.add(cohort) - sa.save() - logger.info( - f"StudentAffiliation for {student.systemkey} added: " - f"{affiliation.name} ({affiliation.id}), {cohort_str}") + if "error" in p: + continue + + student, _ = Student.objects.get_or_create( + system_key=p["system_key"]) + sa, _ = StudentAffiliation.objects.get_or_create( + student=student, affiliation=affiliation) + sa.date = current_datetime_utc().date() + sa.cohorts.clear() + sa.cohorts.add(cohort) + sa.save() + + logger.info( + f"StudentAffiliation for {student.systemkey} added: " + f"{affiliation.name} ({affiliation.id}), {cohort_str}") + + serializer = StudentAffiliationReadSerializer(sa) + p['affiliation'] = serializer.data return self.response_ok(person_data)