Replace all clean_course_id form methods with common method.

This commit is contained in:
John Eskew
2018-01-18 14:25:57 -05:00
committed by Troy Sankey
parent 875a127f84
commit 27edca3c5e
12 changed files with 42 additions and 126 deletions

View File

@@ -4,13 +4,13 @@ Defines a form for providing validation.
import logging
from django import forms
from opaque_keys import InvalidKeyError
from opaque_keys.edx.locator import CourseLocator
from six import text_type
from contentstore.config.models import CourseNewAssetsPageFlag
from opaque_keys import InvalidKeyError
from six import text_type
from openedx.core.lib.courses import clean_course_id
from xmodule.modulestore.django import modulestore
from opaque_keys.edx.locator import CourseLocator
log = logging.getLogger(__name__)
@@ -23,16 +23,7 @@ class CourseNewAssetsPageAdminForm(forms.ModelForm):
fields = '__all__'
def clean_course_id(self):
"""Validate the course id"""
cleaned_id = self.cleaned_data["course_id"]
try:
course_key = CourseLocator.from_string(cleaned_id)
except InvalidKeyError:
msg = u'Course id invalid. Entered course id was: "{0}."'.format(cleaned_id)
raise forms.ValidationError(msg)
if not modulestore().has_course(course_key):
msg = u'Course not found. Entered course id was: "{0}". '.format(text_type(course_key))
raise forms.ValidationError(msg)
return course_key
"""
Validate the course id
"""
return clean_course_id(self)