Ejemplo n.º 1
0
def _create_schedule(enrollment, enrollment_created):
    """
    Checks configuration and creates Schedule with ScheduleExperience for this enrollment.
    """
    if not enrollment_created:
        # only create schedules when enrollment records are created
        return

    current_site = get_current_site()
    if current_site is None:
        log.debug('Schedules: No current site')
        return

    schedule_config = ScheduleConfig.current(current_site)
    if (not schedule_config.create_schedules and
            not CREATE_SCHEDULE_WAFFLE_FLAG.is_enabled(enrollment.course_id)):
        log.debug(
            'Schedules: Creation not enabled for this course or for this site')
        return

    # This represents the first date at which the learner can access the content. This will be the latter of
    # either the enrollment date or the course's start date.
    content_availability_date = max(enrollment.created,
                                    enrollment.course_overview.start)
    upgrade_deadline = _calculate_upgrade_deadline(enrollment.course_id,
                                                   content_availability_date)
    experience_type = _get_experience_type(enrollment)

    if _should_randomly_suppress_schedule_creation(
            schedule_config,
            enrollment,
            upgrade_deadline,
            experience_type,
            content_availability_date,
    ):
        return

    schedule = Schedule.objects.create(enrollment=enrollment,
                                       start_date=content_availability_date,
                                       upgrade_deadline=upgrade_deadline)

    ScheduleExperience(schedule=schedule,
                       experience_type=experience_type).save()

    return {
        'content_availability_date': content_availability_date,
        'upgrade_deadline': upgrade_deadline,
        'experience_type': experience_type,
    }