Exemple #1
0
    def handle(self, *args, **options):
        program_uuids, usernames = None, None
        if options['args_from_database']:
            logger.info(
                'Loading arguments from the database for custom programs or learners.'
            )

            arguments = self.get_args_from_database()
            program_uuids = arguments.get('program-uuids', None)
            usernames = arguments.get('usernames', None)

        logger.info('Loading programs from the catalog.')
        self._load_course_runs(program_uuids=program_uuids)

        logger.info(
            'Looking for users who may be eligible for a program certificate.')
        self._load_usernames(users=usernames)

        if options.get('commit'):
            logger.info(
                'Enqueuing program certification tasks for %d candidates.',
                len(self.usernames))
        else:
            logger.info(
                'Found %d candidates. To enqueue program certification tasks, pass the -c or --commit flags.',
                len(self.usernames))
            return

        succeeded, failed = 0, 0
        for username in self.usernames:
            try:
                award_program_certificates.delay(username)
            except:  # pylint: disable=bare-except
                failed += 1
                logger.exception('Failed to enqueue task for user [%s]',
                                 username)
            else:
                succeeded += 1
                logger.debug('Successfully enqueued task for user [%s]',
                             username)

        logger.info(
            'Done. Successfully enqueued tasks for %d candidates. '
            'Failed to enqueue tasks for %d candidates.', succeeded, failed)
def handle_course_cert_awarded(sender, user, course_key, mode, status,
                               **kwargs):  # pylint: disable=unused-argument
    """
    If programs is enabled and a learner is awarded a course certificate,
    schedule a celery task to process any programs certificates for which
    the learner may now be eligible.

    Args:
        sender:
            class of the object instance that sent this signal
        user:
            django.contrib.auth.User - the user to whom a cert was awarded
        course_key:
            refers to the course run for which the cert was awarded
        mode:
            mode / certificate type, e.g. "verified"
        status:
            either "downloadable" or "generating"

    Returns:
        None

    """
    # Import here instead of top of file since this module gets imported before
    # the credentials app is loaded, resulting in a Django deprecation warning.
    from openedx.core.djangoapps.credentials.models import CredentialsApiConfig

    # Avoid scheduling new tasks if certification is disabled.
    if not CredentialsApiConfig.current().is_learner_issuance_enabled:
        return

    # schedule background task to process
    LOGGER.debug(
        'handling COURSE_CERT_AWARDED: username=%s, course_key=%s, mode=%s, status=%s',
        user,
        course_key,
        mode,
        status,
    )
    # import here, because signal is registered at startup, but items in tasks are not yet able to be loaded
    from openedx.core.djangoapps.programs.tasks import award_program_certificates
    award_program_certificates.delay(user.username)