Skip to content

Commit

Permalink
feat: [AXM-549] Add query limit to User Enrollments
Browse files Browse the repository at this point in the history
  • Loading branch information
KyryloKireiev committed Oct 24, 2024
1 parent 2077249 commit bc71be4
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lms/djangoapps/mobile_api/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ class UserEnrollmentsStatus(views.APIView):
less than 30 days ago or has progressed in the course in the last 30 days.
Otherwise, the registration is considered inactive.
USER_ENROLLMENTS_LIMIT - adds users enrollments query limit to
safe API from possible DDOS attacks.
**Example Request**
GET /api/mobile/{api_version}/users/<user_name>/enrollments_status/
Expand Down Expand Up @@ -586,6 +589,9 @@ class UserEnrollmentsStatus(views.APIView):
]
```
"""

USER_ENROLLMENTS_LIMIT = 500

def get(self, request, *args, **kwargs) -> Response:
"""
Gets user's enrollments status.
Expand Down Expand Up @@ -613,7 +619,12 @@ def _build_enrollments_status_dict(
Builds list with dictionaries with user's enrolments statuses.
"""
user = get_object_or_404(User, username=username)
user_enrollments = CourseEnrollment.enrollments_for_user(user).select_related('course')
user_enrollments = (
CourseEnrollment
.enrollments_for_user(user)
.select_related('course')
[:self.USER_ENROLLMENTS_LIMIT]
)
mobile_available = [
enrollment for enrollment in user_enrollments
if is_mobile_available_for_user(user, enrollment.course_overview)
Expand Down

0 comments on commit bc71be4

Please sign in to comment.