From b99aeda62881764f67806948fc3181a69015e456 Mon Sep 17 00:00:00 2001
From: uzairr <uzairr@yahoo.com>
Date: Wed, 22 Jan 2020 18:33:39 +0500
Subject: [PATCH] Add additional check for grade display on course banner.

PROD-1142
---
 common/djangoapps/student/tests/test_views.py | 22 ++++++++++++-------
 common/djangoapps/util/course.py              | 16 ++++++++++----
 .../_dashboard_certificate_information.html   |  7 +++++-
 3 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/common/djangoapps/student/tests/test_views.py b/common/djangoapps/student/tests/test_views.py
index f9da759552c..1e9ecc2360f 100644
--- a/common/djangoapps/student/tests/test_views.py
+++ b/common/djangoapps/student/tests/test_views.py
@@ -222,32 +222,38 @@ class StudentDashboardTests(SharedModuleStoreTestCase, MilestonesTestCaseMixin,
         response = self.client.get(self.path)
         self.assertRedirects(response, reverse('account_settings'))
 
-    def test_grade_doesnt_appears_before_course_end_date(self):
+    def test_grade_appears_before_course_end_date(self):
         """
         Verify that learners are not able to see their final grade before the end
         of course in the learner dashboard
         """
-        self.course = CourseFactory.create(end=self.TOMORROW, emit_signals=True)
+        self.course_key = CourseKey.from_string('course-v1:edX+DemoX+Demo_Course')
+        self.course = CourseOverviewFactory.create(id=self.course_key, end_date=self.TOMORROW,
+                                                   certificate_available_date=self.THREE_YEARS_AGO,
+                                                   lowest_passing_grade=0.3)
         self.course_enrollment = CourseEnrollmentFactory(course_id=self.course.id, user=self.user)
         GeneratedCertificateFactory(status='notpassing', course_id=self.course.id, user=self.user, grade=0.45)
 
         response = self.client.get(reverse('dashboard'))
         # The final grade does not appear before the course has ended
-        self.assertNotContains(response, 'Your final grade:')
-        self.assertNotContains(response, '<span class="grade-value">45%</span>')
+        self.assertContains(response, 'Your final grade:')
+        self.assertContains(response, '<span class="grade-value">45%</span>')
 
-    def test_grade_appears_after_course_has_ended(self):
+    def test_grade_not_appears_before_cert_available_date(self):
         """
         Verify that learners are able to see their final grade of the course in
         the learner dashboard after the course had ended
         """
-        self.course = CourseFactory.create(end=self.THREE_YEARS_AGO, emit_signals=True)
+        self.course_key = CourseKey.from_string('course-v1:edX+DemoX+Demo_Course')
+        self.course = CourseOverviewFactory.create(id=self.course_key, end_date=self.THREE_YEARS_AGO,
+                                                   certificate_available_date=self.TOMORROW,
+                                                   lowest_passing_grade=0.3)
         self.course_enrollment = CourseEnrollmentFactory(course_id=self.course.id, user=self.user)
         GeneratedCertificateFactory(status='notpassing', course_id=self.course.id, user=self.user, grade=0.45)
 
         response = self.client.get(reverse('dashboard'))
-        self.assertContains(response, 'Your final grade:')
-        self.assertContains(response, '<span class="grade-value">45%</span>')
+        self.assertNotContains(response, 'Your final grade:')
+        self.assertNotContains(response, '<span class="grade-value">45%</span>')
 
     @patch.multiple('django.conf.settings', **MOCK_SETTINGS)
     @ddt.data(
diff --git a/common/djangoapps/util/course.py b/common/djangoapps/util/course.py
index a7a491c675b..4ff94b7dd36 100644
--- a/common/djangoapps/util/course.py
+++ b/common/djangoapps/util/course.py
@@ -76,7 +76,15 @@ def has_certificates_enabled(course):
     return course.cert_html_view_enabled
 
 
-def should_display_grade(end_date):
-    if end_date and end_date < now().replace(hour=0, minute=0, second=0, microsecond=0):
-        return True
-    return False
+def should_display_grade(course_overview):
+    """
+    Returns True or False depending upon either certificate available date
+    or course-end-date
+    """
+    course_end_date = course_overview.end_date
+    cert_available_date = course_overview.certificate_available_date
+    current_date = now().replace(hour=0, minute=0, second=0, microsecond=0)
+    if cert_available_date:
+        return cert_available_date < current_date
+
+    return course_end_date and course_end_date < current_date
diff --git a/lms/templates/dashboard/_dashboard_certificate_information.html b/lms/templates/dashboard/_dashboard_certificate_information.html
index 1d8d29cc881..62f4b81bb86 100644
--- a/lms/templates/dashboard/_dashboard_certificate_information.html
+++ b/lms/templates/dashboard/_dashboard_certificate_information.html
@@ -46,9 +46,14 @@ else:
   % else:
     <div class="message message-status ${status_css_class} is-shown">
       <p class="message-copy">
-        % if should_display_grade(course_overview.end):
+        % if should_display_grade(course_overview):
             ${_("Your final grade:")}
             <span class="grade-value">${"{0:.0f}%".format(float(cert_status['grade'])*100)}</span>.
+        % elif course_overview.certificate_available_date:
+          <%
+            cert_available_date = course_overview.certificate_available_date.strftime('%Y-%m-%d')
+          %>
+          ${_("Grades will be finalized on {cert_available_date}".format(cert_available_date=cert_available_date))}
         % endif
         % if cert_status['status'] == 'notpassing':
           % if enrollment.mode != 'audit':
-- 
GitLab