Skip to content

Commit

Permalink
fix for coord adding nonexistent user
Browse files Browse the repository at this point in the history
  • Loading branch information
joshualiao committed Oct 31, 2023
1 parent 0af4cb6 commit b5d4127
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
1 change: 1 addition & 0 deletions csm_web/scheduler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def can_enroll_in_course(self, course, bypass_enrollment_time=False):
is_associated = (
self.student_set.filter(active=True, section__mentor__course=course).count()
or self.mentor_set.filter(section__mentor__course=course).count()
or self.id not in course.coordinator_set.values_list("user", flat=True)
)
if bypass_enrollment_time:
return not is_associated
Expand Down
69 changes: 35 additions & 34 deletions csm_web/scheduler/views/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,42 +374,43 @@ 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")
== RestrictedAction.WHITELIST
):
db_actions.append(("create", email))
curstatus["status"] = Status.OK
else:
any_invalid = True
curstatus["status"] = Status.RESTRICTED
# There are no students in the course with this email.
# Check if student exists.
try:
student_user = User.objects.get(email=email)
# Check if the student is associated with the course.
if student_user.id in course_coords:
curstatus["status"] = Status.CONFLICT
curstatus["detail"] = {"reason": "coordinator"}
elif student_user.mentor_set.filter(
course=section.mentor.course
).exists():
curstatus["status"] = Status.CONFLICT
curstatus["detail"] = {"reason": "mentor"}
# Check if the course is available.
elif (
not student_user.is_whitelisted_for(section.mentor.course)
and not email_obj.get("restricted_action")
== RestrictedAction.WHITELIST
):
any_invalid = True
curstatus["status"] = Status.RESTRICTED
# Everything is okay: enroll student.
else:
db_actions.append(("enroll", student))
curstatus["status"] = Status.OK
except User.DoesNotExist:
# If the course is not restricted, or if they are allowed to whitelist, allow.
if (
not section.mentor.course.is_restricted
or email_obj.get("restricted_action")
== RestrictedAction.WHITELIST
):
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(
course=section.mentor.course
).exists():
reason = "mentor"
curstatus["detail"] = {"reason": reason}
curstatus["status"] = Status.RESTRICTED
else: # student_queryset.count() == 1
student = student_queryset.get()

Expand Down

0 comments on commit b5d4127

Please sign in to comment.