From 4f977fb2283b1563d0ed0a518666f632a3dc537a Mon Sep 17 00:00:00 2001 From: Sanford Student Date: Thu, 7 Sep 2017 12:02:43 -0400 Subject: [PATCH] common function for cert display date --- .../certificates/tests/test_webview_views.py | 5 ++++- lms/djangoapps/certificates/views/webview.py | 11 ++--------- openedx/core/djangoapps/certificates/api.py | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lms/djangoapps/certificates/tests/test_webview_views.py b/lms/djangoapps/certificates/tests/test_webview_views.py index fc335794cc..8de0f4fb59 100644 --- a/lms/djangoapps/certificates/tests/test_webview_views.py +++ b/lms/djangoapps/certificates/tests/test_webview_views.py @@ -39,6 +39,7 @@ from lms.djangoapps.badges.tests.factories import ( CourseCompleteImageConfigurationFactory ) from lms.djangoapps.grades.tests.utils import mock_passing_grade +from openedx.core.djangoapps.certificates.config import waffle from openedx.core.lib.tests.assertions.events import assert_event_matches from student.roles import CourseStaffRole from student.tests.factories import CourseEnrollmentFactory, UserFactory @@ -819,7 +820,9 @@ class CertificatesViewsTests(CommonCertificatesTestCase): expected_date = datetime.datetime.today() else: expected_date = self.course.certificate_available_date - response = self.client.get(test_url) + with waffle.waffle().override(waffle.SELF_PACED_ONLY, active=True): + with waffle.waffle().override(waffle.INSTRUCTOR_PACED_ONLY, active=True): + response = self.client.get(test_url) date = '{month} {day}, {year}'.format( month=strftime_localized(expected_date, "%B"), day=expected_date.day, diff --git a/lms/djangoapps/certificates/views/webview.py b/lms/djangoapps/certificates/views/webview.py index 88a37ff783..910c9606df 100644 --- a/lms/djangoapps/certificates/views/webview.py +++ b/lms/djangoapps/certificates/views/webview.py @@ -41,6 +41,7 @@ from edxmako.shortcuts import render_to_response from edxmako.template import Template from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers from openedx.core.lib.courses import course_image_url +from openedx.core.djangoapps.certificates.api import display_date_for_certificate from student.models import LinkedInAddToProfileConfiguration from util import organizations_helpers as organization_api from util.date_utils import strftime_localized @@ -100,15 +101,7 @@ def _update_certificate_context(context, user_certificate, platform_name): # Translators: The format of the date includes the full name of the month course = get_course_by_id(user_certificate.course_id) if user_certificate.course_id else None - if ( - course and - not course.self_paced and - course.certificate_available_date and - course.certificate_available_date < datetime.now(pytz.UTC) - ): - date = course.certificate_available_date - else: - date = user_certificate.modified_date + date = display_date_for_certificate(course, user_certificate) context['certificate_date_issued'] = _('{month} {day}, {year}').format( month=strftime_localized(date, "%B"), day=date.day, diff --git a/openedx/core/djangoapps/certificates/api.py b/openedx/core/djangoapps/certificates/api.py index b981046e54..21b015c03d 100644 --- a/openedx/core/djangoapps/certificates/api.py +++ b/openedx/core/djangoapps/certificates/api.py @@ -1,7 +1,8 @@ """ The public API for certificates. """ - +from datetime import datetime +from pytz import UTC from openedx.core.djangoapps.certificates.config import waffle @@ -37,3 +38,15 @@ def _enabled_and_self_paced(course): def can_show_certificate_available_date_field(course): return _enabled_and_self_paced(course) + + +def display_date_for_certificate(course, certificate): + if ( + auto_certificate_generation_enabled_for_course(course) and + not course.self_paced and + course.certificate_available_date and + course.certificate_available_date < datetime.now(UTC) + ): + return course.certificate_available_date + + return certificate.modified_date