diff --git a/csm_web/scheduler/models.py b/csm_web/scheduler/models.py index 9e367ab6..cca8cb44 100644 --- a/csm_web/scheduler/models.py +++ b/csm_web/scheduler/models.py @@ -260,19 +260,15 @@ def save(self, *args, **kwargs): ): if settings.DJANGO_ENV != settings.DEVELOPMENT: logger.info( - ( - " SO automatically created for student" - " %s in course %s for date %s" - ), + " SO automatically created for student" + " %s in course %s for date %s", self.user.email, course.name, now.date(), ) logger.info( - ( - " Attendance automatically created for student" - " %s in course %s for date %s" - ), + " Attendance automatically created for student" + " %s in course %s for date %s", self.user.email, course.name, now.date(), diff --git a/csm_web/scheduler/views/section.py b/csm_web/scheduler/views/section.py index 1a063140..ceecce1a 100644 --- a/csm_web/scheduler/views/section.py +++ b/csm_web/scheduler/views/section.py @@ -356,10 +356,8 @@ class RestrictedAction: if student_queryset.count() > 1: # something bad happened, return immediately with error logger.error( - ( - " Multiple student objects exist in the" - " database (Students %s)!" - ), + " Multiple student objects exist in the" + " database (Students %s)!", student_queryset.all(), ) return Response( @@ -374,42 +372,49 @@ class RestrictedAction: status=status.HTTP_500_INTERNAL_SERVER_ERROR, ) if student_queryset.count() == 0: - # check if the user can actually enroll in the section - student_user, _ = User.objects.get_or_create( - username=email.split("@")[0], email=email - ) - if ( - student_user.id not in course_coords - and student_user.can_enroll_in_course( - section.mentor.course, bypass_enrollment_time=True - ) - ): - # student does not exist yet; we can always create it - db_actions.append(("create", email)) - curstatus["status"] = Status.OK - else: - # user can't enroll; give details on the reason why - curstatus["status"] = Status.CONFLICT - if not student_user.is_whitelisted_for(section.mentor.course): - if ( - email_obj.get("restricted_action") + # There are no students in the course with this email. + # Check if user exists. + try: + user = User.objects.get(email=email) + # Check if the student is associated with the course. + if ( + user.id not in course_coords + and user.can_enroll_in_course( + section.mentor.course, bypass_enrollment_time=True + ) + or ( + not user.is_whitelisted_for(section.mentor.course) + and email_obj.get("restricted_action") == RestrictedAction.WHITELIST - ): - db_actions.append(("create", email)) - curstatus["status"] = Status.OK - else: - any_invalid = True - curstatus["status"] = Status.RESTRICTED + ) + ): + db_actions.append("create", email) + curstatus["status"] = Status.OK else: any_invalid = True - reason = "other" - if student_user.id in course_coords: - reason = "coordinator" - elif student_user.mentor_set.filter( + curstatus["status"] = Status.CONFLICT + if not user.is_whitelisted_for(section.mentor.course): + curstatus["status"] = Status.RESTRICTED + elif user.id in course_coords: + curstatus["detail"] = {"reason": "coordinator"} + elif user.mentor_set.filter( course=section.mentor.course ).exists(): - reason = "mentor" - curstatus["detail"] = {"reason": reason} + curstatus["detail"] = {"reason": "mentor"} + else: + curstatus["detail"] = {"reason": "other"} + except User.DoesNotExist: + # Create user. If they would be allowed to enroll, also create student. + User.objects.create(username=email.split("@")[0], email=email) + if user.can_enroll_in_course( + section.mentor.course, bypass_enrollment_time=True + ): + db_actions.append("create", email) + curstatus["status"] = Status.OK + else: + any_invalid = True + curstatus["status"] = Status.RESTRICTED + curstatus["detail"] = {"reason": "new users restricted"} else: # student_queryset.count() == 1 student = student_queryset.get() @@ -536,10 +541,8 @@ class RestrictedAction: ) student.save() logger.info( - ( - " User %s swapped into Section %s from" - " Section %s" - ), + " User %s swapped into Section %s from" + " Section %s", log_str(student.user), log_str(section), log_str(old_section), @@ -564,26 +567,20 @@ def _student_add(self, request, section): """ if not request.user.can_enroll_in_course(section.mentor.course): logger.warning( - ( - " User %s was unable to enroll in Section %s" - " because they are already involved in this course" - ), + " User %s was unable to enroll in Section %s" + " because they are already involved in this course", log_str(request.user), log_str(section), ) raise PermissionDenied( - ( - "You are already either mentoring for this course or enrolled in a" - " section, or the course is closed for enrollment" - ), + "You are already either mentoring for this course or enrolled in a" + " section, or the course is closed for enrollment", status.HTTP_422_UNPROCESSABLE_ENTITY, ) if section.current_student_count >= section.capacity: logger.warning( - ( - " User %s was unable to enroll in Section %s" - " because it was full" - ), + " User %s was unable to enroll in Section %s" + " because it was full", log_str(request.user), log_str(section), ) @@ -596,18 +593,14 @@ def _student_add(self, request, section): ) if student_queryset.count() > 1: logger.error( - ( - " Multiple student objects exist in the" - " database (Students %s)!" - ), + " Multiple student objects exist in the" + " database (Students %s)!", student_queryset.all(), ) return PermissionDenied( - ( - "An internal error occurred; email mentors@berkeley.edu" - " immediately. (Duplicate students exist in the database (Students" - f" {student_queryset.all()}))" - ), + "An internal error occurred; email mentors@berkeley.edu" + " immediately. (Duplicate students exist in the database (Students" + f" {student_queryset.all()}))", code=status.HTTP_500_INTERNAL_SERVER_ERROR, ) if student_queryset.count() == 1: