def get_schedules(self): """ Grabs possible schedules that could receive a Course Next Section Update and if a next section highlight is applicable for the user, yields information needed to send the next section highlight email. """ target_date = self.target_datetime.date() course_duration = get_expected_duration(self.course_id) schedules = Schedule.objects.select_related('enrollment').filter( self.experience_filter, active=True, enrollment__course_id=self.course_id, enrollment__user__is_active=True, start_date__gte=target_date - course_duration, start_date__lt=target_date, ) template_context = get_base_template_context(self.site) for schedule in schedules: course = schedule.enrollment.course user = schedule.enrollment.user start_date = max(filter(None, (schedule.start_date, course.start))) LOG.info( 'Received a schedule for user {} in course {} for date {}'. format( user.username, self.course_id, target_date, )) try: week_highlights, week_num = get_next_section_highlights( user, course.id, start_date, target_date) # (None, None) is returned when there is no section with a due date of the target_date if week_highlights is None: continue except CourseUpdateDoesNotExist as e: log_message = self.log_prefix + ': ' + str(e) LOG.warning(log_message) # continue to the next schedule, don't yield an email for this one continue unsubscribe_url = None if (COURSE_UPDATE_SHOW_UNSUBSCRIBE_WAFFLE_SWITCH.is_enabled() and 'bulk_email_optout' in settings.ACE_ENABLED_POLICIES): unsubscribe_url = reverse('bulk_email_opt_out', kwargs={ 'token': UsernameCipher.encrypt( user.username), 'course_id': str(course.id), }) template_context.update({ 'course_name': course.display_name, 'course_url': _get_trackable_course_home_url(course.id), 'week_num': week_num, 'week_highlights': week_highlights, # This is used by the bulk email optout policy 'course_ids': [str(course.id)], 'unsubscribe_url': unsubscribe_url, }) template_context.update( _get_upsell_information_for_schedule(user, schedule)) yield (user, course.closest_released_language, template_context, course.self_paced)
def get_schedules(self): course_key = CourseKey.from_string(self.course_id) target_date = self.target_datetime.date() schedules = get_schedules_with_due_date( course_key, target_date).filter( self.experience_filter, active=True, enrollment__user__is_active=True, ) template_context = get_base_template_context(self.site) for schedule in schedules: enrollment = schedule.enrollment course = schedule.enrollment.course user = enrollment.user start_date = schedule.start_date LOG.info( u'Received a schedule for user {} in course {} for date {}'. format( user.username, self.course_id, target_date, )) try: week_highlights, week_num = get_next_section_highlights( user, course.id, start_date, target_date) except CourseUpdateDoesNotExist: LOG.warning( u'Weekly highlights for user {} of course {} does not exist or is disabled' .format(user, course.id)) # continue to the next schedule, don't yield an email for this one continue unsubscribe_url = None if (COURSE_UPDATE_SHOW_UNSUBSCRIBE_WAFFLE_SWITCH.is_enabled() and 'bulk_email_optout' in settings.ACE_ENABLED_POLICIES): unsubscribe_url = reverse('bulk_email_opt_out', kwargs={ 'token': UsernameCipher.encrypt( user.username), 'course_id': str(enrollment.course_id), }) template_context.update({ 'course_name': course.display_name, 'course_url': _get_trackable_course_home_url(enrollment.course_id), 'week_num': week_num, 'week_highlights': week_highlights, # This is used by the bulk email optout policy 'course_ids': [str(enrollment.course_id)], 'unsubscribe_url': unsubscribe_url, }) template_context.update( _get_upsell_information_for_schedule(user, schedule)) yield (user, enrollment.course.closest_released_language, template_context, course.self_paced)