forms.py 4.44 KB
Newer Older
Sarina Canelake committed
1 2 3
"""
Defines a form for providing validation of CourseEmail templates.
"""
4 5 6 7 8
import logging

from django import forms
from django.core.exceptions import ValidationError

9 10
from bulk_email.models import CourseEmailTemplate, COURSE_EMAIL_MESSAGE_BODY_TAG, CourseAuthorization

11
from opaque_keys import InvalidKeyError
12
from xmodule.modulestore import ModuleStoreEnum
13
from xmodule.modulestore.django import modulestore
14 15
from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locations import SlashSeparatedCourseKey
16 17 18 19

log = logging.getLogger(__name__)


20
class CourseEmailTemplateForm(forms.ModelForm):
21 22
    """Form providing validation of CourseEmail templates."""

23
    name = forms.CharField(required=False)
24

25
    class Meta(object):
26
        model = CourseEmailTemplate
27
        fields = ('html_template', 'plain_template', 'name')
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

    def _validate_template(self, template):
        """Check the template for required tags."""
        index = template.find(COURSE_EMAIL_MESSAGE_BODY_TAG)
        if index < 0:
            msg = 'Missing tag: "{}"'.format(COURSE_EMAIL_MESSAGE_BODY_TAG)
            log.warning(msg)
            raise ValidationError(msg)
        if template.find(COURSE_EMAIL_MESSAGE_BODY_TAG, index + 1) >= 0:
            msg = 'Multiple instances of tag: "{}"'.format(COURSE_EMAIL_MESSAGE_BODY_TAG)
            log.warning(msg)
            raise ValidationError(msg)
        # TODO: add more validation here, including the set of known tags
        # for which values will be supplied.  (Email will fail if the template
        # uses tags for which values are not supplied.)

    def clean_html_template(self):
        """Validate the HTML template."""
        template = self.cleaned_data["html_template"]
        self._validate_template(template)
        return template

    def clean_plain_template(self):
        """Validate the plaintext template."""
        template = self.cleaned_data["plain_template"]
        self._validate_template(template)
        return template
55

56 57
    def clean_name(self):
        """Validate the name field. Enforce uniqueness constraint on 'name' field"""
58 59 60 61 62

        # Note that we get back a blank string in the Form for an empty 'name' field
        # we want those to be set to None in Python and NULL in the database
        name = self.cleaned_data.get("name").strip() or None

63 64 65 66 67 68 69 70 71 72 73 74
        # if we are creating a new CourseEmailTemplate, then we need to
        # enforce the uniquess constraint as part of the Form validation
        if not self.instance.pk:
            try:
                CourseEmailTemplate.get_template(name)
                # already exists, this is no good
                raise ValidationError('Name of "{}" already exists, this must be unique.'.format(name))
            except CourseEmailTemplate.DoesNotExist:
                # this is actually the successful validation
                pass
        return name

75

76
class CourseAuthorizationAdminForm(forms.ModelForm):
77 78
    """Input form for email enabling, allowing us to verify data."""

79
    class Meta(object):
80
        model = CourseAuthorization
81
        fields = '__all__'
82 83 84

    def clean_course_id(self):
        """Validate the course id"""
85
        cleaned_id = self.cleaned_data["course_id"]
86
        try:
87
            course_key = CourseKey.from_string(cleaned_id)
88
        except InvalidKeyError:
89 90 91
            try:
                course_key = SlashSeparatedCourseKey.from_deprecated_string(cleaned_id)
            except InvalidKeyError:
92
                msg = u'Course id invalid.'
93 94 95 96 97
                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):
98
            msg = u'COURSE NOT FOUND'
99
            msg += u' --- Entered course id was: "{0}". '.format(course_key.to_deprecated_string())
100
            msg += 'Please recheck that you have supplied a valid course id.'
101 102 103
            raise forms.ValidationError(msg)

        # Now, try and discern if it is a Studio course - HTML editor doesn't work with XML courses
104
        is_studio_course = modulestore().get_modulestore_type(course_key) != ModuleStoreEnum.Type.xml
105 106
        if not is_studio_course:
            msg = "Course Email feature is only available for courses authored in Studio. "
107
            msg += '"{0}" appears to be an XML backed course.'.format(course_key.to_deprecated_string())
108 109
            raise forms.ValidationError(msg)

110
        return course_key