Skip to content

Commit

Permalink
fix: filter enrollments instead of get in defer_enrollment (#3215)
Browse files Browse the repository at this point in the history
* feat: only look from active enrollments while deferring enrollment

* fix: raise ValidationError if user is not enrolled in given run

---------

Co-authored-by: Muhammad Arslan <[email protected]>
  • Loading branch information
arslanashraf7 and marslanabdulrauf authored Jan 28, 2025
1 parent 645b5b4 commit b587cb4
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions courses/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,17 @@ def defer_enrollment(
(CourseRunEnrollment, CourseRunEnrollment): The deactivated enrollment paired with the
new enrollment that was the target of the deferral
"""
from_enrollment = CourseRunEnrollment.all_objects.get(
user=user, run__courseware_id=from_courseware_id
from_enrollment = (
CourseRunEnrollment.all_objects.filter(
user=user, run__courseware_id=from_courseware_id
)
.order_by("-created_on")
.first()
)
if not from_enrollment:
raise ValidationError(
f"User is not enrolled in course run '{from_courseware_id}'" # noqa: EM102
)
if not force and not from_enrollment.active:
raise ValidationError(
f"Cannot defer from inactive enrollment (id: {from_enrollment.id}, run: {from_enrollment.run.courseware_id}, user: {user.email}). " # noqa: EM102
Expand Down

0 comments on commit b587cb4

Please sign in to comment.