refactor: add _redirect_to_course_or_dashboard

This commit is contained in:
Phillip Shiu
2021-09-09 14:10:10 -04:00
parent 2997e23f83
commit 5cf419b9f6

View File

@@ -139,14 +139,11 @@ class ChooseModeView(View):
# If there isn't a verified mode available, then there's nothing
# to do on this page. Send the user to the dashboard.
if not CourseMode.has_verified_mode(modes):
return redirect(reverse('dashboard'))
return self._redirect_to_course_or_dashboard(course, course_key, request.user)
# If a user has already paid, redirect them to the dashboard.
if is_active and (enrollment_mode in CourseMode.VERIFIED_MODES + [CourseMode.NO_ID_PROFESSIONAL_MODE]):
# If the course has started redirect to course home instead
if course.has_started():
return redirect(reverse('openedx.course_experience.course_home', kwargs={'course_id': course_key}))
return redirect(reverse('dashboard'))
return self._redirect_to_course_or_dashboard(course, course_key, request.user)
donation_for_course = request.session.get("donation_for_course", {})
chosen_price = donation_for_course.get(str(course_key), None)
@@ -312,17 +309,11 @@ class ChooseModeView(View):
# system, such as third-party discovery. These workflows result in learners arriving
# directly at this screen, and they will not necessarily be pre-enrolled in the audit mode.
CourseEnrollment.enroll(request.user, course_key, CourseMode.AUDIT)
# If the course has started redirect to course home instead
if course.has_started():
return redirect(reverse('openedx.course_experience.course_home', kwargs={'course_id': course_key}))
return redirect(reverse('dashboard'))
return self._redirect_to_course_or_dashboard(course, course_key, user)
if requested_mode == 'honor':
CourseEnrollment.enroll(user, course_key, mode=requested_mode)
# If the course has started redirect to course home instead
if course.has_started():
return redirect(reverse('openedx.course_experience.course_home', kwargs={'course_id': course_key}))
return redirect(reverse('dashboard'))
return self._redirect_to_course_or_dashboard(course, course_key, user)
mode_info = allowed_modes[requested_mode]
@@ -369,6 +360,24 @@ class ChooseModeView(View):
else:
return None
def _redirect_to_course_or_dashboard(self, course, course_key, user):
"""Perform a redirect to the course if the user is able to access the course.
If the user is not able to access the course, redirect the user to the dashboard.
Args:
course: modulestore object for course
course_key: course_id converted to a course_key
user: request.user, the current user for the request
Returns:
302 to the course if possible or the dashboard if not.
"""
if course.has_started() or user.is_staff:
return redirect(reverse('openedx.course_experience.course_home', kwargs={'course_id': course_key}))
else:
return redirect(reverse('dashboard'))
def create_mode(request, course_id):
"""Add a mode to the course corresponding to the given course ID.