19194. Un-Revoke a program certificate for a user
2020./mange.py manage_program_certificates -—unrevoke —-program=<program_readable_id> -—user=<username or email>
2121
22+ 5. Generate Program certificate for all enrolled users in the program who have earned it
23+ ./mange.py manage_program_certificates --create -—program=<program_readable_id>
2224"""
2325
2426from django .contrib .auth import get_user_model
2527from django .core .management .base import BaseCommand , CommandError
2628
2729from courses .api import generate_program_certificate , manage_program_certificate_access
28- from courses .models import Program
30+ from courses .models import Program , ProgramEnrollment
2931from users .api import fetch_user
3032
3133User = get_user_model ()
@@ -45,7 +47,7 @@ def add_arguments(self, parser):
4547 "--user" ,
4648 type = str ,
4749 help = "The id, email or username of the enrolled User" ,
48- required = True ,
50+ required = False ,
4951 )
5052 parser .add_argument (
5153 "--program" ,
@@ -75,7 +77,7 @@ def add_arguments(self, parser):
7577
7678 super ().add_arguments (parser )
7779
78- def handle (self , * args , ** options ): # pylint: disable=too-many-locals # noqa: ARG002
80+ def handle (self , * args , ** options ): # pylint: disable=too-many-locals # noqa: ARG002, C901
7981 """Handle command execution"""
8082
8183 revoke = options .get ("revoke" )
@@ -90,12 +92,13 @@ def handle(self, *args, **options): # pylint: disable=too-many-locals # noqa:
9092 "The command needs a valid action e.g. --revoke, --unrevoke, --create." # noqa: EM101
9193 )
9294
93- try :
94- user = fetch_user (user )
95- except User .DoesNotExist :
96- raise CommandError ( # noqa: B904
97- f"Could not find a user with <username or email>={ user } ." # noqa: EM102
98- )
95+ if user :
96+ try :
97+ user = fetch_user (user )
98+ except User .DoesNotExist :
99+ raise CommandError ( # noqa: B904
100+ f"Could not find a user with <username or email>={ user } ." # noqa: EM102
101+ )
99102
100103 # Unable to obtain a program object based on the provided program readable id
101104 try :
@@ -105,6 +108,10 @@ def handle(self, *args, **options): # pylint: disable=too-many-locals # noqa:
105108
106109 # Handle revoke/un-revoke of a certificate
107110 if revoke or unrevoke :
111+ if not user :
112+ raise CommandError (
113+ "If you want to revoke/unrevoke a user argument is required" # noqa: EM101
114+ )
108115 revoke_status = manage_program_certificate_access (
109116 user = user ,
110117 program = program ,
@@ -125,27 +132,44 @@ def handle(self, *args, **options): # pylint: disable=too-many-locals # noqa:
125132
126133 # Handle the creation of the program certificate.
127134 elif create :
128- # If -f or --force argument is provided, we'll forcefully generate the program certificate
129- certificate , created_cert = generate_program_certificate (
130- user = user , program = program , force_create = force_create
131- )
132- success_result = True
133-
134- if created_cert :
135- cert_status = "created"
136- elif certificate and not created_cert :
137- cert_status = "already exists"
138- else :
139- success_result = False
140- cert_status = (
141- "ignored, certificates for required courses are missing, use "
142- "-f or --force argument to forcefully create a program certificate"
135+ if user :
136+ # If -f or --force argument is provided, we'll forcefully generate the program certificate
137+ certificate , created_cert = generate_program_certificate (
138+ user = user , program = program , force_create = force_create
143139 )
140+ success_result = True
141+
142+ if created_cert :
143+ cert_status = "created"
144+ elif certificate and not created_cert :
145+ cert_status = "already exists"
146+ else :
147+ success_result = False
148+ cert_status = (
149+ "ignored, certificates for required courses are missing, use "
150+ "-f or --force argument to forcefully create a program certificate"
151+ )
152+
153+ result_summary = f"Certificate: { cert_status } "
144154
145- result_summary = f"Certificate: { cert_status } "
155+ result = f"Processed user { user .username } ({ user .email } ) in program { program .readable_id } . Result - { result_summary } "
156+ result_output = self .style .SUCCESS (result )
157+ if not success_result :
158+ result_output = self .style .ERROR (result )
159+ self .stdout .write (result_output )
160+ else :
161+ # generate certificates for all enrolled users
162+ enrollments = ProgramEnrollment .objects .filter (program = program )
163+ cert_count = 0
164+ for enrollment in enrollments :
165+ certificate , created_cert = generate_program_certificate (
166+ user = enrollment .user , program = program
167+ )
168+ if created_cert :
169+ cert_count += 1
146170
147- result = f"Processed user { user . username } ( { user . email } ) in program { program . readable_id } . Result - { result_summary } "
148- result_output = self .style .SUCCESS (result )
149- if not success_result :
150- result_output = self . style . ERROR ( result )
151- self . stdout . write ( result_output )
171+ self . stdout . write (
172+ self .style .SUCCESS (
173+ f"Successfully created { cert_count } certificates for program { program . readable_id } "
174+ )
175+ )
0 commit comments