Beispiel #1
0
def get_user_course_duration(user, course):
    """
    Return a timedelta measuring the duration of the course for a particular user.

    Business Logic:
      - Course access duration is bounded by the min and max duration.
      - If course fields are missing, default course access duration to MIN_DURATION.
    """
    if not CourseDurationLimitConfig.enabled_for_enrollment(user, course):
        return None

    enrollment = CourseEnrollment.get_enrollment(user, course.id)
    if enrollment is None or enrollment.mode != CourseMode.AUDIT:
        return None

    verified_mode = CourseMode.verified_mode_for_course(course=course,
                                                        include_expired=True)
    if not verified_mode:
        return None

    return get_expected_duration(course.id)
Beispiel #2
0
    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,
            enrollment__is_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
            # We don't want to show any updates if the course has ended so we short circuit here.
            if course.end and course.end.date() <= target_date:
                return

            # Next Section Updates are only for Self-paced courses since it uses Personalized
            # Learner Schedule logic. See CourseUpdateResolver for Instructor-paced updates
            if not course.self_paced:
                continue

            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)