Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cms/djangoapps/contentstore/rest_api/v2/authoring_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.urls import path

from cms.djangoapps.contentstore.rest_api.v2.views.certificates import CourseCertificateSettingsViewSet
from cms.djangoapps.contentstore.rest_api.v2.views.textbooks import CourseTextbooksViewSet
from cms.djangoapps.contentstore.rest_api.v2.views.video_settings import CourseVideoSettingsViewSet

Expand All @@ -18,4 +19,9 @@
CourseVideoSettingsViewSet.as_view({"get": "retrieve"}),
name="course_video_settings",
),
path(
"courses/<course_key:course_key>/certificate_settings/",
CourseCertificateSettingsViewSet.as_view({"get": "retrieve"}),
name="course_certificate_settings",
),
]
46 changes: 46 additions & 0 deletions cms/djangoapps/contentstore/rest_api/v2/certificates_service.py
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 cms/djangoapps/contentstore/rest_api/v2/serializers/certificates.py
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 cms/djangoapps/contentstore/rest_api/v2/views/certificates.py
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)
Comment thread
Faraz32123 marked this conversation as resolved.
Resolved
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)
Loading
Loading