Exemplo n.º 1
0
def current_time_is_in_interval(start, end):
    """
    Determine whether the current time is on the interval [start, end].
    """
    interval_start = parse_lms_api_datetime(start or UNIX_MIN_DATE_STRING)
    interval_end = parse_lms_api_datetime(end or UNIX_MAX_DATE_STRING)
    return interval_start <= timezone.now() <= interval_end
Exemplo n.º 2
0
def parse_datetime_to_epoch(datestamp):
    """
    Convert an ISO-8601 datetime string to a Unix epoch timestamp in milliseconds.
    """
    parsed_datetime = parse_lms_api_datetime(datestamp)
    time_since_epoch = parsed_datetime - UNIX_EPOCH
    return int(time_since_epoch.total_seconds() * 1000)
Exemplo n.º 3
0
    def notify_enrolled_learners(cls, enterprise_customer, request, course_id, users):
        """
        Notify learners about a course in which they've been enrolled.

        Args:
            enterprise_customer: The EnterpriseCustomer being linked to
            request: The HTTP request that's being processed
            course_id: The specific course the learners were enrolled in
            users: An iterable of the users or pending users who were enrolled
        """
        course_details = CourseCatalogApiClient(request.user).get_course_run(course_id)
        if not course_details:
            logging.warning(
                _(
                    "Course details were not found for course key {} - Course Catalog API returned nothing. "
                    "Proceeding with enrollment, but notifications won't be sent"
                ).format(course_id)
            )
            return
        course_url = course_details.get('marketing_url')
        if course_url is None:
            # If we didn't get a useful path to the course on a marketing site from the catalog API,
            # then we should build a path to the course on the LMS directly.
            course_url = get_reversed_url_by_site(
                request,
                enterprise_customer.site,
                'about_course',
                args=(course_id,),
            )
        course_name = course_details.get('title')

        try:
            course_start = parse_lms_api_datetime(course_details.get('start'))
        except (TypeError, ValueError):
            course_start = None
            logging.exception(
                "None or empty value passed as course start date.\nCourse Details:\n{course_details}".format(
                    course_details=course_details,
                )
            )

        with mail.get_connection() as email_conn:
            for user in users:
                send_email_notification_message(
                    user=user,
                    enrolled_in={
                        'name': course_name,
                        'url': course_url,
                        'type': 'course',
                        'start': course_start,
                    },
                    enterprise_customer=enterprise_customer,
                    email_connection=email_conn
                )
Exemplo n.º 4
0
def get_earliest_start_date_from_program(program):
    """
    Get the earliest date that one of the courses in the program was available.
    For the sake of emails to new learners, we treat this as the program start date.

    Arguemnts:
        program (dict): Program data from Course Catalog API

    returns:
        datetime.datetime: The date and time at which the first course started
    """
    start_dates = []
    for course in program.get('courses', []):
        for run in course.get('course_runs', []):
            if run.get('start'):
                start_dates.append(parse_lms_api_datetime(run['start']))
    if not start_dates:
        return None
    return min(start_dates)
Exemplo n.º 5
0
 def test_current_time_in_interval(self, start, end, expected,
                                   fake_timezone):
     fake_timezone.now.return_value = parse_lms_api_datetime(
         '2016-01-01T00:00:00Z')
     assert current_time_is_in_interval(start, end) is expected