From 47db17c79eae95d5ba063b9751d9534f5e5fe2fd Mon Sep 17 00:00:00 2001 From: Sanford Student Date: Thu, 15 Jun 2017 14:14:52 -0400 Subject: [PATCH] instructor task for auto cert creation --- .../certificates/config/__init__.py | 0 lms/djangoapps/certificates/config/waffle.py | 19 ++++++++++++++ lms/djangoapps/certificates/tasks.py | 26 +++++++++++++++++++ .../certificates/tests/test_tasks.py | 21 +++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 lms/djangoapps/certificates/config/__init__.py create mode 100644 lms/djangoapps/certificates/config/waffle.py create mode 100644 lms/djangoapps/certificates/tasks.py create mode 100644 lms/djangoapps/certificates/tests/test_tasks.py diff --git a/lms/djangoapps/certificates/config/__init__.py b/lms/djangoapps/certificates/config/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lms/djangoapps/certificates/config/waffle.py b/lms/djangoapps/certificates/config/waffle.py new file mode 100644 index 0000000000..52f69f04d5 --- /dev/null +++ b/lms/djangoapps/certificates/config/waffle.py @@ -0,0 +1,19 @@ +""" +This module contains various configuration settings via +waffle switches for the Certificates app. +""" +from openedx.core.djangoapps.waffle_utils import WaffleSwitchNamespace + +# Namespace +WAFFLE_NAMESPACE = u'certificates' + +# Switches +SELF_PACED_ONLY = u'self_paced_only' +INSTRUCTOR_PACED_ONLY = u'instructor_paced_only' + + +def waffle(): + """ + Returns the namespaced, cached, audited Waffle class for Certificates. + """ + return WaffleSwitchNamespace(name=WAFFLE_NAMESPACE, log_prefix=u'Certificates: ') diff --git a/lms/djangoapps/certificates/tasks.py b/lms/djangoapps/certificates/tasks.py new file mode 100644 index 0000000000..9ccd36bb3e --- /dev/null +++ b/lms/djangoapps/certificates/tasks.py @@ -0,0 +1,26 @@ +from celery import task +from logging import getLogger + +from celery_utils.logged_task import LoggedTask +from celery_utils.persist_on_failure import PersistOnFailureTask + +from .api import generate_user_certificates + +logger = getLogger(__name__) + + +class _BaseCertificateTask(PersistOnFailureTask, LoggedTask): # pylint: disable=abstract-method + """ + Include persistence features, as well as logging of task invocation. + """ + abstract = True + + +@task(base=_BaseCertificateTask) +def generate_certificate(**kwargs): + """ + Generates a certificate for a single user. + """ + student = kwargs.pop('student') + course_key = kwargs.pop('course_key') + generate_user_certificates(student=student, course_key=course_key, **kwargs) diff --git a/lms/djangoapps/certificates/tests/test_tasks.py b/lms/djangoapps/certificates/tests/test_tasks.py new file mode 100644 index 0000000000..9572e279a2 --- /dev/null +++ b/lms/djangoapps/certificates/tests/test_tasks.py @@ -0,0 +1,21 @@ +from unittest import TestCase + +import ddt +from mock import patch + +from lms.djangoapps.certificates.tasks import generate_certificate + + +@ddt.ddt +class GenerateUserCertificateTest(TestCase): + @patch('lms.djangoapps.certificates.tasks.generate_user_certificates') + def test_cert_task(self, generate_user_certs_mock): + generate_certificate(student='a', course_key='b', otherarg='c', otherotherarg='d') + generate_user_certs_mock.assert_called_with(student='a', course_key='b', otherarg='c', otherotherarg='d') + + @ddt.data('student', 'course_key') + def test_cert_task_missing_args(self, missing_arg): + kwargs = {'student': 'a', 'course_key': 'b', 'otherarg': 'c'} + del kwargs[missing_arg] + with self.assertRaisesRegexp(KeyError, missing_arg): + generate_certificate(**kwargs)