Repository navigation
feat: standardize Certificates API, add v2 (FC-0118 #39062) #39316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Faraz32123
wants to merge
4
commits into
master
Choose a base branch
from
feat/apply-fc-0118-adrs-to-certificates
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3ffd1e1
feat: standardize Certificates API, add v2 (FC-0118 #39062)
Faraz32123 39391ab
fix: address review — drop resolve_course_key, match sibling auth tuple
Faraz32123 56fef6e
fix(certificates): correct inactive-user test and v2 query count
Faraz32123 042c7f4
fix(certificates): correct v2 query count with a course mode present
Faraz32123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
46 changes: 46 additions & 0 deletions
46
cms/djangoapps/contentstore/rest_api/v2/certificates_service.py
This file contains hidden or 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,46 @@ | ||
| """Service layer for the v2 course certificate settings endpoint.""" | ||
|
|
||
| import logging | ||
|
|
||
| from openedx_authz.authz import COURSES_MANAGE_CERTIFICATES | ||
| from rest_framework.exceptions import NotFound | ||
|
|
||
| from cms.djangoapps.contentstore.utils import get_certificates_context | ||
| from cms.djangoapps.contentstore.views.permissions import COURSE_NOT_FOUND_MESSAGE | ||
| from openedx.core.djangoapps.authz.constants import LegacyAuthoringPermission | ||
| from openedx.core.djangoapps.authz.decorators import user_has_course_permission | ||
| from xmodule.modulestore.django import modulestore | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def can_manage_certificates(user, course_key): | ||
| """ | ||
| Return whether ``user`` may change the certificate configuration of the course. | ||
|
|
||
| Informational only: it reports a capability and is not an access check. | ||
| """ | ||
| return user_has_course_permission( | ||
| user, | ||
| COURSES_MANAGE_CERTIFICATES.identifier, | ||
| course_key, | ||
| LegacyAuthoringPermission.WRITE, | ||
| ) | ||
|
|
||
|
|
||
| def get_certificate_settings(course_key, user): | ||
| """ | ||
| Return the certificate configuration of the course ``course_key`` as seen by ``user``. | ||
|
|
||
| Raises ``NotFound`` when the course does not exist or cannot be loaded from | ||
| the modulestore. Nothing is written. | ||
| """ | ||
| store = modulestore() | ||
| with store.bulk_operations(course_key): | ||
| course = store.get_course(course_key) | ||
| if course is None: | ||
| log.info("Certificate settings requested for course %s, which is not in the modulestore.", course_key) | ||
| raise NotFound(COURSE_NOT_FOUND_MESSAGE) | ||
| certificate_settings = get_certificates_context(course, user) | ||
| certificate_settings["can_manage"] = can_manage_certificates(user, course_key) | ||
| return certificate_settings |
155 changes: 155 additions & 0 deletions
155
cms/djangoapps/contentstore/rest_api/v2/serializers/certificates.py
This file contains hidden or 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,155 @@ | ||
| """Serializers for a course's certificate settings.""" | ||
|
|
||
| from rest_framework import serializers | ||
|
|
||
|
|
||
| class ConfiguredCertificateSignatorySerializer(serializers.Serializer): | ||
| """ | ||
| A person whose name and signature appear on a configured certificate. | ||
|
|
||
| Courses imported from other sources can hold a signatory missing a field. | ||
| Such a signatory is returned with ``null`` for the missing value rather | ||
| than failing the whole response. | ||
| """ | ||
|
|
||
| id = serializers.IntegerField( | ||
| allow_null=True, | ||
| default=None, | ||
| help_text=( | ||
| "Identifier of the signatory, unique within its certificate. Null when the stored signatory has none." | ||
| ), | ||
| ) | ||
| name = serializers.CharField( | ||
| allow_null=True, | ||
| default=None, | ||
| help_text="Full name of the signatory. Null when the stored signatory has none.", | ||
| ) | ||
| organization = serializers.CharField( | ||
| required=False, | ||
| help_text="Organization the signatory represents. Absent when none is stored.", | ||
| ) | ||
| signature_image_path = serializers.CharField( | ||
| allow_null=True, | ||
| default=None, | ||
| help_text=( | ||
| "Course asset path of the image of the signatory's signature. " | ||
| "Null when the stored signatory has none." | ||
| ), | ||
| ) | ||
| title = serializers.CharField( | ||
| allow_null=True, | ||
| default=None, | ||
| help_text="Job title of the signatory. Null when the stored signatory has none.", | ||
| ) | ||
|
|
||
| class Meta: | ||
| ref_name = "ConfiguredCertificateSignatory" | ||
|
|
||
|
|
||
| class ConfiguredCertificateSerializer(serializers.Serializer): | ||
| """ | ||
| A certificate configured for the course, with its signatories. | ||
|
|
||
| Courses imported from other sources can hold a certificate missing a field. | ||
| Such a certificate is returned with ``null`` for the missing value, ``false`` | ||
| for a missing active flag and an empty signatory list, rather than failing | ||
| the whole response. | ||
| """ | ||
|
|
||
| course_title = serializers.CharField( | ||
| required=False, | ||
| help_text="Course title printed on the certificate. Absent when none is stored.", | ||
| ) | ||
| description = serializers.CharField( | ||
| allow_null=True, | ||
| default=None, | ||
| help_text="Description of the certificate. Null when the stored certificate has none.", | ||
| ) | ||
| editing = serializers.BooleanField( | ||
| required=False, | ||
| help_text="Whether the certificate was left open for editing in Studio. Absent when none is stored.", | ||
| ) | ||
| id = serializers.IntegerField( | ||
| allow_null=True, | ||
| default=None, | ||
| help_text="Identifier of the certificate, unique within its course. Null when the stored certificate has none.", | ||
| ) | ||
| is_active = serializers.BooleanField( | ||
| default=False, | ||
| help_text="Whether the certificate is active for the course. False when the stored certificate has no flag.", | ||
| ) | ||
| name = serializers.CharField( | ||
| allow_null=True, | ||
| default=None, | ||
| help_text="Name of the certificate. Null when the stored certificate has none.", | ||
| ) | ||
| signatories = ConfiguredCertificateSignatorySerializer( | ||
| many=True, | ||
| allow_null=True, | ||
| default=list, | ||
| help_text="People who sign the certificate, in display order. Empty when the stored certificate has none.", | ||
| ) | ||
| version = serializers.IntegerField( | ||
| allow_null=True, | ||
| default=None, | ||
| help_text="Version of the certificate's stored schema. Null when the stored certificate has none.", | ||
| ) | ||
|
|
||
| class Meta: | ||
| ref_name = "ConfiguredCertificate" | ||
|
|
||
|
|
||
| class CertificateSettingsSerializer(serializers.Serializer): | ||
| """The certificate configuration of one course and the context Studio needs to edit it.""" | ||
|
|
||
| certificate_activation_handler_url = serializers.CharField( | ||
| help_text="Studio address that activates or deactivates the course's certificates.", | ||
| ) | ||
| certificate_web_view_url = serializers.CharField( | ||
| allow_null=True, | ||
| help_text=( | ||
| "Address of the course's certificate preview in the learning site. " | ||
| "Null when the course has no mode that awards a certificate." | ||
| ), | ||
| ) | ||
| certificates = ConfiguredCertificateSerializer( | ||
| many=True, | ||
| allow_null=True, | ||
| help_text="Certificates configured for the course, in stored order.", | ||
| ) | ||
| course_modes = serializers.ListField( | ||
| child=serializers.CharField(), | ||
| help_text="Slugs of the course's enrollment modes that can award a certificate. The audit mode is excluded.", | ||
| ) | ||
| has_certificate_modes = serializers.BooleanField( | ||
| help_text="Whether the course has at least one enrollment mode that can award a certificate.", | ||
| ) | ||
| is_active = serializers.BooleanField(help_text="Whether the course's certificates are active.") | ||
| is_global_staff = serializers.BooleanField(help_text="Whether the requester is a global staff member.") | ||
| mfe_proctored_exam_settings_url = serializers.CharField( | ||
| required=False, | ||
| allow_null=True, | ||
| allow_blank=True, | ||
| help_text="Address of the proctored exam settings page. Empty or null when that page is not available.", | ||
| ) | ||
| course_number = serializers.CharField( | ||
| source="context_course.number", | ||
| help_text="Number of the course.", | ||
| ) | ||
| course_title = serializers.CharField( | ||
| source="context_course.display_name_with_default", | ||
| help_text="Display name of the course.", | ||
| ) | ||
| course_number_override = serializers.CharField( | ||
| source="context_course.display_coursenumber", | ||
| allow_null=True, | ||
| help_text="Course number shown in place of the course's own number. Null when none is set.", | ||
| ) | ||
| can_manage = serializers.BooleanField( | ||
| default=False, | ||
| read_only=True, | ||
| help_text="Whether the requester may change the course's certificate configuration.", | ||
| ) | ||
|
|
||
| class Meta: | ||
| ref_name = "CertificateSettings" |
74 changes: 74 additions & 0 deletions
74
cms/djangoapps/contentstore/rest_api/v2/views/certificates.py
This file contains hidden or 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,74 @@ | ||
| """API views for a course's certificate settings.""" | ||
|
|
||
| from drf_spectacular.utils import OpenApiResponse, extend_schema | ||
| from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication | ||
| from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser | ||
| from edx_rest_framework_extensions.errors import ErrorResponseSerializer | ||
| from edx_rest_framework_extensions.mixins import StandardizedErrorMixin | ||
| from rest_framework import viewsets | ||
| from rest_framework.permissions import IsAuthenticated | ||
| from rest_framework.response import Response | ||
|
|
||
| from cms.djangoapps.contentstore.rest_api.v2.certificates_service import get_certificate_settings | ||
| from cms.djangoapps.contentstore.rest_api.v2.serializers.certificates import CertificateSettingsSerializer | ||
| from cms.djangoapps.contentstore.views.permissions import CanViewCertificates | ||
|
|
||
|
|
||
| @extend_schema(tags=["openedx-platform-sdk"]) | ||
| class CourseCertificateSettingsViewSet(StandardizedErrorMixin, viewsets.ViewSet): | ||
| """ | ||
| The certificate configuration of one course. | ||
|
|
||
| Returns the certificates configured for the course together with the | ||
| context Studio needs to present them: the course's certificate-awarding | ||
| modes, whether the certificates are active, and whether the requester may | ||
| manage them. | ||
|
|
||
| Callable by anyone who may view the course's certificate configuration. | ||
| Permission is checked before the course is looked up, so a caller without | ||
| access is refused whether or not the course exists. | ||
|
|
||
| A plain ``ViewSet``: the data comes from the modulestore, not from database | ||
| rows. | ||
| """ | ||
|
|
||
| # The legacy decorator allows inactive (unverified-email) users; the v2 | ||
| # siblings (textbooks, video settings) keep that, so certificates does too. | ||
| authentication_classes = (JwtAuthentication, SessionAuthenticationAllowInactiveUser) | ||
| permission_classes = (IsAuthenticated, CanViewCertificates) | ||
| serializer_class = CertificateSettingsSerializer | ||
|
|
||
| def get_serializer(self, *args, **kwargs): | ||
| """Instantiate the settings serializer declared on this viewset.""" | ||
| return self.serializer_class(*args, **kwargs) | ||
|
|
||
| @extend_schema( | ||
| summary="Retrieve a course's certificate settings", | ||
| description=( | ||
| "Returns the certificates configured for the course, their signatories, the enrollment " | ||
| "modes that can award a certificate, whether the certificates are active, and whether " | ||
| "the requester may manage them." | ||
| ), | ||
| responses={ | ||
| 200: OpenApiResponse( | ||
| response=CertificateSettingsSerializer, | ||
| description="The course's certificate settings.", | ||
| ), | ||
| 401: OpenApiResponse( | ||
| response=ErrorResponseSerializer, | ||
| description="The requester is not authenticated.", | ||
| ), | ||
| 403: OpenApiResponse( | ||
| response=ErrorResponseSerializer, | ||
| description="The requester may not view this course's certificate settings.", | ||
| ), | ||
| 404: OpenApiResponse( | ||
| response=ErrorResponseSerializer, | ||
| description="The course does not exist.", | ||
| ), | ||
| }, | ||
| ) | ||
| def retrieve(self, request, course_key): | ||
| """Return the course's certificate settings.""" | ||
| certificate_settings = get_certificate_settings(course_key, request.user) | ||
| return Response(self.get_serializer(certificate_settings).data) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.