forms.py 3.08 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 11
from bulk_email.models import CourseEmailTemplate, COURSE_EMAIL_MESSAGE_BODY_TAG, CourseAuthorization

from courseware.courses import get_course_by_id
12
from xmodule.modulestore import XML_MODULESTORE_TYPE
13
from xmodule.modulestore.django import modulestore
14 15 16 17

log = logging.getLogger(__name__)


18
class CourseEmailTemplateForm(forms.ModelForm):  # pylint: disable=R0924
19 20
    """Form providing validation of CourseEmail templates."""

Sarina Canelake committed
21
    class Meta:  # pylint: disable=C0111
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        model = CourseEmailTemplate

    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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66


class CourseAuthorizationAdminForm(forms.ModelForm):  # pylint: disable=R0924
    """Input form for email enabling, allowing us to verify data."""

    class Meta:  # pylint: disable=C0111
        model = CourseAuthorization

    def clean_course_id(self):
        """Validate the course id"""
        course_id = self.cleaned_data["course_id"]
        try:
            # Just try to get the course descriptor.
            # If we can do that, it's a real course.
            get_course_by_id(course_id, depth=1)
        except Exception as exc:
            msg = 'Error encountered ({0})'.format(str(exc).capitalize())
67
            msg += u' --- Entered course id was: "{0}". '.format(course_id)
68 69 70 71
            msg += 'Please recheck that you have supplied a course id in the format: ORG/COURSE/RUN'
            raise forms.ValidationError(msg)

        # Now, try and discern if it is a Studio course - HTML editor doesn't work with XML courses
72
        is_studio_course = modulestore().get_modulestore_type(course_id) != XML_MODULESTORE_TYPE
73 74 75 76 77 78
        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)
            raise forms.ValidationError(msg)

        return course_id