models.py 4.21 KB
Newer Older
1 2
from django.contrib.auth.models import User
from django.db import models
3
from datetime import datetime
4

Victor Shnayder committed
5
"""
6 7
Certificates are created for a student and an offering of a course.

8
When a certificate is generated, a unique ID is generated so that
9
the certificate can be verified later. The ID is a UUID4, so that
John Jarvis committed
10
it can't be easily guessed and so that it is unique.
11

12 13 14 15
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.
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

State diagram:

[deleted,error,unavailable] [error,downloadable]
            +                +             +
            |                |             |
            |                |             |
         add_cert       regen_cert     del_cert
            |                |             |
            v                v             v
       [generating]    [regenerating]  [deleting]
            +                +             +
            |                |             |
       certificate      certificate    certificate
         created       removed,created   deleted
            +----------------+-------------+------->[error]
            |                |             |
            |                |             |
            v                v             v
      [downloadable]   [downloadable]  [deleted]

Victor Shnayder committed
38
"""
John Jarvis committed
39

40

41
class CertificateStatuses(object):
John Jarvis committed
42 43 44 45 46 47
    unavailable = 'unavailable'
    generating = 'generating'
    regenerating = 'regenerating'
    deleting = 'deleting'
    deleted = 'deleted'
    downloadable = 'downloadable'
48
    notpassing = 'notpassing'
John Jarvis committed
49 50
    error = 'error'

51

52
class GeneratedCertificate(models.Model):
53
    user = models.ForeignKey(User)
54 55 56 57 58 59 60 61
    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')
62
    name = models.CharField(blank=True, max_length=255)
63 64 65 66
    created_date = models.DateTimeField(
            auto_now_add=True, default=datetime.now)
    modified_date = models.DateTimeField(
            auto_now=True, default=datetime.now)
John Jarvis committed
67
    error_reason = models.CharField(max_length=512, blank=True, default='')
68 69

    class Meta:
John Jarvis committed
70 71
        unique_together = (('user', 'course_id'),)

72 73

def certificate_status_for_student(student, course_id):
74
    '''
75 76
    This returns a dictionary with a key for status, and other information.
    The status is one of the following:
77

78 79 80
    unavailable  - No entry for this student--if they are actually in
                   the course, they probably have not been graded for
                   certificate generation yet.
81 82 83
    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,
John Jarvis committed
84
                   but it has not been generated yet.
85
    deleting     - A request has been made to delete a certificate.
John Jarvis committed
86

87 88
    deleted      - The certificate has been deleted.
    downloadable - The certificate is available for download.
89
    notpassing   - The student was graded but is not passing
90 91 92

    If the status is "downloadable", the dictionary also contains
    "download_url".
93

94
    If the student has been graded, the dictionary also contains their
95
    grade for the course with the key "grade".
96
    '''
97

98 99
    try:
        generated_certificate = GeneratedCertificate.objects.get(
100
                user=student, course_id=course_id)
101 102 103
        d = {'status': generated_certificate.status}
        if generated_certificate.grade:
            d['grade'] = generated_certificate.grade
104
        if generated_certificate.status == CertificateStatuses.downloadable:
105
            d['download_url'] = generated_certificate.download_url
106 107

        return d
108 109
    except GeneratedCertificate.DoesNotExist:
        pass
110
    return {'status': CertificateStatuses.unavailable}