models.py 7.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.urls import reverse_lazy

from courses.models import Course, CourseStudent, Class
from courses.workspaces.models import Workspace

#FIXME: remove has_name dependency. Is this really needed?
from courses.utils import hash_name


class CourseCertification(models.Model):
    TYPES = (
        ('receipt', _('Receipt')),
        ('certificate', _('Certificate')),
    )

    type = models.CharField(
        _('Certificate Type'),
        choices=TYPES,
        max_length=127,
    )
    course_student = models.OneToOneField(
        CourseStudent,
26
        models.CASCADE,
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        verbose_name=_('Enrollment'),
        related_name='certificate',
    )
    created_date = models.DateTimeField(
        _('Created'),
        auto_now_add=True,
    )
    modified_date = models.DateTimeField(
        _('Last modified'),
        auto_now=True,
    )
    is_valid = models.BooleanField(
        _('Certificate is valid'),
        default=False,
    )
    course_workload = models.TextField(
        _('Workload'),
        blank=True,
    )
    course_total_units = models.IntegerField(
        _('Total units'),
        blank=True,
    )
    link_hash = models.CharField(
        _('Hash'),
        max_length=255,
        unique=True,
    )

    @property
    def student(self):
        return self.course_student.user

    @property
    def course(self):
        return self.course_student.course

    @property
    def get_approved_process(self):
        return CertificationProcess.objects.get(course_certification=self.id,
                                                approved=True)

    @property
    def get_absolute_url(self):
        return reverse_lazy('courses_legacy:certificate', args=[self.link_hash])

73 74 75 76 77
    # FIXME: this is a compatibility patch and must be removed in the future
    @property
    def document_type(self):
        return self.type

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    def save(self, *args, **kwargs):
        self.course_workload = self.course_student.course.workload
        self.course_total_units = self.course_student.units_done.count()
        super(CourseCertification, self).save(*args, **kwargs)

    class Meta:
        verbose_name = _("Certificate")

    def __str__(self):
        return '({0}): {1}'.format(self.course_student, self.is_valid)


class Evaluation(models.Model):
    min_grade = models.IntegerField(
        _('Evaluation grade needed'),
        blank=True,
    )
    date = models.DateTimeField(
        _('Evaluation date'),
        blank=True,
    )
    results_date = models.DateTimeField(
        _('Evaluation results date'),
        blank=True,
    )
    instructions = models.CharField(
        _('Comments'),
        max_length=255,
    )
    klass = models.ForeignKey(
        Class,
        models.CASCADE,
        verbose_name=_('Class'),
        related_name='evaluations',
    )

    class Meta:
        verbose_name = _('Evaluation')

    def __str__(self):
        return '({0}): {1}'.format(self.klass, self.instructions)


class CertificationProcess(models.Model):

    student = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        models.CASCADE,
        verbose_name=_('Student'),
        related_name='processes',
    )
    course_certification = models.ForeignKey(
        CourseCertification,
        models.CASCADE,
        null=True,
        related_name="processes",
        verbose_name=_('Certificate'),
    )
    comments = models.CharField(
        _('Comments'),
        max_length=255,
        null=True,
        blank=True,
    )
    created_date = models.DateTimeField(
        _('Created'),
        auto_now_add=True,
    )
    evaluation_grade = models.IntegerField(
        _('Evaluation grade'),
        blank=True,
        null=True,
    )
    approved = models.BooleanField(
        _('Approved'),
        default=False,
    )
    no_show = models.BooleanField(
        _('No show'),
        default=False,
    )
    evaluation = models.ForeignKey(
        Evaluation,
        models.SET_NULL,
        verbose_name=_('Evaluation'),
        blank=True,
        null=True,
        related_name='processes',
    )
    klass = models.ForeignKey(
        Class,
        models.CASCADE,
        verbose_name=_('Class'),
        related_name='processes',
    )
    active = models.BooleanField(
        _('Active'),
        default=True,
    )

    @property
    def certification_progress(self):
        return {
            'receipt': self.course_certification is not None,
            'grade': self.evaluation_grade is not None,
            'approved': self.approved
        }

    class Meta:
        verbose_name = _("Certification Process")

    def __str__(self):
        return '({0}): {1}'.format(
            self.course_certification.course_student.user, self.evaluation)


class CertificateTemplate(models.Model):
195 196 197 198
    TYPES = (
        ('receipt', _('Receipt')),
        ('certificate', _('Certificate')),
    )
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

    course = models.ForeignKey(
        Course,
        models.CASCADE,
        verbose_name=_('Course'),
    )
    role = models.CharField(
        _('Role'),
        max_length=128,
        blank=True,
        null=True,
    )
    name = models.CharField(
        _('Signature Name'),
        blank=True,
        max_length=255,
        null=True,
    )
217 218 219 220 221 222 223 224 225 226 227 228
    second_role = models.CharField(
        _('Second Role'),
        max_length=128,
        blank=True,
        null=True,
    )
    second_name = models.CharField(
        _('Second Signature Name'),
        blank=True,
        max_length=255,
        null=True,
    )
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    cert_logo = models.ImageField(
        _('Logo'),
        null=True,
        blank=True,
        upload_to=hash_name('logo', 'organization_name'),
    )
    base_logo = models.ImageField(
        _('Logo'),
        null=True,
        blank=True,
        upload_to=hash_name('base_logo', 'organization_name'),
    )
    signature = models.ImageField(
        _('Signature'),
        null=True,
        blank=True,
        upload_to=hash_name('signature', 'organization_name'),
    )
247 248 249 250 251 252
    second_signature = models.ImageField(
        _('Second Signature'),
        null=True,
        blank=True,
        upload_to=hash_name('second_signature', 'organization_name'),
    )
253 254 255 256 257 258
    site_logo = models.ImageField(
        _('Site Logo'),
        null=True,
        blank=True,
        upload_to='certificates_files'
    )
Bruno Martin's avatar
Bruno Martin committed
259 260 261 262 263 264 265 266 267 268
    background_image = models.ImageField(
        _('Background image'),
        null=True,
        blank=True,
        upload_to='certificates_files'
    )
    display_background_image = models.BooleanField(
        _('Display background image'),
        default=True,
    )
269 270 271 272 273 274
    organization_name = models.CharField(
        _('Name'),
        max_length=255,
        blank=True,
        null=True,
    )
Bruno Martin's avatar
Bruno Martin committed
275 276 277 278 279 280
    text = models.TextField(
        _('Content'),
        default='',
        null=True,
        blank=True,
    )
281 282 283 284 285 286 287 288 289 290 291
    document_type = models.CharField(
        _('Certificate Type'),
        choices=TYPES,
        max_length=127,
        default=TYPES[0][0]
    )
    workspace = models.ForeignKey(
        Workspace,
        models.CASCADE,
        null=True
    )
292 293 294

    class Meta:
        verbose_name = _('Certificate Template')
295
        unique_together = ('workspace', 'course', 'document_type')
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

    def __str__(self):
        return '({0})'.format(self.course)

    @property
    def cert_logo_url(self):
        if self.cert_logo:
            return self.cert_logo.url
        return ''

    @property
    def base_logo_url(self):
        if self.base_logo:
            return self.base_logo.url
        return ''

    @property
    def signature_url(self):
        if self.signature:
            return self.signature.url
        return ''

318 319 320 321 322 323
    @property
    def second_signature_url(self):
        if self.second_signature:
            return self.second_signature.url
        return ''

324 325 326 327 328 329 330
    @property
    def site_logo_url(self):
        if self.site_logo:
            return self.site_logo.url
        return ''

    @property
331
    def contract(self):
332
        return self.workspace