def invalidate_certificate(self, user_id, course_key_or_id): """ Invalidate the user certificate in a given course if it exists and the user is not on the allowlist for this course run. """ course_key = _get_key(course_key_or_id, CourseKey) if is_on_certificate_allowlist(user_id, course_key): log.info(f'User {user_id} is on the allowlist for {course_key}. The certificate will not be invalidated.') return False try: generated_certificate = GeneratedCertificate.objects.get( user=user_id, course_id=course_key ) generated_certificate.invalidate() except ObjectDoesNotExist: log.warning( 'Invalidation failed because a certificate for user %d in course %s does not exist.', user_id, course_key ) return False return True
def _listen_for_certificate_allowlist_append(sender, instance, **kwargs): # pylint: disable=unused-argument """ Listen for a user being added to or modified on the allowlist """ if not auto_certificate_generation_enabled(): return if is_on_certificate_allowlist(instance.user, instance.course_id): log.info( f'User {instance.user.id} is now on the allowlist for course {instance.course_id}. Attempt will be ' f'made to generate an allowlist certificate.') return generate_allowlist_certificate_task(instance.user, instance.course_id)
def test_is_on_allowlist_false(self): """ Test the absence of the user on the allowlist """ u = UserFactory() CourseEnrollmentFactory( user=u, course_id=self.course_run_key, is_active=True, mode=CourseMode.VERIFIED, ) CertificateAllowlistFactory.create(course_id=self.course_run_key, user=u, allowlist=False) assert not is_on_certificate_allowlist(u, self.course_run_key)
def _listen_for_certificate_whitelist_append(sender, instance, **kwargs): # pylint: disable=unused-argument """ Listen for a user being added to or modified on the whitelist (allowlist) """ if not auto_certificate_generation_enabled(): return if is_on_certificate_allowlist(instance.user, instance.course_id): log.info( f'User {instance.user.id} is now on the allowlist for course {instance.course_id}. Attempt will be ' f'made to generate an allowlist certificate.') return generate_allowlist_certificate_task(instance.user, instance.course_id) if _fire_ungenerated_certificate_task(instance.user, instance.course_id): log.info( 'Certificate generation task initiated for {user} : {course} via whitelist' .format(user=instance.user.id, course=instance.course_id))
def _listen_for_failing_grade(sender, user, course_id, grade, **kwargs): # pylint: disable=unused-argument """ Listen for a signal indicating that the user has failed a course run. If needed, mark the certificate as notpassing. """ if is_on_certificate_allowlist(user, course_id): log.info( f'User {user.id} is on the allowlist for {course_id}. The failing grade will not affect the ' f'certificate.') return cert = GeneratedCertificate.certificate_for_student(user, course_id) if cert is not None: if CertificateStatuses.is_passing_status(cert.status): cert.mark_notpassing(grade.percent) log.info( 'Certificate marked not passing for {user} : {course} via failing grade: {grade}' .format(user=user.id, course=course_id, grade=grade))
def _listen_for_failing_grade(sender, user, course_id, grade, **kwargs): # pylint: disable=unused-argument """ Listen for a signal indicating that the user has failed a course run. If needed, mark the certificate as notpassing. """ if is_on_certificate_allowlist(user, course_id): log.info( f'User {user.id} is on the allowlist for {course_id}. The failing grade will not affect the ' f'certificate.') return cert = GeneratedCertificate.certificate_for_student(user, course_id) if cert is not None: if CertificateStatuses.is_passing_status(cert.status): enrollment_mode, __ = CourseEnrollment.enrollment_mode_for_user( user, course_id) cert.mark_notpassing(mode=enrollment_mode, grade=grade.percent, source='notpassing_signal') log.info( f'Certificate marked not passing for {user.id} : {course_id} via failing grade' )
def test_is_on_allowlist(self): """ Test the presence of the user on the allowlist """ assert is_on_certificate_allowlist(self.user, self.course_run_key)