forms.py 1.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
"""
Defines a form for providing validation.
"""
import logging

from django import forms

from contentstore.config.models import CourseNewAssetsPageFlag

from opaque_keys import InvalidKeyError
from xmodule.modulestore.django import modulestore
from opaque_keys.edx.locator import CourseLocator

log = logging.getLogger(__name__)


class CourseNewAssetsPageAdminForm(forms.ModelForm):
18
    """Input form for new asset page enablement, allowing us to verify user input."""
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

    class Meta(object):
        model = CourseNewAssetsPageFlag
        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(course_key.to_deprecated_string())
            raise forms.ValidationError(msg)

        return course_key