def create( cls, course_id, sender, to_option, subject, html_message, text_message=None, template_name=None, from_addr=None): """ Create an instance of CourseEmail. """ # 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, template_name=template_name, from_addr=from_addr, ) course_email.save() return course_email
def create(cls, course_id, sender, to_option, subject, html_message, text_message=None, template_name=None, from_addr=None): """ 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, template_name=template_name, from_addr=from_addr, ) course_email.save_now() return course_email
def create( cls, course_id, sender, targets, subject, html_message, text_message=None, template_name=None, from_addr=None, ): """ Create an instance of CourseEmail. """ # automatically generate the stripped version of the text from the HTML markup: if text_message is None: text_message = html_to_text(html_message) # create the task, then save it immediately: course_email = cls( course_id=course_id, subject=subject, html_message=html_message, text_message=text_message, template_name=template_name, ) course_email.save( ) # Must exist in db before setting M2M relationship values course_email.targets.add(*new_targets) course_email.save() return course_email
def create(cls, course_id, sender, targets, selected, subject, html_message, text_message=None, template_name=None, from_addr=None, cohort_name=None): """ Create an instance of CourseEmail. """ # automatically generate the stripped version of the text from the HTML markup: if text_message is None: text_message = html_to_text(html_message) new_targets = [] for target in targets: # split target, to handle cohort:cohort_name target_split = target.split(':', 1) # Ensure our desired target exists if target_split[0] not in EMAIL_TARGETS: fmt = 'Course email being sent to unrecognized target: "{target}" for "{course}", subject "{subject}"' msg = fmt.format(target=target, course=course_id, subject=subject) raise ValueError(msg) elif target_split[0] == SEND_TO_COHORT: # target_split[1] will contain the cohort name cohort = CohortTarget.ensure_valid_cohort( target_split[1], course_id) new_target, _ = CohortTarget.objects.get_or_create( target_type=target_split[0], cohort=cohort) else: new_target, _ = Target.objects.get_or_create( target_type=target_split[0]) new_targets.append(new_target) # create the task, then save it immediately: course_email = cls( course_id=course_id, sender=sender, subject=subject, html_message=html_message, text_message=text_message, selected_emails=selected, template_name=template_name, from_addr=from_addr, ) course_email.save( ) # Must exist in db before setting M2M relationship values course_email.targets.add(*new_targets) course_email.save() return course_email
def create( cls, course_id, sender, targets, subject, html_message, text_message=None, template_name=None, from_addr=None): """ Create an instance of CourseEmail. """ # automatically generate the stripped version of the text from the HTML markup: if text_message is None: text_message = html_to_text(html_message) new_targets = [] for target in targets: # split target, to handle cohort:cohort_name and track:mode_slug target_split = target.split(':', 1) # Ensure our desired target exists if target_split[0] not in EMAIL_TARGETS: # lint-amnesty, pylint: disable=no-else-raise fmt = u'Course email being sent to unrecognized target: "{target}" for "{course}", subject "{subject}"' msg = fmt.format(target=target, course=course_id, subject=subject).encode('utf8') raise ValueError(msg) elif target_split[0] == SEND_TO_COHORT: # target_split[1] will contain the cohort name cohort = CohortTarget.ensure_valid_cohort(target_split[1], course_id) new_target, _ = CohortTarget.objects.get_or_create(target_type=target_split[0], cohort=cohort) elif target_split[0] == SEND_TO_TRACK: # target_split[1] contains the desired mode slug CourseModeTarget.ensure_valid_mode(target_split[1], course_id) # There could exist multiple CourseModes that match this query, due to differing currency types. # The currencies do not affect user lookup though, so we can just use the first result. mode = CourseMode.objects.filter(course_id=course_id, mode_slug=target_split[1])[0] new_target, _ = CourseModeTarget.objects.get_or_create(target_type=target_split[0], track=mode) else: new_target, _ = Target.objects.get_or_create(target_type=target_split[0]) new_targets.append(new_target) # create the task, then save it immediately: course_email = cls( course_id=course_id, sender=sender, subject=subject, html_message=html_message, text_message=text_message, template_name=template_name, from_addr=from_addr, ) course_email.save() # Must exist in db before setting M2M relationship values course_email.targets.add(*new_targets) course_email.save() return course_email
def create( cls, course_id, sender, targets, subject, html_message, text_message=None, template_name=None, from_addr=None): """ Create an instance of CourseEmail. """ # automatically generate the stripped version of the text from the HTML markup: if text_message is None: text_message = html_to_text(html_message) new_targets = [] for target in targets: # split target, to handle cohort:cohort_name and track:mode_slug target_split = target.split(':', 1) # Ensure our desired target exists if target_split[0] not in EMAIL_TARGETS: fmt = 'Course email being sent to unrecognized target: "{target}" for "{course}", subject "{subject}"' msg = fmt.format(target=target, course=course_id, subject=subject) raise ValueError(msg) elif target_split[0] == SEND_TO_COHORT: # target_split[1] will contain the cohort name cohort = CohortTarget.ensure_valid_cohort(target_split[1], course_id) new_target, _ = CohortTarget.objects.get_or_create(target_type=target_split[0], cohort=cohort) elif target_split[0] == SEND_TO_TRACK: # target_split[1] contains the desired mode slug CourseModeTarget.ensure_valid_mode(target_split[1], course_id) # There could exist multiple CourseModes that match this query, due to differing currency types. # The currencies do not affect user lookup though, so we can just use the first result. mode = CourseMode.objects.filter(course_id=course_id, mode_slug=target_split[1])[0] new_target, _ = CourseModeTarget.objects.get_or_create(target_type=target_split[0], track=mode) else: new_target, _ = Target.objects.get_or_create(target_type=target_split[0]) new_targets.append(new_target) # create the task, then save it immediately: course_email = cls( course_id=course_id, sender=sender, subject=subject, html_message=html_message, text_message=text_message, template_name=template_name, from_addr=from_addr, ) course_email.save() # Must exist in db before setting M2M relationship values course_email.targets.add(*new_targets) course_email.save() return course_email
def test_create_course_email(self): """ Happy path test for the `create_course_email` function. Verifies the creation of a CourseEmail instance with the bare minimum information required for the function call. """ course_email = create_course_email( self.course.id, self.instructor, self.target, self.subject, self.html_message, ) assert course_email.sender.id == self.instructor.id assert course_email.subject == self.subject assert course_email.html_message == self.html_message assert course_email.course_id == self.course.id assert course_email.text_message == html_to_text(self.html_message)
def create( cls, course_id, sender, targets, subject, html_message, text_message=None, template_name=None, from_addr=None, cohort_name=None): """ Create an instance of CourseEmail. """ # automatically generate the stripped version of the text from the HTML markup: if text_message is None: text_message = html_to_text(html_message) new_targets = [] for target in targets: # split target, to handle cohort:cohort_name target_split = target.split(':', 1) # Ensure our desired target exists if target_split[0] not in EMAIL_TARGETS: fmt = 'Course email being sent to unrecognized target: "{target}" for "{course}", subject "{subject}"' msg = fmt.format(target=target, course=course_id, subject=subject) raise ValueError(msg) elif target_split[0] == SEND_TO_COHORT: # target_split[1] will contain the cohort name cohort = CohortTarget.ensure_valid_cohort(target_split[1], course_id) new_target, _ = CohortTarget.objects.get_or_create(target_type=target_split[0], cohort=cohort) else: new_target, _ = Target.objects.get_or_create(target_type=target_split[0]) new_targets.append(new_target) # create the task, then save it immediately: course_email = cls( course_id=course_id, sender=sender, subject=subject, html_message=html_message, text_message=text_message, template_name=template_name, from_addr=from_addr, ) course_email.save() # Must exist in db before setting M2M relationship values course_email.targets.add(*new_targets) course_email.save() return course_email
def create( cls, sender, subject, html_message, text_message=None, template_name=None, from_addr=None): """ Create an instance of EmailAllUsers. """ # automatically generate the stripped version of the text from the HTML markup: if text_message is None: text_message = html_to_text(html_message) # create the task, then save it immediately: all_users_email = cls( sender=sender, subject=subject, html_message=html_message, text_message=text_message, template_name=template_name, from_addr=from_addr, ) all_users_email.save() return all_users_email