diff --git a/lms/djangoapps/certificates/__init__.py b/lms/djangoapps/certificates/__init__.py index e69de29bb2..755f73d708 100644 --- a/lms/djangoapps/certificates/__init__.py +++ b/lms/djangoapps/certificates/__init__.py @@ -0,0 +1,3 @@ +""" Certificates app """ +# this is here to support registering the signals in signals.py +from . import signals diff --git a/lms/djangoapps/certificates/signals.py b/lms/djangoapps/certificates/signals.py new file mode 100644 index 0000000000..27b2e2fe84 --- /dev/null +++ b/lms/djangoapps/certificates/signals.py @@ -0,0 +1,26 @@ +""" Signal handler for enabling self-generated certificates by default +for self-paced courses. +""" +from celery.task import task +from django.dispatch.dispatcher import receiver + +from certificates.models import CertificateGenerationCourseSetting +from xmodule.modulestore.django import SignalHandler, modulestore + + +@receiver(SignalHandler.course_published) +def _listen_for_course_publish(sender, course_key, **kwargs): # pylint: disable=unused-argument + """ Catches the signal that a course has been published in Studio and + enable the self-generated certificates by default for self-paced + courses. + """ + enable_self_generated_certs.delay(course_key) + + +@task() +def enable_self_generated_certs(course_key): + """Enable the self-generated certificates by default for self-paced courses.""" + course = modulestore().get_course(course_key) + is_enabled_for_course = CertificateGenerationCourseSetting.is_enabled_for_course(course_key) + if course.self_paced and not is_enabled_for_course: + CertificateGenerationCourseSetting.set_enabled_for_course(course_key, True) diff --git a/lms/djangoapps/certificates/tests/test_signals.py b/lms/djangoapps/certificates/tests/test_signals.py new file mode 100644 index 0000000000..7e27c496b0 --- /dev/null +++ b/lms/djangoapps/certificates/tests/test_signals.py @@ -0,0 +1,31 @@ +""" Unit tests for enabling self-generated certificates by default +for self-paced courses. +""" +from certificates import api as certs_api +from certificates.models import CertificateGenerationConfiguration +from certificates.signals import _listen_for_course_publish +from openedx.core.djangoapps.self_paced.models import SelfPacedConfiguration +from xmodule.modulestore.tests.factories import CourseFactory +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase + + +class SelfGeneratedCertsSignalTest(ModuleStoreTestCase): + """ Tests for enabling self-generated certificates by default + for self-paced courses. + """ + + def setUp(self): + super(SelfGeneratedCertsSignalTest, self).setUp() + SelfPacedConfiguration(enabled=True).save() + self.course = CourseFactory.create(self_paced=True) + # Enable the feature + CertificateGenerationConfiguration.objects.create(enabled=True) + + def test_cert_generation_enabled_for_self_paced(self): + """ Verify the signal enable the self-generated certificates by default for + self-paced courses. + """ + self.assertFalse(certs_api.cert_generation_enabled(self.course.id)) + + _listen_for_course_publish('store', self.course.id) + self.assertTrue(certs_api.cert_generation_enabled(self.course.id)) diff --git a/lms/djangoapps/instructor/views/instructor_dashboard.py b/lms/djangoapps/instructor/views/instructor_dashboard.py index 04f59d898c..bce031e887 100644 --- a/lms/djangoapps/instructor/views/instructor_dashboard.py +++ b/lms/djangoapps/instructor/views/instructor_dashboard.py @@ -327,6 +327,7 @@ def _section_certificates(course): 'example_certificate_status': example_cert_status, 'can_enable_for_course': can_enable_for_course, 'enabled_for_course': certs_api.cert_generation_enabled(course.id), + 'is_self_paced': course.self_paced, 'instructor_generation_enabled': instructor_generation_enabled, 'html_cert_enabled': html_cert_enabled, 'active_certificate': certs_api.get_active_web_certificate(course), diff --git a/lms/templates/instructor/instructor_dashboard_2/certificates.html b/lms/templates/instructor/instructor_dashboard_2/certificates.html index 8b4f4a3fc5..40e44d8d9e 100644 --- a/lms/templates/instructor/instructor_dashboard_2/certificates.html +++ b/lms/templates/instructor/instructor_dashboard_2/certificates.html @@ -50,27 +50,29 @@ from openedx.core.djangolib.js_utils import dump_js_escaped_json, js_escaped_str % endif -
${_("You must successfully generate example certificates before you enable student-generated certificates.")}
- - % endif -${_("You must successfully generate example certificates before you enable student-generated certificates.")}
+ + % endif +