Skip to content

Commit

Permalink
add oprion to generate certificates for a program
Browse files Browse the repository at this point in the history
  • Loading branch information
annagav committed Dec 18, 2024
1 parent e830687 commit 28f102a
Showing 1 changed file with 48 additions and 28 deletions.
76 changes: 48 additions & 28 deletions courses/management/commands/manage_program_certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
4. Un-Revoke a program certificate for a user
./mange.py manage_program_certificates -—unrevoke —-program=<program_readable_id> -—user=<username or email>
5. Generate Program certificate for all enrolled users in the program who have earned it
./mange.py manage_program_certificates --create -—program=<program_readable_id>
"""

from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError

from courses.api import generate_program_certificate, manage_program_certificate_access
from courses.models import Program
from courses.models import Program, ProgramEnrollment
from users.api import fetch_user

User = get_user_model()
Expand All @@ -45,7 +47,7 @@ def add_arguments(self, parser):
"--user",
type=str,
help="The id, email or username of the enrolled User",
required=True,
required=False,
)
parser.add_argument(
"--program",
Expand Down Expand Up @@ -90,12 +92,13 @@ def handle(self, *args, **options): # pylint: disable=too-many-locals # noqa:
"The command needs a valid action e.g. --revoke, --unrevoke, --create." # noqa: EM101
)

try:
user = fetch_user(user)
except User.DoesNotExist:
raise CommandError( # noqa: B904
f"Could not find a user with <username or email>={user}." # noqa: EM102
)
if user:
try:
user = fetch_user(user)
except User.DoesNotExist:
raise CommandError( # noqa: B904
f"Could not find a user with <username or email>={user}." # noqa: EM102
)

# Unable to obtain a program object based on the provided program readable id
try:
Expand All @@ -105,6 +108,8 @@ def handle(self, *args, **options): # pylint: disable=too-many-locals # noqa:

# Handle revoke/un-revoke of a certificate
if revoke or unrevoke:
if not user:
raise CommandError("If you want to revoke/unrevoke a user argument is required")
revoke_status = manage_program_certificate_access(
user=user,
program=program,
Expand All @@ -125,27 +130,42 @@ def handle(self, *args, **options): # pylint: disable=too-many-locals # noqa:

# Handle the creation of the program certificate.
elif create:
# If -f or --force argument is provided, we'll forcefully generate the program certificate
certificate, created_cert = generate_program_certificate(
user=user, program=program, force_create=force_create
)
success_result = True
if user:

if created_cert:
cert_status = "created"
elif certificate and not created_cert:
cert_status = "already exists"
else:
success_result = False
cert_status = (
"ignored, certificates for required courses are missing, use "
"-f or --force argument to forcefully create a program certificate"
# If -f or --force argument is provided, we'll forcefully generate the program certificate
certificate, created_cert = generate_program_certificate(
user=user, program=program, force_create=force_create
)
success_result = True

if created_cert:
cert_status = "created"
elif certificate and not created_cert:
cert_status = "already exists"
else:
success_result = False
cert_status = (
"ignored, certificates for required courses are missing, use "
"-f or --force argument to forcefully create a program certificate"
)

result_summary = f"Certificate: {cert_status}"

result_summary = f"Certificate: {cert_status}"
result = f"Processed user {user.username} ({user.email}) in program {program.readable_id}. Result - {result_summary}"
result_output = self.style.SUCCESS(result)
if not success_result:
result_output = self.style.ERROR(result)
self.stdout.write(result_output)
else:

# generate certificates for all enrolled users
enrollments = ProgramEnrollment.objects.filter(program=program)
cert_count = 0
for enrollment in enrollments:
certificate, created_cert = generate_program_certificate(
user=enrollment.user, program=program
)
if created_cert:
cert_count += 1

result = f"Processed user {user.username} ({user.email}) in program {program.readable_id}. Result - {result_summary}"
result_output = self.style.SUCCESS(result)
if not success_result:
result_output = self.style.ERROR(result)
self.stdout.write(result_output)
self.stdout.write(f"Successfully created {cert_count} certificates for program {program.readable_id}")

0 comments on commit 28f102a

Please sign in to comment.