[MICROBA-1075] - Adds a new `edx.certificate.revoked` event to the LMS. - Refactor of our certificate revocation functions in the GeneratedCertificate model. This new event will be emit when a GeneratedCertificate with the status of `downloadable` is revoked (through the `invalidate(), mark_notpassing(), or mark_unverified() functions). Event will have a `source` field that will allow us how our certificates are being revoked from learners and can be broken down in the following way: *Invalidate* - allowlist_removal - certificate_generation - certificate_regeneration - certificate_service - certificate_invalidation_list - bulk_certificate_regeneration *mark_notpassing* - certificate_generation - notpassing_signal *unverified* - certificate_generation
48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
"""
|
|
Certificate service
|
|
"""
|
|
|
|
|
|
import logging
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
from opaque_keys.edx.keys import CourseKey
|
|
|
|
from lms.djangoapps.certificates.generation_handler import is_on_certificate_allowlist
|
|
from lms.djangoapps.certificates.models import GeneratedCertificate
|
|
from lms.djangoapps.utils import _get_key
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class CertificateService:
|
|
"""
|
|
User Certificate service
|
|
"""
|
|
|
|
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(source='certificate_service')
|
|
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
|