Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for coord adding nonexistent user #456

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix for coord adding nonexistent user
joshualiao committed Oct 31, 2023
commit b5d4127d4ac4bbdc11d4bf3ee0dc4fc701b03a31
1 change: 1 addition & 0 deletions csm_web/scheduler/models.py
Original file line number Diff line number Diff line change
@@ -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
69 changes: 35 additions & 34 deletions csm_web/scheduler/views/section.py
Original file line number Diff line number Diff line change
@@ -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()