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
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 4 additions & 8 deletions csm_web/scheduler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,15 @@ def save(self, *args, **kwargs):
):
if settings.DJANGO_ENV != settings.DEVELOPMENT:
logger.info(
(
"<SectionOccurrence> SO automatically created for student"
" %s in course %s for date %s"
),
"<SectionOccurrence> SO automatically created for student"
" %s in course %s for date %s",
self.user.email,
course.name,
now.date(),
)
logger.info(
(
"<Attendance> Attendance automatically created for student"
" %s in course %s for date %s"
),
"<Attendance> Attendance automatically created for student"
" %s in course %s for date %s",
self.user.email,
course.name,
now.date(),
Expand Down
113 changes: 53 additions & 60 deletions csm_web/scheduler/views/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,8 @@ class RestrictedAction:
if student_queryset.count() > 1:
# something bad happened, return immediately with error
logger.error(
(
"<Enrollment:Critical> Multiple student objects exist in the"
" database (Students %s)!"
),
"<Enrollment:Critical> Multiple student objects exist in the"
" database (Students %s)!",
student_queryset.all(),
)
return Response(
Expand All @@ -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()

Expand Down Expand Up @@ -536,10 +541,8 @@ class RestrictedAction:
)
student.save()
logger.info(
(
"<Enrollment:Success> User %s swapped into Section %s from"
" Section %s"
),
"<Enrollment:Success> User %s swapped into Section %s from"
" Section %s",
log_str(student.user),
log_str(section),
log_str(old_section),
Expand All @@ -564,26 +567,20 @@ def _student_add(self, request, section):
"""
if not request.user.can_enroll_in_course(section.mentor.course):
logger.warning(
(
"<Enrollment:Failure> User %s was unable to enroll in Section %s"
" because they are already involved in this course"
),
"<Enrollment:Failure> 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(
(
"<Enrollment:Failure> User %s was unable to enroll in Section %s"
" because it was full"
),
"<Enrollment:Failure> User %s was unable to enroll in Section %s"
" because it was full",
log_str(request.user),
log_str(section),
)
Expand All @@ -596,18 +593,14 @@ def _student_add(self, request, section):
)
if student_queryset.count() > 1:
logger.error(
(
"<Enrollment:Critical> Multiple student objects exist in the"
" database (Students %s)!"
),
"<Enrollment:Critical> Multiple student objects exist in the"
" database (Students %s)!",
student_queryset.all(),
)
return PermissionDenied(
(
"An internal error occurred; email [email protected]"
" immediately. (Duplicate students exist in the database (Students"
f" {student_queryset.all()}))"
),
"An internal error occurred; email [email protected]"
" immediately. (Duplicate students exist in the database (Students"
f" {student_queryset.all()}))",
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
if student_queryset.count() == 1:
Expand Down
Loading