Commit 055f7fa4 by Jason Bau

Make sneakpeek OpaqueKeys compliant

Conflicts:
	common/djangoapps/student/views.py
	lms/djangoapps/courseware/views.py
parent c2bce25e
......@@ -581,7 +581,9 @@ def _create_and_login_nonregistered_user(request):
@require_POST
def setup_sneakpeek(request, course_id):
if not CoursePreference.course_allows_nonregistered_access(course_id):
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
if not CoursePreference.course_allows_nonregistered_access(course_key):
return HttpResponseForbidden("Cannot access the course")
if not request.user.is_authenticated():
# if there's no user, create a nonregistered user
......@@ -592,13 +594,13 @@ def setup_sneakpeek(request, course_id):
_create_and_login_nonregistered_user(request)
can_enroll, error_msg = _check_can_enroll_in_course(request.user,
course_id,
course_key,
access_type='within_enrollment_period')
if not can_enroll:
log.error(error_msg)
return HttpResponseBadRequest(error_msg)
CourseEnrollment.enroll(request.user, course_id)
CourseEnrollment.enroll(request.user, course_key)
return HttpResponse("OK. Allowed sneakpeek")
......@@ -712,18 +714,16 @@ def change_enrollment(request):
return HttpResponseBadRequest(_("Enrollment action is invalid"))
def _check_can_enroll_in_course(user, course_id, access_type="enroll"):
def _check_can_enroll_in_course(user, course_key, access_type="enroll"):
"""
Refactored check for user being able to enroll in course
Returns (bool, error_message), where error message is only applicable if bool == False
"""
try:
course = modulestore().get_course(course_id)
course = modulestore().get_course(course_key)
except ItemNotFoundError:
log.warning(
"User {0} tried to enroll in non-existent course {1}"
.format(user.username, course_id),
)
log.warning("User {0} tried to enroll in non-existent course {1}"
.format(user.username, course_key))
return False, _("Course id is invalid")
if not has_access(user, access_type, course):
......
......@@ -267,7 +267,7 @@ class CoursePreference(models.Model):
of the course xmodule (advanced settings).
A good example is whether this course allows nonregistered users to access it.
"""
course_id = models.CharField(max_length=255, db_index=True)
course_id = CourseKeyField(max_length=255, db_index=True)
pref_key = models.CharField(max_length=255)
pref_value = models.CharField(max_length=255, null=True)
......
......@@ -604,7 +604,7 @@ def course_about(request, course_id):
# 2) course specifies it's okay
# 3) request.user is not a registered user.
sneakpeek_allowed = (has_access(request.user, 'within_enrollment_period', course) and
CoursePreference.course_allows_nonregistered_access(course.id.to_deprecated_string()) and
CoursePreference.course_allows_nonregistered_access(course_key) and
not UserProfile.has_registered(request.user))
# see if we have already filled up all allowed enrollments
......
......@@ -25,7 +25,7 @@ def url_class(is_active):
<p class="unauth-warning">
<%
if settings.FEATURES.get('RESTRICT_ENROLL_BY_REG_METHOD'):
reg_url = reverse('course-specific-register', args=[course.id])
reg_url = reverse('course-specific-register', args=[course.id.to_deprecated_string()])
else:
reg_url = reverse('register_user')
%>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment