def test_no_certificates_for_user(self): """ Test the case when there are no certificates for a user. """ self.assertEqual( certs_api.get_certificates_for_user(self.student_no_cert.username), [])
def test_no_certificates_for_user(self): """ Test the case when there are no certificates for a user. """ self.assertEqual( certs_api.get_certificates_for_user(self.student_no_cert.username), [] )
def search_by_user(request): """ Search for certificates for a particular user. Supports search by either username or email address. Arguments: request (HttpRequest): The request object. Returns: JsonResponse Example Usage: GET /certificates/[email protected] Response: 200 OK Content-Type: application/json [ { "username": "******", "course_key": "edX/DemoX/Demo_Course", "type": "verified", "status": "downloadable", "download_url": "http://www.example.com/cert.pdf", "grade": "0.98", "created": 2015-07-31T00:00:00Z, "modified": 2015-07-31T00:00:00Z } ] """ query = request.GET.get("query") if not query: return JsonResponse([]) try: user = User.objects.get(Q(email=query) | Q(username=query)) except User.DoesNotExist: return JsonResponse([]) certificates = api.get_certificates_for_user(user.username) for cert in certificates: cert["course_key"] = unicode(cert["course_key"]) cert["created"] = cert["created"].isoformat() cert["modified"] = cert["modified"].isoformat() return JsonResponse(certificates)
def test_get_certificates_for_user(self): """ Test to get all the certificates for a user """ certs = certs_api.get_certificates_for_user(self.student.username) self.assertEqual(len(certs), 2) self.assertEqual(certs[0]['username'], self.student.username) self.assertEqual(certs[1]['username'], self.student.username) self.assertEqual(certs[0]['course_key'], self.course_1.id) self.assertEqual(certs[1]['course_key'], self.course_2.id) self.assertEqual(certs[0]['type'], CourseMode.VERIFIED) self.assertEqual(certs[1]['type'], CourseMode.HONOR) self.assertEqual(certs[0]['status'], CertificateStatuses.downloadable) self.assertEqual(certs[1]['status'], CertificateStatuses.downloadable) self.assertEqual(certs[0]['grade'], '0.88') self.assertEqual(certs[1]['grade'], '0.99') self.assertEqual(certs[0]['download_url'], 'www.google.com') self.assertEqual(certs[1]['download_url'], 'www.gmail.com')
def search_certificates(request): """ Search for certificates for a particular user OR along with the given course. Supports search by either username or email address along with course id. First filter the records for the given username/email and then filter against the given course id (if given). Show the 'Regenerate' button if a record found in 'generatedcertificate' model otherwise it will show the Generate button. Arguments: request (HttpRequest): The request object. Returns: JsonResponse Example Usage: GET /certificates/[email protected] GET /certificates/[email protected]&course_id=xyz Response: 200 OK Content-Type: application/json [ { "username": "******", "course_key": "edX/DemoX/Demo_Course", "type": "verified", "status": "downloadable", "download_url": "http://www.example.com/cert.pdf", "grade": "0.98", "created": 2015-07-31T00:00:00Z, "modified": 2015-07-31T00:00:00Z } ] """ user_filter = request.GET.get("user", "") if not user_filter: msg = _("user is not given.") return HttpResponseBadRequest(msg) try: user = User.objects.get(Q(email=user_filter) | Q(username=user_filter)) except User.DoesNotExist: return HttpResponseBadRequest(_("user '{user}' does not exist").format(user=user_filter)) certificates = api.get_certificates_for_user(user.username) for cert in certificates: cert["course_key"] = unicode(cert["course_key"]) cert["created"] = cert["created"].isoformat() cert["modified"] = cert["modified"].isoformat() cert["regenerate"] = True course_id = urllib.quote_plus(request.GET.get("course_id", ""), safe=':/') if course_id: try: course_key = CourseKey.from_string(course_id) except InvalidKeyError: return HttpResponseBadRequest(_("Course id '{course_id}' is not valid").format(course_id=course_id)) else: try: if CourseOverview.get_from_id(course_key): certificates = [certificate for certificate in certificates if certificate['course_key'] == course_id] if not certificates: return JsonResponse([{'username': user.username, 'course_key': course_id, 'regenerate': False}]) except CourseOverview.DoesNotExist: msg = _("The course does not exist against the given key '{course_key}'").format(course_key=course_key) return HttpResponseBadRequest(msg) return JsonResponse(certificates)