def _create_cert_pdf(self, student, request, course): cert, created = GeneratedCertificate.objects.get_or_create( course_id=self.course_id, user=student) grade = grades.grade(cert.user, request, course) print "User {0}: Grade {1}% - {2}".format( cert.user, grade['percent'] * 100, grade['grade']), profile = UserProfile.objects.get(user=student) cert.grade = grade['percent'] cert.mode = 'honor' cert.user = student cert.course_id = self.course_id cert.name = profile.name is_whitelisted = CertificateWhitelist.objects.filter( user=student, course_id=self.course_id, whitelist=True ).exists() if is_whitelisted or grade['grade']: if profile.allow_certificate is False: new_status = CertificateStatuses.restricted cert.status = new_status print ": Status {0}".format(new_status) if not self.noop: cert.save() else: key = self._make_hashkey(self.course_id.to_deprecated_string() + student.username) cert.key = key if not self.noop: response_json = create_cert_pdf( student.username, self.course_id, cert.key, cert.name, course.display_name, grade['percent'], self.file_prefix) response = json.loads(response_json) self._dprint(": Response = {}".format(response), newline=False) cert.download_url = response.get(u'download_url', False) msg = response.get(u'error', False) if msg is False: new_status = CertificateStatuses.generating cert.status = new_status cert.save() print ": Status {0}".format(new_status) else: new_status = CertificateStatuses.error cert.status = new_status cert.save() print ": Status {0}".format(new_status), print ": Error {}".format(msg) else: print ": Status {0} (Noop)".format(cert.status) else: new_status = CertificateStatuses.notpassing cert.status = new_status print ": Status {0}".format(new_status) if not self.noop: cert.save()
def test_create_cert_pdf(self): with patch('pdfgen.views.CertificateHonor.create', spec=True, return_value=self.result) as moc1: contents = create_cert_pdf(self.username, self.course_id, self.key, self.display_name, self.course_name, self.grade, self.file_prefix) self.assertEqual(contents, self.result) moc1.assert_called_once_with()
def test_create_cert_pdf_raise_BotoClientError(self): botoexception = BotoClientError(reason="reason") with patch('pdfgen.views.CertificateHonor.create', spec=True, side_effect=botoexception) as moc1: contents = create_cert_pdf(self.username, self.course_id, self.key, self.display_name, self.course_name, self.grade, self.file_prefix) self.assertEqual( contents, json.dumps({"error": "BotoClientError: reason"})) moc1.assert_called_once_with()
def test_create_cert_pdf_raise_BotoClientError(self): botoexception = BotoClientError(reason="reason") with patch('pdfgen.views.CertificateHonor.create', spec=True, side_effect=botoexception) as moc1: contents = create_cert_pdf(self.username, self.course_id, self.key, self.display_name, self.course_name, self.grade, self.file_prefix) self.assertEqual(contents, json.dumps({"error": "BotoClientError: reason"})) moc1.assert_called_once_with()
def _create_cert_pdf(self, student, request, course): cert, created = GeneratedCertificate.objects.get_or_create( course_id=self.course_id, user=student) grade = grades.grade(cert.user, request, course) print "User {0}: Grade {1}% - {2}".format(cert.user, grade['percent'] * 100, grade['grade']), profile = UserProfile.objects.get(user=student) cert.grade = grade['percent'] cert.mode = 'honor' cert.user = student cert.course_id = self.course_id cert.name = profile.name is_whitelisted = CertificateWhitelist.objects.filter( user=student, course_id=self.course_id, whitelist=True).exists() if is_whitelisted or grade['grade']: if profile.allow_certificate is False: new_status = CertificateStatuses.restricted cert.status = new_status print ": Status {0}".format(new_status) if not self.noop: cert.save() else: key = self._make_hashkey( self.course_id.to_deprecated_string() + student.username) cert.key = key if not self.noop: response_json = create_cert_pdf(student.username, self.course_id, cert.key, cert.name, course.display_name, grade['percent'], self.file_prefix) response = json.loads(response_json) self._dprint(": Response = {}".format(response), newline=False) cert.download_url = response.get(u'download_url', False) msg = response.get(u'error', False) if msg is False: new_status = CertificateStatuses.generating cert.status = new_status cert.save() print ": Status {0}".format(new_status) else: new_status = CertificateStatuses.error cert.status = new_status cert.save() print ": Status {0}".format(new_status), print ": Error {}".format(msg) else: print ": Status {0} (Noop)".format(cert.status) else: new_status = CertificateStatuses.notpassing cert.status = new_status print ": Status {0}".format(new_status) if not self.noop: cert.save()