Replace all clean_course_id form methods with common method.
This commit is contained in:
@@ -19,6 +19,7 @@ from course_modes.models import CourseMode, CourseModeExpirationConfig
|
||||
# but the test suite for Studio will fail because
|
||||
# the verification deadline table won't exist.
|
||||
from lms.djangoapps.verify_student import models as verification_models
|
||||
from openedx.core.lib.courses import clean_course_id
|
||||
from util.date_utils import get_time_display
|
||||
from xmodule.modulestore.django import modulestore
|
||||
|
||||
@@ -92,16 +93,10 @@ class CourseModeForm(forms.ModelForm):
|
||||
)
|
||||
|
||||
def clean_course_id(self):
|
||||
course_id = self.cleaned_data['course']
|
||||
try:
|
||||
course_key = CourseKey.from_string(course_id)
|
||||
except InvalidKeyError:
|
||||
raise forms.ValidationError("Cannot make a valid CourseKey from id {}!".format(course_id))
|
||||
|
||||
if not modulestore().has_course(course_key):
|
||||
raise forms.ValidationError("Cannot find course with id {} in the modulestore".format(course_id))
|
||||
|
||||
return course_key
|
||||
"""
|
||||
Validate the course id
|
||||
"""
|
||||
return clean_course_id(self)
|
||||
|
||||
def clean__expiration_datetime(self):
|
||||
"""
|
||||
|
||||
@@ -9,6 +9,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
from opaque_keys import InvalidKeyError
|
||||
from opaque_keys.edx.keys import CourseKey
|
||||
|
||||
from openedx.core.lib.courses import clean_course_id
|
||||
from student.models import (
|
||||
CourseAccessRole,
|
||||
CourseEnrollment,
|
||||
@@ -41,23 +42,10 @@ class CourseAccessRoleForm(forms.ModelForm):
|
||||
|
||||
def clean_course_id(self):
|
||||
"""
|
||||
Checking course-id format and course exists in module store.
|
||||
This field can be null.
|
||||
Validate the course id
|
||||
"""
|
||||
if self.cleaned_data['course_id']:
|
||||
course_id = self.cleaned_data['course_id']
|
||||
|
||||
try:
|
||||
course_key = CourseKey.from_string(course_id)
|
||||
except InvalidKeyError:
|
||||
raise forms.ValidationError(u"Invalid CourseID. Please check the format and re-try.")
|
||||
|
||||
if not modulestore().has_course(course_key):
|
||||
raise forms.ValidationError(u"Cannot find course with id {} in the modulestore".format(course_id))
|
||||
|
||||
return course_key
|
||||
|
||||
return None
|
||||
return clean_course_id(self)
|
||||
|
||||
def clean_org(self):
|
||||
"""If org and course-id exists then Check organization name
|
||||
|
||||
@@ -124,7 +124,7 @@ class AdminCourseRolesPageTest(SharedModuleStoreTestCase):
|
||||
response = self.client.post(reverse('admin:student_courseaccessrole_add'), data=data)
|
||||
self.assertContains(
|
||||
response,
|
||||
'Cannot find course with id {} in the modulestore'.format(
|
||||
'Course not found. Entered course id was: "{}".'.format(
|
||||
course
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user