Commit 08b818ab authored by Laury Bueno's avatar Laury Bueno
Browse files

[Certification] Add a management script to create missing CourseCertifications

parent 6df26efa
from import BaseCommand, CommandError
from django.contrib.auth import get_user_model
from django.db import transaction
from base64 import urlsafe_b64encode as ub64
from hashlib import sha1
from time import time
from courses.models import CourseStudent
from courses.certification.models import CourseCertification, CertificateTemplate
User = get_user_model()
class Command(BaseCommand):
help = 'Create CourseCertitification objects for every CourseStudent that doesn\'t yet have one'
def add_arguments(self, parser):
help='Certificate any users that have already earned a valid receipt'
help='Don\'t save any changes, just print results'
def handle(self, *args, **options):
# Create any missing CourseCertification object
created_cc = 0
for cs in CourseStudent.objects.all():
if CourseCertification.objects.filter(course_student=cs).exists():
link_hash = ub64(sha1((str(time()) + cs.user.last_name).encode('utf-8')).digest()[0:6])
receipt = CourseCertification(course_student=cs,
is_valid=True, link_hash=link_hash.decode())
if not options['dry_run']:
created_cc += 1
# If '--certificate' was passed, transform any earned receipt in a certificate
certificates_earned = 0
if options['certificate']:
for cc in CourseCertification.objects.all():
if cc.course_student.can_emmit_receipt() and cc.type == CourseCertification.TYPES[0][0]:
cc.type = CourseCertification.TYPES[1][0]
if not options['dry_run']:
certificates_earned += 1
print(f'CourseCertification objects created: {created_cc}')
if options['certificate']:
print(f'Certificates earned: {certificates_earned}')
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