refactor: Use certificates api method instead of the model, and rename whitelist to allowlist. Also move method to certificates app to prevent circular import. (#27532)

MICROBA-1021
This commit is contained in:
Christie Rice
2021-05-10 13:02:43 -04:00
committed by GitHub
parent 4b247013ff
commit 4867535d2d
6 changed files with 50 additions and 39 deletions

View File

@@ -7,7 +7,8 @@ import logging
from datetime import datetime
from pytz import UTC
from lms.djangoapps.certificates.models import CertificateStatuses, CertificateWhitelist
from lms.djangoapps.certificates import api as certs_api
from lms.djangoapps.certificates.models import CertificateStatuses
from openedx.core.djangoapps.certificates.config import waffle
from common.djangoapps.student.models import CourseEnrollment
@@ -26,30 +27,6 @@ def _enabled_and_instructor_paced(course):
return False
def certificates_viewable_for_course(course):
"""
Returns True if certificates are viewable for any student enrolled in the course, False otherwise.
"""
if course.self_paced:
return True
if (
course.certificates_display_behavior in ('early_with_info', 'early_no_info')
or course.certificates_show_before_end
):
return True
if (
course.certificate_available_date
and course.certificate_available_date <= datetime.now(UTC)
):
return True
if (
course.certificate_available_date is None
and course.has_ended()
):
return True
return False
def is_certificate_valid(certificate):
"""
Returns True if the student has a valid, verified certificate for this course, False otherwise.
@@ -57,17 +34,20 @@ def is_certificate_valid(certificate):
return CourseEnrollment.is_enrolled_as_verified(certificate.user, certificate.course_id) and certificate.is_valid()
def can_show_certificate_message(course, student, course_grade, certificates_enabled_for_course): # lint-amnesty, pylint: disable=missing-function-docstring
is_whitelisted = CertificateWhitelist.objects.filter(user=student, course_id=course.id, whitelist=True).exists()
def can_show_certificate_message(course, student, course_grade, certificates_enabled_for_course):
"""
Returns True if a course certificate message can be shown
"""
is_allowlisted = certs_api.is_on_allowlist(student, course.id)
auto_cert_gen_enabled = auto_certificate_generation_enabled()
has_active_enrollment = CourseEnrollment.is_enrolled(student, course.id)
certificates_are_viewable = certificates_viewable_for_course(course)
certificates_are_viewable = certs_api.certificates_viewable_for_course(course)
return (
(auto_cert_gen_enabled or certificates_enabled_for_course) and
has_active_enrollment and
certificates_are_viewable and
(course_grade.passed or is_whitelisted)
(course_grade.passed or is_allowlisted)
)