signals.py 1.04 KB
Newer Older
1
from django.db.models.signals import post_save
2
from django.dispatch import receiver
3 4 5 6 7

from base64 import urlsafe_b64encode as ub64
from hashlib import sha1
from time import time

8
from courses.models import Course, CourseStudent
9
from courses.certification.models import CourseCertification, CertificateTemplate
10 11 12


@receiver(post_save, sender=CourseStudent)
13
def course_student_created_or_updated(sender, created, instance: CourseStudent, **kwargs):
14 15

    if created:
16
        link_hash = ub64(sha1((str(time()) + instance.user.last_name).encode('utf-8')).digest()[0:6])
17
        receipt = CourseCertification(course_student=instance,
18 19
                                      type=CourseCertification.TYPES[0][0],
                                      is_valid=True, link_hash=link_hash.decode())
20 21 22 23 24 25 26
        receipt.save()


@receiver(post_save, sender=Course)
def course_created(sender, created, instance, **kwargs):

    if created:
27 28
        for type_ in CertificateTemplate.TYPES:
            ct = CertificateTemplate(course=instance, document_type=type_[0])
29
            ct.save()