refactor!: Removing the cert_whitelist management command
[MICROBA-1052] We are removing the cert_whitelist management command because it does not provide a clear value with the current state of Certificate generation on edx-platform. It adds a user to the whitelist table, but does not generate a certificate. We recommend using the exceptions tab in the Instructor tab of the LMS Course.
This commit is contained in:
@@ -1,131 +0,0 @@
|
||||
"""
|
||||
Management command which sets or gets the certificate allowlist for a given
|
||||
user/course
|
||||
"""
|
||||
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.db.models import Q
|
||||
from opaque_keys.edx.keys import CourseKey
|
||||
|
||||
from lms.djangoapps.certificates.api import (
|
||||
can_be_added_to_allowlist,
|
||||
create_or_update_certificate_allowlist_entry, remove_allowlist_entry,
|
||||
)
|
||||
from lms.djangoapps.certificates.models import CertificateWhitelist
|
||||
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
def get_user_from_identifier(identifier):
|
||||
"""
|
||||
This function takes the string identifier and fetch relevant user object from database
|
||||
"""
|
||||
user = User.objects.filter(Q(username=identifier) | Q(email=identifier)).first()
|
||||
if not user:
|
||||
raise CommandError(f"User {identifier} does not exist.")
|
||||
return user
|
||||
|
||||
|
||||
def update_allowlist(user, course, enable):
|
||||
"""
|
||||
Update the status of a user on the allowlist.
|
||||
"""
|
||||
if enable and can_be_added_to_allowlist(user, course):
|
||||
create_or_update_certificate_allowlist_entry(
|
||||
user,
|
||||
course,
|
||||
"Updated by mngmt cmd",
|
||||
enable
|
||||
)
|
||||
elif not enable:
|
||||
remove_allowlist_entry(user, course)
|
||||
else:
|
||||
print(f"Failed to process allowlist request for student {user.id} in course {course} and enable={enable}.")
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
"""
|
||||
Management command to set or get the certificate allowlist
|
||||
for a given user(s)/course
|
||||
"""
|
||||
|
||||
help = """
|
||||
Sets or gets the certificate whitelist for a given
|
||||
user(s)/course
|
||||
|
||||
Add a user or list of users to the whitelist for a course
|
||||
|
||||
$ ... cert_whitelist --add joe -c "MITx/6.002x/2012_Fall"
|
||||
OR
|
||||
$ ... cert_whitelist --add joe,jenny,tom,jerry -c "MITx/6.002x/2012_Fall"
|
||||
|
||||
Remove a user or list of users from the whitelist for a course
|
||||
|
||||
$ ... cert_whitelist --del joe -c "MITx/6.002x/2012_Fall"
|
||||
OR
|
||||
$ ... cert_whitelist --del joe,jenny,tom,jerry -c "MITx/6.002x/2012_Fall"
|
||||
|
||||
Print out who is whitelisted for a course
|
||||
|
||||
$ ... cert_whitelist -c "MITx/6.002x/2012_Fall"
|
||||
|
||||
"""
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'-a', '--add',
|
||||
metavar='USER',
|
||||
dest='add',
|
||||
default=False,
|
||||
help='user or list of users to add to the certificate allowlist'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-d', '--del',
|
||||
metavar='USER',
|
||||
dest='del',
|
||||
default=False,
|
||||
help='user or list of users to remove from the certificate allowlist'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-c', '--course-id',
|
||||
metavar='COURSE_ID',
|
||||
dest='course_id',
|
||||
default=False,
|
||||
help="course id to query"
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
course_id = options['course_id']
|
||||
if not course_id:
|
||||
raise CommandError("You must specify a course-id")
|
||||
|
||||
# try to parse the serialized course key into a CourseKey
|
||||
course = CourseKey.from_string(course_id)
|
||||
|
||||
if options['add'] and options['del']:
|
||||
raise CommandError("Either remove or add a user, not both")
|
||||
|
||||
if options['add'] or options['del']:
|
||||
user_str = options['add'] or options['del']
|
||||
enable = True if options['add'] else False # pylint: disable=simplifiable-if-expression
|
||||
|
||||
users_list = user_str.split(",")
|
||||
for username in users_list:
|
||||
username = username.strip()
|
||||
if username:
|
||||
try:
|
||||
user = get_user_from_identifier(username)
|
||||
except CommandError as error:
|
||||
print(f"Error occurred retrieving user {username}: {error}")
|
||||
else:
|
||||
update_allowlist(user, course, enable)
|
||||
|
||||
whitelist = CertificateWhitelist.objects.filter(course_id=course)
|
||||
wl_users = '\n'.join(
|
||||
"{u.user.username} {u.user.email} {u.whitelist}".format(u=u)
|
||||
for u in whitelist
|
||||
)
|
||||
print(f"Allowlist for course {course_id}:\n{wl_users}")
|
||||
@@ -1,87 +0,0 @@
|
||||
"""
|
||||
Extremely basic tests for the cert_whitelist command
|
||||
"""
|
||||
import pytest
|
||||
from django.core.management import call_command
|
||||
|
||||
from common.djangoapps.student.tests.factories import CourseEnrollmentFactory, UserFactory
|
||||
from lms.djangoapps.certificates.api import is_on_allowlist
|
||||
from lms.djangoapps.certificates.tests.factories import CertificateAllowlistFactory
|
||||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
|
||||
from xmodule.modulestore.tests.factories import CourseFactory
|
||||
|
||||
|
||||
def test_cert_whitelist_help(capsys):
|
||||
"""
|
||||
Basic test to see if the command will parse and get args
|
||||
"""
|
||||
with pytest.raises(SystemExit):
|
||||
call_command('cert_whitelist', '--help')
|
||||
|
||||
out, err = capsys.readouterr() # pylint: disable=unused-variable
|
||||
assert "COURSE_ID" in out
|
||||
|
||||
|
||||
class CertAllowlistManagementCommandTests(ModuleStoreTestCase):
|
||||
"""
|
||||
Tests for the cert_whitelist management command.
|
||||
"""
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
|
||||
self.user = UserFactory()
|
||||
self.user2 = UserFactory()
|
||||
self.course_run = CourseFactory()
|
||||
self.course_run_key = self.course_run.id # pylint: disable=no-member
|
||||
|
||||
CourseEnrollmentFactory(
|
||||
user=self.user,
|
||||
course_id=self.course_run_key,
|
||||
is_active=True,
|
||||
mode="verified",
|
||||
)
|
||||
CourseEnrollmentFactory(
|
||||
user=self.user2,
|
||||
course_id=self.course_run_key,
|
||||
is_active=True,
|
||||
mode="verified",
|
||||
)
|
||||
|
||||
def test_allowlist_entry_created(self):
|
||||
"""
|
||||
Verify an allowlist entry can be made using the management command.
|
||||
"""
|
||||
call_command(
|
||||
"cert_whitelist",
|
||||
"--add",
|
||||
f"{self.user.username},{self.user2.username}",
|
||||
"-c",
|
||||
f"{self.course_run_key}")
|
||||
|
||||
assert is_on_allowlist(self.user, self.course_run_key)
|
||||
|
||||
assert is_on_allowlist(self.user2, self.course_run_key)
|
||||
|
||||
def test_allowlist_removal(self):
|
||||
"""
|
||||
Verify an allowlist entry can be removed using the management command.
|
||||
"""
|
||||
CertificateAllowlistFactory.create(course_id=self.course_run_key, user=self.user)
|
||||
assert is_on_allowlist(self.user, self.course_run_key)
|
||||
|
||||
call_command(
|
||||
"cert_whitelist",
|
||||
"--del",
|
||||
f"{self.user.username}",
|
||||
"-c",
|
||||
f"{self.course_run_key}")
|
||||
|
||||
assert not is_on_allowlist(self.user, self.course_run_key)
|
||||
|
||||
def test_bad_user_account(self):
|
||||
"""
|
||||
Verify that the management command will continue processing when running into a user account problem.
|
||||
"""
|
||||
call_command("cert_whitelist", "--add", f"gumby,{self.user.username}", "-c", f"{self.course_run_key}")
|
||||
|
||||
assert is_on_allowlist(self.user, self.course_run_key)
|
||||
Reference in New Issue
Block a user