def _get_cert_status_common(user, course_key, enrollment_mode, course_grade,
                            cert):
    """
    Determine the certificate status for this user, in this course run.

    This is used when a downloadable cert cannot be generated, but we want to provide more info about why it cannot
    be generated.
    """
    if CertificateInvalidation.has_certificate_invalidation(
            user, course_key) and cert is not None:
        if cert.status != CertificateStatuses.unavailable:
            cert.invalidate(mode=enrollment_mode,
                            source='certificate_generation')
        return CertificateStatuses.unavailable

    if _required_verification_missing(
            user) and _has_passing_grade_or_is_allowlisted(
                user, course_key, course_grade):
        if cert is None:
            _generate_certificate_task(user=user,
                                       course_key=course_key,
                                       enrollment_mode=enrollment_mode,
                                       course_grade=course_grade,
                                       status=CertificateStatuses.unverified,
                                       generation_mode='batch')
        elif cert.status != CertificateStatuses.unverified:
            cert.mark_unverified(mode=enrollment_mode,
                                 source='certificate_generation')
        return CertificateStatuses.unverified

    return None
def _can_generate_certificate_common(user, course_key, enrollment_mode):
    """
    Check if a course certificate can be generated (created if it doesn't already exist, or updated if it does
    exist) for this user, in this course run.

    This method contains checks that are common to both allowlist and regular course certificates.
    """
    if CertificateInvalidation.has_certificate_invalidation(user, course_key):
        # The invalidation list prevents certificate generation
        log.info(
            f'{user.id} : {course_key} is on the certificate invalidation list. Certificate cannot be generated.'
        )
        return False

    if enrollment_mode is None:
        log.info(
            f'{user.id} : {course_key} does not have an enrollment. Certificate cannot be generated.'
        )
        return False

    is_eligible_for_cert = modes_api.is_eligible_for_certificate(
        enrollment_mode)
    if not is_eligible_for_cert:
        log.info(
            f'{user.id} : {course_key} has an enrollment mode of {enrollment_mode}, which is not eligible for a '
            f'certificate. Certificate cannot be generated.')
        return False

    # If the IDV check fails we then check if the course-run requires ID verification. Honor and Professional-No-ID
    # modes do not require IDV for certificate generation.
    if _required_verification_missing(user):
        if enrollment_mode not in CourseMode.NON_VERIFIED_MODES:
            log.info(
                f'{user.id} does not have a verified id. Certificate cannot be generated for {course_key}.'
            )
            return False

        log.info(
            f'{user.id} : {course_key} is eligible for a certificate without requiring a verified ID. '
            'Skipping results of the ID verification check.')

    if not _can_generate_certificate_for_status(user, course_key,
                                                enrollment_mode):
        return False

    course_overview = get_course_overview_or_none(course_key)
    if not course_overview:
        log.info(
            f'{course_key} does not a course overview. Certificate cannot be generated for {user.id}.'
        )
        return False

    if not has_html_certificates_enabled(course_overview):
        log.info(
            f'{course_key} does not have HTML certificates enabled. Certificate cannot be generated for '
            f'{user.id}.')
        return False

    return True
示例#3
0
    def test_is_certificate_invalid_with_invalid_cert(self):
        """ Verify that method return true if certificate is invalid. """

        invalid_cert = CertificateInvalidationFactory.create(
            generated_certificate=self.certificate, invalidated_by=self.user)
        # Invalidate user certificate
        self.certificate.invalidate()
        assert CertificateInvalidation.has_certificate_invalidation(
            self.user, self.course_id)

        # mark the entry as in-active.
        invalid_cert.active = False
        invalid_cert.save()

        # After making the certificate valid method will return false.
        assert not CertificateInvalidation.has_certificate_invalidation(
            self.user, self.course_id)
def _can_generate_allowlist_certificate(user, course_key):
    """
    Check if an allowlist certificate can be generated (created if it doesn't already exist, or updated if it does
    exist) for this user, in this course run.
    """
    if not is_using_certificate_allowlist(course_key):
        # This course run is not using the allowlist feature
        log.info(
            f'{course_key} is not using the certificate allowlist. Allowlist certificate cannot be generated'
            f'for {user.id}.')
        return False

    if not _is_on_certificate_allowlist(user, course_key):
        log.info(
            f'{user.id} : {course_key} is not on the certificate allowlist. Allowlist certificate cannot be '
            f'generated.')
        return False

    if not auto_certificate_generation_enabled():
        # Automatic certificate generation is globally disabled
        log.info(
            f'Automatic certificate generation is globally disabled. Allowlist certificate cannot be generated'
            f'for {user.id} : {course_key}.')
        return False

    if CertificateInvalidation.has_certificate_invalidation(user, course_key):
        # The invalidation list overrides the allowlist
        log.info(
            f'{user.id} : {course_key} is on the certificate invalidation list. Allowlist certificate cannot be '
            f'generated.')
        return False

    enrollment_mode, __ = CourseEnrollment.enrollment_mode_for_user(
        user, course_key)
    if enrollment_mode is None:
        log.info(
            f'{user.id} : {course_key} does not have an enrollment. Allowlist certificate cannot be generated.'
        )
        return False

    if not modes_api.is_eligible_for_certificate(enrollment_mode):
        log.info(
            f'{user.id} : {course_key} has an enrollment mode of {enrollment_mode}, which is not eligible for an '
            f'allowlist certificate. Certificate cannot be generated.')
        return False

    if not IDVerificationService.user_is_verified(user):
        log.info(
            f'{user.id} does not have a verified id. Allowlist certificate cannot be generated for {course_key}.'
        )
        return False

    log.info(f'{user.id} : {course_key} is on the certificate allowlist')
    return _can_generate_allowlist_certificate_for_status(user, course_key)
示例#5
0
    def test_is_certificate_invalid_with_invalid_cert(self):
        """ Verify that method return true if certificate is invalid. """

        invalid_cert = CertificateInvalidationFactory.create(
            generated_certificate=self.certificate,
            invalidated_by=self.user
        )
        # Invalidate user certificate
        self.certificate.invalidate()
        self.assertTrue(
            CertificateInvalidation.has_certificate_invalidation(self.user, self.course_id)
        )

        # mark the entry as in-active.
        invalid_cert.active = False
        invalid_cert.save()

        # After making the certificate valid method will return false.
        self.assertFalse(
            CertificateInvalidation.has_certificate_invalidation(self.user, self.course_id)
        )
示例#6
0
def can_generate_allowlist_certificate(user, course_key):
    """
    Check if an allowlist certificate can be generated (created if it doesn't already exist, or updated if it does
    exist) for this user, in this course run.
    """
    if not is_using_certificate_allowlist(course_key):
        # This course run is not using the allowlist feature
        log.info(
            '{course} is not using the certificate allowlist. Certificate cannot be generated.'.format(
                course=course_key
            ))
        return False

    if not auto_certificate_generation_enabled():
        # Automatic certificate generation is globally disabled
        log.info('Automatic certificate generation is globally disabled. Certificate cannot be generated.')
        return False

    if CertificateInvalidation.has_certificate_invalidation(user, course_key):
        # The invalidation list overrides the allowlist
        log.info(
            '{user} : {course} is on the certificate invalidation list. Certificate cannot be generated.'.format(
                user=user.id,
                course=course_key
            ))
        return False

    enrollment_mode, __ = CourseEnrollment.enrollment_mode_for_user(user, course_key)
    if enrollment_mode is None:
        log.info('{user} : {course} does not have an enrollment. Certificate cannot be generated.'.format(
            user=user.id,
            course=course_key
        ))
        return False

    if not IDVerificationService.user_has_ever_been_verified(user):
        log.info(f'{user.id} has not ever had a verified id. Certificate cannot be generated.')
        return False

    if not _is_on_certificate_allowlist(user, course_key):
        log.info('{user} : {course} is not on the certificate allowlist. Certificate cannot be generated.'.format(
            user=user.id,
            course=course_key
        ))
        return False

    log.info('{user} : {course} is on the certificate allowlist'.format(
        user=user.id,
        course=course_key
    ))
    cert = GeneratedCertificate.certificate_for_student(user, course_key)
    return _can_generate_allowlist_certificate_for_status(cert)
def _is_cert_downloadable(user, course_key):
    """
    Check if cert already exists, has a downloadable status, and has not been invalidated
    """
    cert = GeneratedCertificate.certificate_for_student(user, course_key)
    if cert is None:
        return False
    if cert.status != CertificateStatuses.downloadable:
        return False
    if CertificateInvalidation.has_certificate_invalidation(user, course_key):
        return False

    return True
示例#8
0
def _can_generate_certificate_common(user, course_key):
    """
    Check if a course certificate can be generated (created if it doesn't already exist, or updated if it does
    exist) for this user, in this course run.

    This method contains checks that are common to both allowlist and V2 regular course certificates.
    """
    if CertificateInvalidation.has_certificate_invalidation(user, course_key):
        # The invalidation list prevents certificate generation
        log.info(
            f'{user.id} : {course_key} is on the certificate invalidation list. Certificate cannot be generated.'
        )
        return False

    enrollment_mode, __ = CourseEnrollment.enrollment_mode_for_user(
        user, course_key)
    if enrollment_mode is None:
        log.info(
            f'{user.id} : {course_key} does not have an enrollment. Certificate cannot be generated.'
        )
        return False

    if not modes_api.is_eligible_for_certificate(enrollment_mode):
        log.info(
            f'{user.id} : {course_key} has an enrollment mode of {enrollment_mode}, which is not eligible for a '
            f'certificate. Certificate cannot be generated.')
        return False

    if not IDVerificationService.user_is_verified(user):
        log.info(
            f'{user.id} does not have a verified id. Certificate cannot be generated for {course_key}.'
        )
        return False

    if not _can_generate_certificate_for_status(user, course_key):
        return False

    course_overview = get_course_overview_or_none(course_key)
    if not course_overview:
        log.info(
            f'{course_key} does not a course overview. Certificate cannot be generated for {user.id}.'
        )
        return False

    if not has_html_certificates_enabled(course_overview):
        log.info(
            f'{course_key} does not have HTML certificates enabled. Certificate cannot be generated for '
            f'{user.id}.')
        return False

    return True
示例#9
0
def is_certificate_invalidated(student, course_key):
    """Check whether the certificate belonging to the given student (in given course) has been invalidated.

    Arguments:
        student (user object): logged-in user
        course_key (CourseKey): The course identifier.

    Returns:
        Boolean denoting whether the certificate has been invalidated for this learner.
    """
    log.info(f"Checking if student {student.id} has an invalidated certificate in course {course_key}.")

    certificate = GeneratedCertificate.certificate_for_student(student, course_key)
    if certificate:
        return CertificateInvalidation.has_certificate_invalidation(student, course_key)

    return False
示例#10
0
def is_certificate_invalid(student, course_key):
    """Check that whether the student in the course has been invalidated
    for receiving certificates.

    Arguments:
        student (user object): logged-in user
        course_key (CourseKey): The course identifier.

    Returns:
        Boolean denoting whether the student in the course is invalidated
        to receive certificates
    """
    is_invalid = False
    certificate = GeneratedCertificate.certificate_for_student(student, course_key)
    if certificate is not None:
        is_invalid = CertificateInvalidation.has_certificate_invalidation(student, course_key)

    return is_invalid
示例#11
0
def is_certificate_invalid(student, course_key):
    """Check that whether the student in the course has been invalidated
    for receiving certificates.

    Arguments:
        student (user object): logged-in user
        course_key (CourseKey): The course identifier.

    Returns:
        Boolean denoting whether the student in the course is invalidated
        to receive certificates
    """
    is_invalid = False
    certificate = GeneratedCertificate.certificate_for_student(student, course_key)
    if certificate is not None:
        is_invalid = CertificateInvalidation.has_certificate_invalidation(student, course_key)

    return is_invalid
示例#12
0
def _get_cert_status_common(user, course_key, cert):
    """
    Determine the certificate status for this user, in this course run.

    This is used when a downloadable cert cannot be generated, but we want to provide more info about why it cannot
    be generated.
    """
    if CertificateInvalidation.has_certificate_invalidation(user, course_key):
        if cert is None:
            cert = GeneratedCertificate.objects.create(user=user, course_id=course_key)
        if cert.status != CertificateStatuses.unavailable:
            cert.invalidate()
        return CertificateStatuses.unavailable

    if not IDVerificationService.user_is_verified(user):
        if cert is None:
            cert = GeneratedCertificate.objects.create(user=user, course_id=course_key)
        if cert.status != CertificateStatuses.unverified:
            cert.mark_unverified()
        return CertificateStatuses.unverified

    return None
示例#13
0
    def test_is_certificate_invalid_method(self):
        """ Verify that method return false if certificate is valid. """

        assert not CertificateInvalidation.has_certificate_invalidation(
            self.user, self.course_id)
def _can_generate_v2_certificate(user, course_key):
    """
    Check if a v2 course certificate can be generated (created if it doesn't already exist, or updated if it does
    exist) for this user, in this course run.
    """
    if not _is_using_v2_course_certificates(course_key):
        # This course run is not using the v2 course certificate feature
        log.info(
            f'{course_key} is not using v2 course certificates. Certificate cannot be generated.'
        )
        return False

    if not auto_certificate_generation_enabled():
        # Automatic certificate generation is globally disabled
        log.info(
            f'Automatic certificate generation is globally disabled. Certificate cannot be generated for '
            f'{user.id} : {course_key}.')
        return False

    if CertificateInvalidation.has_certificate_invalidation(user, course_key):
        # The invalidation list prevents certificate generation
        log.info(
            f'{user.id} : {course_key} is on the certificate invalidation list. Certificate cannot be generated.'
        )
        return False

    enrollment_mode, __ = CourseEnrollment.enrollment_mode_for_user(
        user, course_key)
    if enrollment_mode is None:
        log.info(
            f'{user.id} : {course_key} does not have an enrollment. Certificate cannot be generated.'
        )
        return False

    if not modes_api.is_eligible_for_certificate(enrollment_mode):
        log.info(
            f'{user.id} : {course_key} has an enrollment mode of {enrollment_mode}, which is not eligible for an '
            f'allowlist certificate. Certificate cannot be generated.')
        return False

    if not IDVerificationService.user_is_verified(user):
        log.info(
            f'{user.id} does not have a verified id. Certificate cannot be generated for {course_key}.'
        )
        return False

    if _is_ccx_course(course_key):
        log.info(
            f'{course_key} is a CCX course. Certificate cannot be generated for {user.id}.'
        )
        return False

    course = modulestore().get_course(course_key, depth=0)
    if _is_beta_tester(user, course):
        log.info(
            f'{user.id} is a beta tester in {course_key}. Certificate cannot be generated.'
        )
        return False

    if not _has_passing_grade(user, course):
        log.info(
            f'{user.id} does not have a passing grade in {course_key}. Certificate cannot be generated.'
        )
        return False

    if not _can_generate_certificate_for_status(user, course_key):
        return False

    log.info(f'V2 certificate can be generated for {user.id} : {course_key}')
    return True
示例#15
0
    def test_is_certificate_invalid_method(self):
        """ Verify that method return false if certificate is valid. """

        self.assertFalse(
            CertificateInvalidation.has_certificate_invalidation(self.user, self.course_id)
        )