Commit 416b5458 by John Jarvis Committed by Carlos Andrés Rocha

Certificate model for tracking certificate statuses

parent 29ed95c1
......@@ -7,71 +7,68 @@ Certificates are created for a student and an offering of a course.
When a certificate is generated, a unique ID is generated so that
the certificate can be verified later. The ID is a UUID4, so that
it can't be easily guessed and so that it is unique. Even though
we save these generated certificates (for later verification), we
also record the UUID so that if we regenerate the certificate it
will have the same UUID.
it can't be easily guessed and so that it is unique.
Certificates are generated in batches by a cron job, when a
certificate is available for download the GeneratedCertificate
table is updated with information that will be displayed
on the course overview page.
'''
class CertificateStatuses(object):
unavailable='unavailable'
generating='generating'
regenerating='regenerating'
deleting='deleting'
deleted='deleted'
downloadable='downloadable'
error='error'
class GeneratedCertificate(models.Model):
user = models.ForeignKey(User)
course_id = models.CharField(max_length=255, default=False)
certificate_id = models.CharField(max_length=32, default=False)
graded_certificate_id = models.CharField(max_length=32, default=False)
download_url = models.CharField(max_length=128, default=False)
graded_download_url = models.CharField(max_length=128, default=False)
grade = models.CharField(max_length=5, default=False)
key = models.CharField(max_length=32, default=False)
# enabled should only be true if the student has earned a passing grade
# in the course.
enabled = models.BooleanField(default=False)
def certificate_state_for_student(student):
course_id = models.CharField(max_length=255, blank=True, default='')
verify_uuid = models.CharField(max_length=32, blank=True, default='')
download_uuid = models.CharField(max_length=32, blank=True, default='')
download_url = models.CharField(max_length=128, blank=True, default='')
grade = models.CharField(max_length=5, blank=True, default='')
key = models.CharField(max_length=32, blank=True, default='')
distinction = models.BooleanField(default=False)
status = models.CharField(max_length=32, default='unavailable')
class Meta:
unique_together= (('user', 'course_id'),)
def certificate_status_for_student(student, course_id):
'''
This returns a dictionary with a key for state, and other information.
The state is one of the following:
This returns a dictionary with a key for status, and other information.
The status is one of the following:
unavailable - A student is not eligible for a certificate.
generating - A student has requested a certificate,
but it is not generated yet.
downloadable - The certificate has been requested and is
available for download.
If the state is "downloadable", the dictionary also contains
"download_url" and "graded_download_url".
generating - A request has been made to generate a certificate,
but it has not been generated yet.
regenerating - A request has been made to regenerate a certificate,
but it has not been generated yet.
deleting - A request has been made to delete a certificate.
deleted - The certificate has been deleted.
downloadable - The certificate is available for download.
If the status is "downloadable", the dictionary also contains
"download_url".
'''
try:
generated_certificate = GeneratedCertificate.objects.get(
user=student)
if generated_certificate.enabled:
if generated_certificate.download_url:
return {
'state': 'downloadable',
'download_url':
generated_certificate.download_url,
'graded_download_url':
generated_certificate.graded_download_url
}
else:
return {'state': 'generating'}
else:
# If enabled=False, there is no certificate available
# Our output will be the same as if the
# GeneratedCertificate did not exist
pass
user=student, course_id=course_id)
if generated_certificate.status == CertificateStatuses.downloadable:
return {
'status': CertificateStatuses.downloadable,
'download_url': generated_certificate.download_url,
}
else:
return {'status': generated_certificate.status}
except GeneratedCertificate.DoesNotExist:
pass
return {'state': 'unavailable'}
return {'status': 'unavailable'}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment