def refresh_background(course_id):
    '\n    Look up existing extensions and apply them to new quizzes.\n\n    :param course_id: The Canvas ID of the Course.\n    :type course_id: int\n    :rtype: dict\n    :returns: A dictionary containing two parts:\n\n        - success `bool` False if there was an error, True otherwise.\n        - message `str` A long description of success or failure.\n    '
    job = get_current_job()
    update_job(job, 0, 'Starting...', 'started')
    with app.app_context():
        (course, created) = get_or_create(db.session, Course, canvas_id=course_id)
        try:
            course_name = get_course(course_id).get('name', '<UNNAMED COURSE>')
            course.course_name = course_name
            db.session.commit()
        except requests.exceptions.HTTPError:
            update_job(job, 0, 'Course not found.', 'failed', error=True)
            logger.exception('Unable to find course #{}'.format(course_id))
            return job.meta
        quizzes = missing_and_stale_quizzes(course_id)
        num_quizzes = len(quizzes)
        if (num_quizzes < 1):
            update_job(job, 100, 'Complete. No quizzes required updates.', 'complete', error=False)
            return job.meta
        percent_user_map = defaultdict(list)
        inactive_list = []
        update_job(job, 0, 'Getting past extensions.', 'processing', False)
        for extension in course.extensions:
            if (not extension.active):
                inactive_list.append(extension.user.sortable_name)
                logger.debug('Extension #{} is inactive.'.format(extension.id))
                continue
            user_canvas_id = User.query.filter_by(id=extension.user_id).first().canvas_id
            try:
                canvas_user = get_user(course_id, user_canvas_id)
                enrolls = canvas_user.get('enrollments', [])
                type_list = [e['type'] for e in enrolls if (e['enrollment_state'] == 'active')]
                if (not any(((t == 'StudentEnrollment') for t in type_list))):
                    logger.info('User #{} was found in course #{}, but is not an active student. Deactivating extension #{}. Roles found: {}'.format(user_canvas_id, course_id, extension.id, (', '.join(type_list) if (len(enrolls) > 0) else None)))
                    extension.active = False
                    db.session.commit()
                    inactive_list.append(extension.user.sortable_name)
                    continue
            except requests.exceptions.HTTPError:
                log_str = 'User #{} not in course #{}. Deactivating extension #{}.'
                logger.info(log_str.format(user_canvas_id, course_id, extension.id))
                extension.active = False
                db.session.commit()
                inactive_list.append(extension.user.sortable_name)
                continue
            percent_user_map[extension.percent].append(user_canvas_id)
        if (len(percent_user_map) < 1):
            msg_str = 'No active extensions were found.<br>'
            if (len(inactive_list) > 0):
                msg_str += ' Extensions for the following students are inactive:<br>{}'
                msg_str = msg_str.format('<br>'.join(inactive_list))
            update_job(job, 100, msg_str, 'complete', error=False)
            return job.meta
        for (index, quiz) in enumerate(quizzes):
            quiz_id = quiz.get('id', None)
            quiz_title = quiz.get('title', '[UNTITLED QUIZ]')
            comp_perc = int(((float(index) / float(num_quizzes)) * 100))
            refreshing_str = 'Refreshing quiz #{} - {} [{} of {}]'
            update_job(job, comp_perc, refreshing_str.format(quiz_id, quiz_title, (index + 1), num_quizzes), 'processing', error=False)
            for (percent, user_list) in percent_user_map.items():
                extension_response = extend_quiz(course_id, quiz, percent, user_list)
                if (extension_response.get('success', False) is True):
                    (quiz_obj, created) = get_or_create(db.session, Quiz, canvas_id=quiz_id, course_id=course.id)
                    quiz_obj.title = quiz_title
                    quiz_obj.time_limit = quiz.get('time_limit')
                    db.session.commit()
                else:
                    error_message = "Some quizzes couldn't be updated. "
                    error_message += extension_response.get('message', '')
                    update_job(job, comp_perc, error_message, 'failed', error=True)
                    return job.meta
        msg = '{} quizzes have been updated.'.format(len(quizzes))
        update_job(job, 100, msg, 'complete', error=False)
        return job.meta
Look up existing extensions and apply them to new quizzes.

:param course_id: The Canvas ID of the Course.
:type course_id: int
:rtype: dict
:returns: A dictionary containing two parts:

    - success `bool` False if there was an error, True otherwise.
    - message `str` A long description of success or failure.
def refresh_background(course_id):
    '\n    Look up existing extensions and apply them to new quizzes.\n\n    :param course_id: The Canvas ID of the Course.\n    :type course_id: int\n    :rtype: dict\n    :returns: A dictionary containing two parts:\n\n        - success `bool` False if there was an error, True otherwise.\n        - message `str` A long description of success or failure.\n    '
    job = get_current_job()
    update_job(job, 0, 'Starting...', 'started')
    with app.app_context():
        (course, created) = get_or_create(db.session, Course, canvas_id=course_id)
        try:
            course_name = get_course(course_id).get('name', '<UNNAMED COURSE>')
            course.course_name = course_name
            db.session.commit()
        except requests.exceptions.HTTPError:
            update_job(job, 0, 'Course not found.', 'failed', error=True)
            logger.exception('Unable to find course #{}'.format(course_id))
            return job.meta
        quizzes = missing_and_stale_quizzes(course_id)
        num_quizzes = len(quizzes)
        if (num_quizzes < 1):
            update_job(job, 100, 'Complete. No quizzes required updates.', 'complete', error=False)
            return job.meta
        percent_user_map = defaultdict(list)
        inactive_list = []
        update_job(job, 0, 'Getting past extensions.', 'processing', False)
        for extension in course.extensions:
            if (not extension.active):
                inactive_list.append(extension.user.sortable_name)
                logger.debug('Extension #{} is inactive.'.format(extension.id))
                continue
            user_canvas_id = User.query.filter_by(id=extension.user_id).first().canvas_id
            try:
                canvas_user = get_user(course_id, user_canvas_id)
                enrolls = canvas_user.get('enrollments', [])
                type_list = [e['type'] for e in enrolls if (e['enrollment_state'] == 'active')]
                if (not any(((t == 'StudentEnrollment') for t in type_list))):
                    logger.info('User #{} was found in course #{}, but is not an active student. Deactivating extension #{}. Roles found: {}'.format(user_canvas_id, course_id, extension.id, (', '.join(type_list) if (len(enrolls) > 0) else None)))
                    extension.active = False
                    db.session.commit()
                    inactive_list.append(extension.user.sortable_name)
                    continue
            except requests.exceptions.HTTPError:
                log_str = 'User #{} not in course #{}. Deactivating extension #{}.'
                logger.info(log_str.format(user_canvas_id, course_id, extension.id))
                extension.active = False
                db.session.commit()
                inactive_list.append(extension.user.sortable_name)
                continue
            percent_user_map[extension.percent].append(user_canvas_id)
        if (len(percent_user_map) < 1):
            msg_str = 'No active extensions were found.<br>'
            if (len(inactive_list) > 0):
                msg_str += ' Extensions for the following students are inactive:<br>{}'
                msg_str = msg_str.format('<br>'.join(inactive_list))
            update_job(job, 100, msg_str, 'complete', error=False)
            return job.meta
        for (index, quiz) in enumerate(quizzes):
            quiz_id = quiz.get('id', None)
            quiz_title = quiz.get('title', '[UNTITLED QUIZ]')
            comp_perc = int(((float(index) / float(num_quizzes)) * 100))
            refreshing_str = 'Refreshing quiz #{} - {} [{} of {}]'
            update_job(job, comp_perc, refreshing_str.format(quiz_id, quiz_title, (index + 1), num_quizzes), 'processing', error=False)
            for (percent, user_list) in percent_user_map.items():
                extension_response = extend_quiz(course_id, quiz, percent, user_list)
                if (extension_response.get('success', False) is True):
                    (quiz_obj, created) = get_or_create(db.session, Quiz, canvas_id=quiz_id, course_id=course.id)
                    quiz_obj.title = quiz_title
                    quiz_obj.time_limit = quiz.get('time_limit')
                    db.session.commit()
                else:
                    error_message = "Some quizzes couldn't be updated. "
                    error_message += extension_response.get('message', )
                    update_job(job, comp_perc, error_message, 'failed', error=True)
                    return job.meta
        msg = '{} quizzes have been updated.'.format(len(quizzes))
        update_job(job, 100, msg, 'complete', error=False)
        return job.meta