forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [AXIMST-148] API for certificates DRF (#2489)
- Loading branch information
1 parent
990a6f1
commit d0d737f
Showing
8 changed files
with
249 additions
and
41 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
cms/djangoapps/contentstore/rest_api/v1/serializers/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
cms/djangoapps/contentstore/rest_api/v1/serializers/certificates.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
API Serializers for certificates page | ||
""" | ||
|
||
from rest_framework import serializers | ||
|
||
|
||
class CertificateSignatorySerializer(serializers.Serializer): | ||
""" | ||
Serializer for representing certificate's signatory. | ||
""" | ||
|
||
id = serializers.IntegerField() | ||
name = serializers.CharField() | ||
organization = serializers.CharField(required=False) | ||
signature_image_path = serializers.CharField() | ||
title = serializers.CharField() | ||
|
||
|
||
class CertificateItemSerializer(serializers.Serializer): | ||
""" | ||
Serializer for representing certificate item created for current course. | ||
""" | ||
|
||
course_title = serializers.CharField(required=False) | ||
description = serializers.CharField() | ||
editing = serializers.BooleanField(required=False) | ||
id = serializers.IntegerField() | ||
is_active = serializers.BooleanField() | ||
name = serializers.CharField() | ||
signatories = CertificateSignatorySerializer(many=True) | ||
version = serializers.IntegerField() | ||
|
||
|
||
class CourseCertificatesSerializer(serializers.Serializer): | ||
""" | ||
Serializer for representing course's certificates. | ||
""" | ||
|
||
certificate_activation_handler_url = serializers.CharField() | ||
certificate_web_view_url = serializers.CharField(allow_null=True) | ||
certificates = CertificateItemSerializer(many=True, allow_null=True) | ||
course_modes = serializers.ListField(child=serializers.CharField()) | ||
has_certificate_modes = serializers.BooleanField() | ||
is_active = serializers.BooleanField() | ||
is_global_staff = serializers.BooleanField() | ||
mfe_proctored_exam_settings_url = serializers.CharField( | ||
required=False, allow_null=True, allow_blank=True | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
cms/djangoapps/contentstore/rest_api/v1/views/certificates.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
""" API Views for course certificates """ | ||
|
||
import edx_api_doc_tools as apidocs | ||
from opaque_keys.edx.keys import CourseKey | ||
from rest_framework.request import Request | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from cms.djangoapps.contentstore.utils import get_certificates_context | ||
from cms.djangoapps.contentstore.rest_api.v1.serializers import ( | ||
CourseCertificatesSerializer, | ||
) | ||
from common.djangoapps.student.auth import has_studio_write_access | ||
from openedx.core.lib.api.view_utils import ( | ||
DeveloperErrorViewMixin, | ||
verify_course_exists, | ||
view_auth_classes, | ||
) | ||
from xmodule.modulestore.django import modulestore | ||
|
||
|
||
@view_auth_classes(is_authenticated=True) | ||
class CourseCertificatesView(DeveloperErrorViewMixin, APIView): | ||
""" | ||
View for course certificate page. | ||
""" | ||
|
||
@apidocs.schema( | ||
parameters=[ | ||
apidocs.string_parameter( | ||
"course_id", apidocs.ParameterLocation.PATH, description="Course ID" | ||
), | ||
], | ||
responses={ | ||
200: CourseCertificatesSerializer, | ||
401: "The requester is not authenticated.", | ||
403: "The requester cannot access the specified course.", | ||
404: "The requested course does not exist.", | ||
}, | ||
) | ||
@verify_course_exists() | ||
def get(self, request: Request, course_id: str): | ||
""" | ||
Get an object containing course's certificates. | ||
**Example Request** | ||
GET /api/contentstore/v1/certificates/{course_id} | ||
**Response Values** | ||
If the request is successful, an HTTP 200 "OK" response is returned. | ||
The HTTP 200 response contains a single dict that contains keys that | ||
are the course's certificates. | ||
**Example Response** | ||
```json | ||
{ | ||
"certificate_activation_handler_url": "/certificates/activation/course-v1:org+101+101/", | ||
"certificate_web_view_url": "///certificates/course/course-v1:org+101+101?preview=honor", | ||
"certificates": [ | ||
{ | ||
"course_title": "Course title", | ||
"description": "Description of the certificate", | ||
"editing": false, | ||
"id": 1622146085, | ||
"is_active": false, | ||
"name": "Name of the certificate", | ||
"signatories": [ | ||
{ | ||
"id": 268550145, | ||
"name": "name_sign", | ||
"organization": "org", | ||
"signature_image_path": "/asset-v1:org+101+101+type@[email protected]", | ||
"title": "title_sign" | ||
} | ||
], | ||
"version": 1 | ||
} | ||
], | ||
"course_modes": [ | ||
"honor" | ||
], | ||
"has_certificate_modes": true, | ||
"is_active": false, | ||
"is_global_staff": true, | ||
"mfe_proctored_exam_settings_url": "" | ||
} | ||
``` | ||
""" | ||
course_key = CourseKey.from_string(course_id) | ||
store = modulestore() | ||
|
||
if not has_studio_write_access(request.user, course_key): | ||
self.permission_denied(request) | ||
|
||
with store.bulk_operations(course_key): | ||
course = modulestore().get_course(course_key) | ||
certificates_context = get_certificates_context(course, request.user) | ||
serializer = CourseCertificatesSerializer(certificates_context) | ||
return Response(serializer.data) |
34 changes: 34 additions & 0 deletions
34
cms/djangoapps/contentstore/rest_api/v1/views/tests/test_certificates.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
Unit tests for the course's certificate. | ||
""" | ||
from django.urls import reverse | ||
from rest_framework import status | ||
|
||
from cms.djangoapps.contentstore.tests.utils import CourseTestCase | ||
from cms.djangoapps.contentstore.views.tests.test_certificates import HelperMethods | ||
|
||
from ...mixins import PermissionAccessMixin | ||
|
||
|
||
class CourseCertificatesViewTest(CourseTestCase, PermissionAccessMixin, HelperMethods): | ||
""" | ||
Tests for CourseCertificatesView. | ||
""" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.url = reverse( | ||
"cms.djangoapps.contentstore:v1:certificates", | ||
kwargs={"course_id": self.course.id}, | ||
) | ||
|
||
def test_success_response(self): | ||
""" | ||
Check that endpoint is valid and success response. | ||
""" | ||
self._add_course_certificates(count=2, signatory_count=2) | ||
response = self.client.get(self.url) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data["certificates"]), 2) | ||
self.assertEqual(len(response.data["certificates"][0]["signatories"]), 2) | ||
self.assertEqual(len(response.data["certificates"][1]["signatories"]), 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters