Files
Jansen Kantor 7b91f8579f fix: return a 403 instead of a redirect from course_home_api api methods (#32210)
* fix: return 403 on course access error rather than redirect
2023-05-24 16:09:34 +00:00

25 lines
984 B
Python

""" Utilities for views in the course home api"""
from rest_framework.exceptions import PermissionDenied
from lms.djangoapps.courseware.courses import get_course_with_access as base_get_course_with_access
from lms.djangoapps.courseware.exceptions import CourseAccessRedirect
def get_course_or_403(*args, **kwargs):
"""
When we make requests to the various Learner Home API endpoints, we do not want to return the actual redirects,
Instead we should return an error code. The redirect info is returned from the course metadata endpoint and the
URL can be followed by whatever client is calling.
Raises:
- 404 if course is not found
- 403 if the requesting user does not have access to the course
"""
try:
return base_get_course_with_access(*args, **kwargs)
except CourseAccessRedirect as e:
raise PermissionDenied(
detail=e.access_error.user_message,
code=e.access_error.error_code
) from e