Skip to content

Commit

Permalink
actually save student affiliations
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaney committed May 30, 2024
1 parent ba8239d commit b2893e8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
5 changes: 3 additions & 2 deletions compass/dao/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
30 changes: 18 additions & 12 deletions compass/views/api/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit b2893e8

Please sign in to comment.