models.py 9.87 KB
Newer Older
1 2
"""
Models for bulk email
3 4 5 6 7 8 9 10 11 12

WE'RE USING MIGRATIONS!

If you make changes to this model, be sure to create an appropriate migration
file and check it in at the same time as your model changes. To do that,

1. Go to the edx-platform dir
2. ./manage.py lms schemamigration bulk_email --auto description_of_your_change
3. Add the migration file created in edx-platform/lms/djangoapps/bulk_email/migrations/

13
"""
14
import logging
15
from django.conf import settings
16
from django.contrib.auth.models import User
17
from django.db import models, transaction
18

19 20
from html_to_text import html_to_text
from mail_utils import wrap_message
21

22
from xmodule_django.models import CourseKeyField
23
from util.keyword_substitution import substitute_keywords_with_data
24

25 26
log = logging.getLogger(__name__)

27 28 29 30 31 32 33
# Bulk email to_options - the send to options that users can
# select from when they send email.
SEND_TO_MYSELF = 'myself'
SEND_TO_STAFF = 'staff'
SEND_TO_ALL = 'all'
TO_OPTIONS = [SEND_TO_MYSELF, SEND_TO_STAFF, SEND_TO_ALL]

34 35 36 37 38 39

class Email(models.Model):
    """
    Abstract base class for common information for an email.
    """
    sender = models.ForeignKey(User, default=1, blank=True, null=True)
40
    slug = models.CharField(max_length=128, db_index=True)
41 42
    subject = models.CharField(max_length=128, blank=True)
    html_message = models.TextField(null=True, blank=True)
43
    text_message = models.TextField(null=True, blank=True)
44 45 46
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

47
    class Meta:  # pylint: disable=missing-docstring
48 49
        abstract = True

50

51
class CourseEmail(Email):
52 53 54
    """
    Stores information for an email to a course.
    """
55 56 57 58 59 60 61 62 63
    # Three options for sending that we provide from the instructor dashboard:
    # * Myself: This sends an email to the staff member that is composing the email.
    #
    # * Staff and instructors: This sends an email to anyone in the staff group and
    #   anyone in the instructor group
    #
    # * All: This sends an email to anyone enrolled in the course, with any role
    #   (student, staff, or instructor)
    #
64
    TO_OPTION_CHOICES = (
65 66 67
        (SEND_TO_MYSELF, 'Myself'),
        (SEND_TO_STAFF, 'Staff and instructors'),
        (SEND_TO_ALL, 'All')
68
    )
69
    course_id = CourseKeyField(max_length=255, db_index=True)
70
    to_option = models.CharField(max_length=64, choices=TO_OPTION_CHOICES, default=SEND_TO_MYSELF)
71 72
    template_name = models.CharField(null=True, max_length=255)
    from_addr = models.CharField(null=True, max_length=255)
73 74 75 76

    def __unicode__(self):
        return self.subject

77
    @classmethod
78
    def create(cls, course_id, sender, to_option, subject, html_message, text_message=None, template_name=None, from_addr=None):
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        """
        Create an instance of CourseEmail.

        The CourseEmail.save_now method makes sure the CourseEmail entry is committed.
        When called from any view that is wrapped by TransactionMiddleware,
        and thus in a "commit-on-success" transaction, an autocommit buried within here
        will cause any pending transaction to be committed by a successful
        save here.  Any future database operations will take place in a
        separate transaction.
        """
        # automatically generate the stripped version of the text from the HTML markup:
        if text_message is None:
            text_message = html_to_text(html_message)

        # perform some validation here:
        if to_option not in TO_OPTIONS:
            fmt = 'Course email being sent to unrecognized to_option: "{to_option}" for "{course}", subject "{subject}"'
            msg = fmt.format(to_option=to_option, course=course_id, subject=subject)
            raise ValueError(msg)

        # create the task, then save it immediately:
        course_email = cls(
            course_id=course_id,
            sender=sender,
            to_option=to_option,
            subject=subject,
            html_message=html_message,
            text_message=text_message,
107 108
            template_name=template_name,
            from_addr=from_addr,
109 110 111 112 113 114 115 116
        )
        course_email.save_now()

        return course_email

    @transaction.autocommit
    def save_now(self):
        """
117
        Writes CourseEmail immediately, ensuring the transaction is committed.
118 119 120 121 122 123 124 125 126 127

        Autocommit annotation makes sure the database entry is committed.
        When called from any view that is wrapped by TransactionMiddleware,
        and thus in a "commit-on-success" transaction, this autocommit here
        will cause any pending transaction to be committed by a successful
        save here.  Any future database operations will take place in a
        separate transaction.
        """
        self.save()

128 129 130 131 132
    def get_template(self):
        """
        Returns the corresponding CourseEmailTemplate for this CourseEmail.
        """
        return CourseEmailTemplate.get_template(name=self.template_name)
133

134

135 136
class Optout(models.Model):
    """
137
    Stores users that have opted out of receiving emails from a course.
138
    """
139 140 141
    # Allowing null=True to support data migration from email->user.
    # We need to first create the 'user' column with some sort of default in order to run the data migration,
    # and given the unique index, 'null' is the best default value.
142
    user = models.ForeignKey(User, db_index=True, null=True)
143
    course_id = CourseKeyField(max_length=255, db_index=True)
144

145
    class Meta:  # pylint: disable=missing-docstring
146
        unique_together = ('user', 'course_id')
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164


# Defines the tag that must appear in a template, to indicate
# the location where the email message body is to be inserted.
COURSE_EMAIL_MESSAGE_BODY_TAG = '{{message_body}}'


class CourseEmailTemplate(models.Model):
    """
    Stores templates for all emails to a course to use.

    This is expected to be a singleton, to be shared across all courses.
    Initialization takes place in a migration that in turn loads a fixture.
    The admin console interface disables add and delete operations.
    Validation is handled in the CourseEmailTemplateForm class.
    """
    html_template = models.TextField(null=True, blank=True)
    plain_template = models.TextField(null=True, blank=True)
165
    name = models.CharField(null=True, max_length=255, unique=True, blank=True)
166 167

    @staticmethod
168
    def get_template(name=None):
169 170 171 172 173
        """
        Fetch the current template

        If one isn't stored, an exception is thrown.
        """
174
        try:
175
            return CourseEmailTemplate.objects.get(name=name)
176 177 178
        except CourseEmailTemplate.DoesNotExist:
            log.exception("Attempting to fetch a non-existent course email template")
            raise
179 180 181 182 183 184 185 186 187 188

    @staticmethod
    def _render(format_string, message_body, context):
        """
        Create a text message using a template, message body and context.

        Convert message body (`message_body`) into an email message
        using the provided template.  The template is a format string,
        which is rendered using format() with the provided `context` dict.

189 190 191
        Any keywords encoded in the form %%KEYWORD%% found in the message
        body are subtituted with user data before the body is inserted into
        the template.
192 193 194 195 196

        Output is returned as a unicode string.  It is not encoded as utf-8.
        Such encoding is left to the email code, which will use the value
        of settings.DEFAULT_CHARSET to encode the message.
        """
197 198 199 200 201

        # Substitute all %%-encoded keywords in the message body
        if 'user_id' in context and 'course_id' in context:
            message_body = substitute_keywords_with_data(message_body, context['user_id'], context['course_id'])

202
        result = format_string.format(**context)
203

204 205 206 207 208 209
        # Note that the body tag in the template will now have been
        # "formatted", so we need to do the same to the tag being
        # searched for.
        message_body_tag = COURSE_EMAIL_MESSAGE_BODY_TAG.format()
        result = result.replace(message_body_tag, message_body, 1)

210 211
        # finally, return the result, after wrapping long lines and without converting to an encoded byte array.
        return wrap_message(result)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

    def render_plaintext(self, plaintext, context):
        """
        Create plain text message.

        Convert plain text body (`plaintext`) into plaintext email message using the
        stored plain template and the provided `context` dict.
        """
        return CourseEmailTemplate._render(self.plain_template, plaintext, context)

    def render_htmltext(self, htmltext, context):
        """
        Create HTML text message.

        Convert HTML text body (`htmltext`) into HTML email message using the
        stored HTML template and the provided `context` dict.
        """
        return CourseEmailTemplate._render(self.html_template, htmltext, context)
230 231 232 233 234 235 236


class CourseAuthorization(models.Model):
    """
    Enable the course email feature on a course-by-course basis.
    """
    # The course that these features are attached to.
237
    course_id = CourseKeyField(max_length=255, db_index=True, unique=True)
238 239 240 241 242 243 244 245 246 247 248

    # Whether or not to enable instructor email
    email_enabled = models.BooleanField(default=False)

    @classmethod
    def instructor_email_enabled(cls, course_id):
        """
        Returns whether or not email is enabled for the given course id.

        If email has not been explicitly enabled, returns False.
        """
249
        # If settings.FEATURES['REQUIRE_COURSE_EMAIL_AUTH'] is
250
        # set to False, then we enable email for every course.
251
        if not settings.FEATURES['REQUIRE_COURSE_EMAIL_AUTH']:
252 253 254 255 256 257 258 259 260 261 262 263
            return True

        try:
            record = cls.objects.get(course_id=course_id)
            return record.email_enabled
        except cls.DoesNotExist:
            return False

    def __unicode__(self):
        not_en = "Not "
        if self.email_enabled:
            not_en = ""
264 265
        # pylint: disable=no-member
        return u"Course '{}': Instructor Email {}Enabled".format(self.course_id.to_deprecated_string(), not_en)