Skip to content
Snippets Groups Projects
Commit 47234673 authored by Sarina Canelake's avatar Sarina Canelake
Browse files

Validate forms to allow old and new style course key serialization syntax

parent 4df73548
No related branches found
No related tags found
No related merge requests found
from ratelimitbackend import admin
from course_modes.models import CourseMode
from django import forms
admin.site.register(CourseMode)
from opaque_keys import InvalidKeyError
from xmodule.modulestore.django import modulestore
from xmodule.modulestore.keys import CourseKey
from xmodule.modulestore.locations import SlashSeparatedCourseKey
class CourseModeForm(forms.ModelForm):
class Meta:
model = CourseMode
def clean_course_id(self):
course_id = self.cleaned_data['course_id']
try:
course_key = CourseKey.from_string(course_id)
except InvalidKeyError:
try:
course_key = SlashSeparatedCourseKey.from_deprecated_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
class CourseModeAdmin(admin.ModelAdmin):
form = CourseModeForm
admin.site.register(CourseMode, CourseModeAdmin)
......@@ -126,5 +126,5 @@ class CourseMode(models.Model):
def __unicode__(self):
return u"{} : {}, min={}, prices={}".format(
self.course_id, self.mode_slug, self.min_price, self.suggested_prices
self.course_id.to_deprecated_string(), self.mode_slug, self.min_price, self.suggested_prices
)
......@@ -11,6 +11,7 @@ import socket
from xmodule.modulestore.django import modulestore
from opaque_keys import InvalidKeyError
from xmodule.modulestore.keys import CourseKey
from xmodule.modulestore.locations import SlashSeparatedCourseKey
......@@ -24,31 +25,24 @@ class EmbargoedCourseForm(forms.ModelForm): # pylint: disable=incomplete-protoc
"""Validate the course id"""
cleaned_id = self.cleaned_data["course_id"]
try:
course_id = SlashSeparatedCourseKey.from_deprecated_string(cleaned_id)
course_key = CourseKey.from_string(cleaned_id)
except InvalidKeyError:
try:
course_key = SlashSeparatedCourseKey.from_deprecated_string(cleaned_id)
except InvalidKeyError:
msg = 'COURSE NOT FOUND'
msg += u' --- Entered course id was: "{0}". '.format(cleaned_id)
msg += 'Please recheck that you have supplied a valid course id.'
raise forms.ValidationError(msg)
if not modulestore().has_course(course_key):
msg = 'COURSE NOT FOUND'
msg += u' --- Entered course id was: "{0}". '.format(cleaned_id)
msg += 'Please recheck that you have supplied a valid course id.'
raise forms.ValidationError(msg)
# Try to get the course. If this returns None, it's not a real course
try:
course = modulestore().get_course(course_id)
except ValueError:
msg = 'COURSE NOT FOUND'
msg += u' --- Entered course id was: "{0}". '.format(course_id.to_deprecated_string())
msg += 'Please recheck that you have supplied a valid course id.'
raise forms.ValidationError(msg)
if not course:
msg = 'COURSE NOT FOUND'
msg += u' --- Entered course id was: "{0}". '.format(course_id.to_deprecated_string())
msg += u' --- Entered course id was: "{0}". '.format(course_key.to_deprecated_string())
msg += 'Please recheck that you have supplied a valid course id.'
raise forms.ValidationError(msg)
return course_id
return course_key
class EmbargoedStateForm(forms.ModelForm): # pylint: disable=incomplete-protocol
......
......@@ -11,6 +11,8 @@ from bulk_email.models import CourseEmailTemplate, COURSE_EMAIL_MESSAGE_BODY_TAG
from opaque_keys import InvalidKeyError
from xmodule.modulestore import XML_MODULESTORE_TYPE
from xmodule.modulestore.django import modulestore
from opaque_keys import InvalidKeyError
from xmodule.modulestore.keys import CourseKey
from xmodule.modulestore.locations import SlashSeparatedCourseKey
log = logging.getLogger(__name__)
......@@ -60,24 +62,27 @@ class CourseAuthorizationAdminForm(forms.ModelForm): # pylint: disable=R0924
"""Validate the course id"""
cleaned_id = self.cleaned_data["course_id"]
try:
course_id = SlashSeparatedCourseKey.from_deprecated_string(cleaned_id)
course_key = CourseKey.from_string(cleaned_id)
except InvalidKeyError:
msg = u'Course id invalid.'
msg += u' --- Entered course id was: "{0}". '.format(cleaned_id)
msg += 'Please recheck that you have supplied a valid course id.'
raise forms.ValidationError(msg)
if not modulestore().has_course(course_id):
try:
course_key = SlashSeparatedCourseKey.from_deprecated_string(cleaned_id)
except InvalidKeyError:
msg = u'Course id invalid.'
msg += u' --- Entered course id was: "{0}". '.format(cleaned_id)
msg += 'Please recheck that you have supplied a valid course id.'
raise forms.ValidationError(msg)
if not modulestore().has_course(course_key):
msg = u'COURSE NOT FOUND'
msg += u' --- Entered course id was: "{0}". '.format(course_id.to_deprecated_string())
msg += u' --- Entered course id was: "{0}". '.format(course_key.to_deprecated_string())
msg += 'Please recheck that you have supplied a valid course id.'
raise forms.ValidationError(msg)
# Now, try and discern if it is a Studio course - HTML editor doesn't work with XML courses
is_studio_course = modulestore().get_modulestore_type(course_id) != XML_MODULESTORE_TYPE
is_studio_course = modulestore().get_modulestore_type(course_key) != XML_MODULESTORE_TYPE
if not is_studio_course:
msg = "Course Email feature is only available for courses authored in Studio. "
msg += '"{0}" appears to be an XML backed course.'.format(course_id.to_deprecated_string())
msg += '"{0}" appears to be an XML backed course.'.format(course_key.to_deprecated_string())
raise forms.ValidationError(msg)
return course_id.to_deprecated_string()
return course_key
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment