chore: remove cert_allowlist_generation management command

[MICROBA-1100]
* Remove `cert_allowlist_generation` management command. This has been replaced by the `cert_generation` management command which can handle generation of allowlist and v2 certificates.
* Remove AllowListGenerationConfiguration configuration model.
This commit is contained in:
Justin Hynes
2021-04-01 08:53:14 -04:00
parent e4206265cc
commit 346fe90002
5 changed files with 17 additions and 209 deletions

View File

@@ -13,7 +13,6 @@ from django.utils.safestring import mark_safe
from organizations.api import get_organizations
from lms.djangoapps.certificates.models import (
AllowListGenerationConfiguration,
CertificateGenerationConfiguration,
CertificateGenerationCommandConfiguration,
CertificateGenerationCourseSetting,
@@ -92,15 +91,11 @@ class CertificateGenerationCourseSettingAdmin(admin.ModelAdmin):
show_full_result_count = False
@admin.register(AllowListGenerationConfiguration)
class AllowListGenerationConfigurationAdmin(ConfigurationModelAdmin):
pass
@admin.register(CertificateGenerationCommandConfiguration)
class CertificateGenerationCommandConfigurationAdmin(ConfigurationModelAdmin):
pass
admin.site.register(CertificateGenerationConfiguration)
admin.site.register(CertificateGenerationCourseSetting, CertificateGenerationCourseSettingAdmin)
admin.site.register(CertificateHtmlViewConfiguration, ConfigurationModelAdmin)

View File

@@ -1,95 +0,0 @@
"""
Management command to generate allowlist certificates for one or more users in a given course run.
"""
import logging
import shlex
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from lms.djangoapps.certificates.generation_handler import generate_allowlist_certificate_task
from lms.djangoapps.certificates.models import AllowListGenerationConfiguration
User = get_user_model()
log = logging.getLogger(__name__)
class Command(BaseCommand):
"""
Management command to generate allowlist certificates for one or more users in a given course run.
Example usage:
./manage.py lms cert_allowlist_generation -u 123 456 -c course-v1:edX+DemoX+Demo_Course
"""
help = """
Generate allowlist certificates for one or more users in a given course run.
"""
def add_arguments(self, parser):
parser.add_argument(
'-u', '--user',
nargs="+",
metavar='USER',
dest='user',
help='user_id or space-separated list of user_ids for whom to generate allowlist certificates'
)
parser.add_argument(
'-c', '--course-key',
metavar='COURSE_KEY',
dest='course_key',
help="course run key"
)
parser.add_argument(
'--args-from-database',
action='store_true',
help='Use arguments from the AllowListGenerationConfiguration model instead of the command line.',
)
def get_args_from_database(self):
""" Returns an options dictionary from the current AllowListGenerationConfiguration model. """
config = AllowListGenerationConfiguration.current()
if not config.enabled:
raise CommandError("AllowListGenerationConfiguration is disabled, but --args-from-database was requested.")
argv = shlex.split(config.arguments)
parser = self.create_parser("manage.py", "cert_allowlist_generation")
return vars(parser.parse_args(argv)) # we want a dictionary, not a non-iterable Namespace object
def handle(self, *args, **options):
# database args will override cmd line args
if options['args_from_database']:
options = self.get_args_from_database()
# Since we're optionally using database args we can't simply make users required in the arguments
if not options["user"]:
raise CommandError("You must specify a list of users")
course_key = options['course_key']
if not course_key:
raise CommandError("You must specify a course-key")
# Parse the serialized course key into a CourseKey
try:
course_key = CourseKey.from_string(course_key)
except InvalidKeyError as e:
raise CommandError("You must specify a valid course-key") from e
# Loop over each user, and ask that a cert be generated for them
users_str = options['user']
for user_id in users_str:
user = None
try:
user = User.objects.get(id=user_id)
except User.DoesNotExist:
log.warning('User {user} could not be found'.format(user=user_id))
if user is not None:
log.info(
'Calling generate_allowlist_certificate_task for {user} : {course}'.format(
user=user.id,
course=course_key
))
generate_allowlist_certificate_task(user, course_key)

View File

@@ -1,84 +0,0 @@
"""
Tests for the cert_allowlist_generation command
"""
from unittest import mock
import pytest
from django.core.management import CommandError, call_command
from edx_toggles.toggles.testutils import override_waffle_flag
from waffle.testutils import override_switch
from common.djangoapps.student.tests.factories import CourseEnrollmentFactory, UserFactory
from lms.djangoapps.certificates.generation_handler import CERTIFICATES_USE_ALLOWLIST
from lms.djangoapps.certificates.tests.factories import CertificateWhitelistFactory
from lms.djangoapps.certificates.tests.test_generation_handler import AUTO_GENERATION_SWITCH_NAME, ID_VERIFIED_METHOD
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory
@override_switch(AUTO_GENERATION_SWITCH_NAME, active=True)
@override_waffle_flag(CERTIFICATES_USE_ALLOWLIST, active=True)
@mock.patch(ID_VERIFIED_METHOD, mock.Mock(return_value=True))
class CertAllowlistGenerationTests(ModuleStoreTestCase):
"""
Tests for the cert_allowlist_generation management command
"""
def setUp(self):
super().setUp()
# Create users, a course run, and enrollments
self.user = UserFactory()
self.course_run = CourseFactory()
self.course_run_key = self.course_run.id # pylint: disable=no-member
self.enrollment = CourseEnrollmentFactory(
user=self.user,
course_id=self.course_run_key,
is_active=True,
mode="verified",
)
self.user2 = UserFactory()
self.enrollment2 = CourseEnrollmentFactory(
user=self.user2,
course_id=self.course_run_key,
is_active=True,
mode="verified",
)
# Whitelist users
CertificateWhitelistFactory.create(course_id=self.course_run_key, user=self.user)
CertificateWhitelistFactory.create(course_id=self.course_run_key, user=self.user2)
def test_command_with_missing_param(self):
"""
Verify command with a missing param
"""
with pytest.raises(CommandError, match="You must specify a course-key"):
call_command("cert_allowlist_generation", "--u", self.user.username)
def test_command_with_invalid_key(self):
"""
Verify command with an invalid course run key
"""
with pytest.raises(CommandError, match="You must specify a valid course-key"):
call_command("cert_allowlist_generation", "--u", self.user.username, "--c", "blah")
def test_successful_generation(self):
"""
Test generation for 1 user
"""
call_command("cert_allowlist_generation", "--u", self.user.id, "--c", self.course_run_key)
def test_successful_generation_multiple_users(self):
"""
Test generation for multiple user
"""
call_command("cert_allowlist_generation",
"--u",
self.user.id,
self.user2.id,
"999999", # non-existent userid
"--c",
self.course_run_key)

View File

@@ -0,0 +1,16 @@
# Generated by Django 2.2.19 on 2021-04-01 12:48
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('certificates', '0023_certificategenerationcommandconfiguration'),
]
operations = [
migrations.DeleteModel(
name='AllowListGenerationConfiguration',
),
]

View File

@@ -1223,30 +1223,6 @@ def create_course_group_badge(sender, user, course_key, status, **kwargs): # py
course_group_check(user, course_key)
class AllowListGenerationConfiguration(ConfigurationModel):
"""
Manages configuration for a run of the cert_allowlist_generation management command.
.. no_pii:
"""
class Meta(object):
app_label = 'certificates'
verbose_name = 'cert_allowlist_generation argument'
arguments = models.TextField(
blank=True,
help_text=(
"Arguments to be passted to cert_allowlist_generation management command. " +
"Specify like `-u edx verified -c course-v1:edX+DemoX+Demo_Course`"
),
default='',
)
def __str__(self):
return str(self.arguments)
class CertificateGenerationCommandConfiguration(ConfigurationModel):
"""
Manages configuration for a run of the cert_generation management command.