diff --git a/lms/djangoapps/certificates/api.py b/lms/djangoapps/certificates/api.py index 181d808b46..7b66138a43 100644 --- a/lms/djangoapps/certificates/api.py +++ b/lms/djangoapps/certificates/api.py @@ -14,6 +14,7 @@ from opaque_keys.edx.keys import CourseKey from openedx.core.djangoapps.content.course_overviews.models import CourseOverview from xmodule.modulestore.django import modulestore +from xmodule_django.models import CourseKeyField from util.organizations_helpers import get_course_organizations from certificates.models import ( @@ -396,6 +397,7 @@ def get_certificate_template(course_key, mode): if not template and org_id and mode: template = CertificateTemplate.objects.filter( organization_id=org_id, + course_key=CourseKeyField.Empty, mode=mode, is_active=True ) @@ -403,11 +405,15 @@ def get_certificate_template(course_key, mode): if not template and org_id: template = CertificateTemplate.objects.filter( organization_id=org_id, + course_key=CourseKeyField.Empty, + mode=None, is_active=True ) # if we still don't template find by only course mode if not template and mode: template = CertificateTemplate.objects.filter( + organization_id=None, + course_key=CourseKeyField.Empty, mode=mode, is_active=True ) diff --git a/lms/djangoapps/certificates/tests/test_webview_views.py b/lms/djangoapps/certificates/tests/test_webview_views.py index 417e5126bb..bceab16b0e 100644 --- a/lms/djangoapps/certificates/tests/test_webview_views.py +++ b/lms/djangoapps/certificates/tests/test_webview_views.py @@ -460,10 +460,11 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase): def test_certificate_custom_template_with_org_mode_course(self): """ Tests custom template search and rendering. + This test should check template matching when org={org}, course={course}, mode={mode}. """ self._add_course_certificates(count=1, signatory_count=2) - self._create_custom_template(1, mode='honor', course_key=unicode(self.course.id)) - self._create_custom_template(2, mode='honor') + self._create_custom_template(org_id=1, mode='honor', course_key=unicode(self.course.id)) + self._create_custom_template(org_id=2, mode='honor') test_url = get_certificate_url( user_id=self.user.id, course_id=unicode(self.course.id) @@ -487,11 +488,17 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase): @override_settings(FEATURES=FEATURES_WITH_CUSTOM_CERTS_ENABLED) def test_certificate_custom_template_with_org(self): """ - Tests custom template search if if have a single template for all courses of organization. + Tests custom template search if we have a single template for organization and mode + with course set to Null. + This test should check template matching when org={org}, course=Null, mode={mode}. """ + course = CourseFactory.create( + org='cstX', number='cst_22', display_name='custom template course' + ) + self._add_course_certificates(count=1, signatory_count=2) - self._create_custom_template(1) - self._create_custom_template(1, mode='honor') + self._create_custom_template(org_id=1, mode='honor') + self._create_custom_template(org_id=1, mode='honor', course_key=course.id) test_url = get_certificate_url( user_id=self.user.id, course_id=unicode(self.course.id) @@ -505,10 +512,33 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase): self.assertEqual(response.status_code, 200) self.assertContains(response, 'course name: {}'.format(self.course.display_name)) + @override_settings(FEATURES=FEATURES_WITH_CUSTOM_CERTS_ENABLED) + def test_certificate_custom_template_with_organization(self): + """ + Tests custom template search when we have a single template for a organization. + This test should check template matching when org={org}, course=Null, mode=null. + """ + self._add_course_certificates(count=1, signatory_count=2) + self._create_custom_template(org_id=1, mode='honor') + self._create_custom_template(org_id=1, mode='honor', course_key=self.course.id) + self._create_custom_template(org_id=2) + test_url = get_certificate_url( + user_id=self.user.id, + course_id=unicode(self.course.id) + ) + + with patch('certificates.api.get_course_organizations') as mock_get_orgs: + mock_get_orgs.side_effect = [ + [{"id": 2, "name": "organization name 2"}], + ] + response = self.client.get(test_url) + self.assertEqual(response.status_code, 200) + @override_settings(FEATURES=FEATURES_WITH_CUSTOM_CERTS_ENABLED) def test_certificate_custom_template_with_course_mode(self): """ - Tests custom template search if if have a single template for a course mode. + Tests custom template search if we have a single template for a course mode. + This test should check template matching when org=null, course=Null, mode={mode}. """ mode = 'honor' self._add_course_certificates(count=1, signatory_count=2)