def handle(self, *args, **options): logger.info('Loading programs from the catalog.') self._load_course_runs() logger.info( 'Looking for users who may be eligible for a program certificate.') self._load_usernames() if options.get('commit'): logger.info( u'Enqueuing program certification tasks for %d candidates.', len(self.usernames)) else: logger.info( u'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(u'Failed to enqueue task for user [%s]', username) else: succeeded += 1 logger.debug(u'Successfully enqueued task for user [%s]', username) logger.info( u'Done. Successfully enqueued tasks for %d candidates. ' u'Failed to enqueue tasks for %d candidates.', succeeded, failed)
def handle(self, *args, **options): logger.info('Loading programs from the catalog.') self._load_course_runs() logger.info('Looking for users who may be eligible for a program certificate.') self._load_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 post(self, request): if not ProgramsApiConfig.current().is_certification_enabled: return Response({"error": "Program certification is disabled."}, status=status.HTTP_400_BAD_REQUEST) username = request.data.get("username") if username: log.info("Enqueuing program certification task for user [%s]", username) award_program_certificates.delay(username) return Response() else: return Response( {"error": "A username is required in order to issue program certificates."}, status=status.HTTP_400_BAD_REQUEST, )
def handle(self, *args, **options): programs_config = ProgramsApiConfig.current() self.client = Client.objects.get(name=programs_config.OAUTH2_CLIENT_NAME) if self.client.user is None: msg = ( 'No user is associated with the {} OAuth2 client. ' 'A service user is necessary to make requests to the Programs API. ' 'No tasks have been enqueued. ' 'Associate a user with the client and try again.' ).format(programs_config.OAUTH2_CLIENT_NAME) raise CommandError(msg) self._load_run_modes() logger.info('Looking for users who may be eligible for a program certificate.') self._load_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(self, *args, **options): programs_config = ProgramsApiConfig.current() self.client = Client.objects.get(name=programs_config.OAUTH2_CLIENT_NAME) if self.client.user is None: msg = ( "No user is associated with the {} OAuth2 client. " "A service user is necessary to make requests to the Programs API. " "No tasks have been enqueued. " "Associate a user with the client and try again." ).format(programs_config.OAUTH2_CLIENT_NAME) raise CommandError(msg) self._load_run_modes() logger.info("Looking for users who may be eligible for a program certificate.") self._load_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(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( u'Enqueuing program certification tasks for %d candidates.', len(self.usernames)) else: logger.info( u'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(u'Failed to enqueue task for user [%s]', username) else: succeeded += 1 logger.debug(u'Successfully enqueued task for user [%s]', username) logger.info( u'Done. Successfully enqueued tasks for %d candidates. ' u'Failed to enqueue tasks for %d candidates.', succeeded, failed)
def handle(self, *args, **options): catalog_config = CatalogIntegration.current() try: user = User.objects.get(username=catalog_config.service_username) except: raise CommandError( 'User with username [{}] not found. ' 'A service user is required to run this command.'.format( catalog_config.service_username)) self._load_run_modes(user) logger.info( 'Looking for users who may be eligible for a program certificate.') self._load_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 post(self, request): if not ProgramsApiConfig.current().is_certification_enabled: return Response({'error': 'Program certification is disabled.'}, status=status.HTTP_400_BAD_REQUEST) username = request.data.get('username') if username: log.info('Enqueuing program certification task for user [%s]', username) award_program_certificates.delay(username) return Response() else: return Response( { 'error': 'A username is required in order to issue program certificates.' }, status=status.HTTP_400_BAD_REQUEST)
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.v1.tasks import award_program_certificates award_program_certificates.delay(user.username)
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.v1.tasks import award_program_certificates award_program_certificates.delay(user.username)