From 19ab698f4c0d8e9676a3799912f0c8ab40de1cfd Mon Sep 17 00:00:00 2001 From: Awais Date: Mon, 27 Apr 2015 15:35:03 +0500 Subject: [PATCH] ECOM-1284 enabling re-generate certs. --- lms/djangoapps/certificates/queue.py | 15 ++++++-- lms/djangoapps/courseware/tests/test_views.py | 35 +++++++++++++++---- lms/djangoapps/courseware/views.py | 6 ++-- lms/static/js/courseware/certificates_api.js | 8 ++--- lms/templates/courseware/progress.html | 15 ++++++-- 5 files changed, 60 insertions(+), 19 deletions(-) diff --git a/lms/djangoapps/certificates/queue.py b/lms/djangoapps/certificates/queue.py index 1867bec28e..a560d83640 100644 --- a/lms/djangoapps/certificates/queue.py +++ b/lms/djangoapps/certificates/queue.py @@ -206,7 +206,8 @@ class XQueueCertInterface(object): status.unavailable, status.deleted, status.error, - status.notpassing + status.notpassing, + status.downloadable ] cert_status = certificate_status_for_student(student, course_id)['status'] @@ -257,13 +258,23 @@ class XQueueCertInterface(object): if forced_grade: grade['grade'] = forced_grade - cert, __ = GeneratedCertificate.objects.get_or_create(user=student, course_id=course_id) + cert, created = GeneratedCertificate.objects.get_or_create(user=student, course_id=course_id) + + if not created: + LOGGER.info( + u"Regenerate certificate for user %s in course %s " + u"with status %s, download_uuid %s, " + u"and download_url %s", + cert.user.id, unicode(cert.course_id), + cert.status, cert.download_uuid, cert.download_url + ) cert.mode = cert_mode cert.user = student cert.grade = grade['percent'] cert.course_id = course_id cert.name = profile_name + cert.download_url = '' # Strip HTML from grade range label grade_contents = grade.get('grade', None) try: diff --git a/lms/djangoapps/courseware/tests/test_views.py b/lms/djangoapps/courseware/tests/test_views.py index 691829b85f..55c2e2888c 100644 --- a/lms/djangoapps/courseware/tests/test_views.py +++ b/lms/djangoapps/courseware/tests/test_views.py @@ -937,21 +937,44 @@ class GenerateUserCertTests(ModuleStoreTestCase): ) resp = self.client.post(self.url) self.assertEqual(resp.status_code, HttpResponseBadRequest.status_code) - self.assertIn("Certificate is already being created.", resp.content) + self.assertIn("Certificate is being created.", resp.content) @patch('courseware.grades.grade', Mock(return_value={'grade': 'Pass', 'percent': 0.75})) + @override_settings(CERT_QUEUE='certificates', SEGMENT_IO_LMS_KEY="foobar", FEATURES={'SEGMENT_IO_LMS': True}) def test_user_with_passing_existing_downloadable_cert(self): - # If user has passing grade but also has existing downloadable cert - # then json will return cert generating message with bad request code + # If user has already downloadable certificate then he can again re-generate the + # the cert. GeneratedCertificateFactory.create( user=self.student, course_id=self.course.id, status=CertificateStatuses.downloadable, mode='verified' ) - resp = self.client.post(self.url) - self.assertEqual(resp.status_code, HttpResponseBadRequest.status_code) - self.assertIn("Certificate has already been created.", resp.content) + + analytics_patcher = patch('courseware.views.analytics') + mock_tracker = analytics_patcher.start() + self.addCleanup(analytics_patcher.stop) + + with patch('capa.xqueue_interface.XQueueInterface.send_to_queue') as mock_send_to_queue: + mock_send_to_queue.return_value = (0, "Successfully queued") + resp = self.client.post(self.url) + self.assertEqual(resp.status_code, 200) + + #Verify Google Analytics event fired after generating certificate + mock_tracker.track.assert_called_once_with( # pylint: disable=no-member + self.student.id, # pylint: disable=no-member + 'edx.bi.user.certificate.generate', + { + 'category': 'certificates', + 'label': unicode(self.course.id) + }, + + context={ + 'Google Analytics': + {'clientId': None} + } + ) + mock_tracker.reset_mock() def test_user_with_non_existing_course(self): # If try to access a course with valid key pattern then it will return diff --git a/lms/djangoapps/courseware/views.py b/lms/djangoapps/courseware/views.py index bfb1f0212f..a0566fbb7d 100644 --- a/lms/djangoapps/courseware/views.py +++ b/lms/djangoapps/courseware/views.py @@ -1348,10 +1348,8 @@ def generate_user_cert(request, course_id): certificate_status = certs_api.certificate_downloadable_status(student, course.id) - if certificate_status["is_downloadable"]: - return HttpResponseBadRequest(_("Certificate has already been created.")) - elif certificate_status["is_generating"]: - return HttpResponseBadRequest(_("Certificate is already being created.")) + if certificate_status["is_generating"]: + return HttpResponseBadRequest(_("Certificate is being created.")) else: # If the certificate is not already in-process or completed, # then create a new certificate generation task. diff --git a/lms/static/js/courseware/certificates_api.js b/lms/static/js/courseware/certificates_api.js index 4027bf05ff..089aee4182 100644 --- a/lms/static/js/courseware/certificates_api.js +++ b/lms/static/js/courseware/certificates_api.js @@ -1,10 +1,10 @@ $(document).ready(function() { 'use strict'; - $("#btn_generate_cert").click(function(e){ + $(".generate_certs").click(function(e){ e.preventDefault(); - var post_url = $("#btn_generate_cert").data("endpoint"); - $('#btn_generate_cert').prop("disabled", true); + var post_url = $(".generate_certs").data("endpoint"); + $(".generate_certs").attr("disabled", true).addClass('is-disabled').attr('aria-disabled', true); $.ajax({ type: "POST", url: post_url, @@ -14,7 +14,7 @@ $(document).ready(function() { }, error: function(jqXHR, textStatus, errorThrown) { $('#errors-info').html(jqXHR.responseText); - $('#btn_generate_cert').prop("disabled", false); + $(".generate_certs").attr("disabled", false).removeClass('is-disabled').attr('aria-disabled', false); } }); }); diff --git a/lms/templates/courseware/progress.html b/lms/templates/courseware/progress.html index e0e279588c..71356892f5 100644 --- a/lms/templates/courseware/progress.html +++ b/lms/templates/courseware/progress.html @@ -1,4 +1,7 @@ -<%! from django.utils.translation import ugettext as _ %> +<%! + from django.utils.translation import ugettext as _ + from django.core.urlresolvers import reverse +%> <%inherit file="/main.html" /> <%namespace name='static' file='/static_content.html'/> @@ -59,10 +62,16 @@ from django.utils.http import urlquote_plus
%if passed: + % if is_downloadable and download_url: + + <% post_url = reverse('generate_user_cert', args=[unicode(course.id)]) %>

${_("Your certificate is available")}

-

${_("You can now download your certificate as a PDF. If you keep working and receive a higher grade, you can request an updated certificate.")}

+

${_( + "You can now download your certificate as a PDF. If you keep working and receive a higher grade,you can request an {link_start} updated certificate {link_end}.").format( + link_start=u"".format(post_url) ,link_end=u"")} +

- +
%endif