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-111] create API for course textbooks (#2491)
* feat: [AXIMST-111] create API for course textbooks * fix: add tabulation
- Loading branch information
1 parent
ced11cd
commit 805504e
Showing
9 changed files
with
194 additions
and
10 deletions.
There are no files selected for viewing
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
32 changes: 32 additions & 0 deletions
32
cms/djangoapps/contentstore/rest_api/v1/serializers/textbooks.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,32 @@ | ||
""" | ||
API Serializers for textbooks page | ||
""" | ||
|
||
from rest_framework import serializers | ||
|
||
|
||
class CourseTextbookChapterSerializer(serializers.Serializer): | ||
""" | ||
Serializer for representing textbook chapter. | ||
""" | ||
|
||
title = serializers.CharField() | ||
url = serializers.CharField() | ||
|
||
|
||
class CourseTextbookItemSerializer(serializers.Serializer): | ||
""" | ||
Serializer for representing textbook item. | ||
""" | ||
|
||
id = serializers.CharField() | ||
chapters = CourseTextbookChapterSerializer(many=True) | ||
tab_title = serializers.CharField() | ||
|
||
|
||
class CourseTextbooksSerializer(serializers.Serializer): | ||
""" | ||
Serializer for representing course's textbooks. | ||
""" | ||
|
||
textbooks = serializers.ListField() |
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
43 changes: 43 additions & 0 deletions
43
cms/djangoapps/contentstore/rest_api/v1/views/tests/test_textbooks.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,43 @@ | ||
""" | ||
Unit tests for the course's textbooks. | ||
""" | ||
from django.urls import reverse | ||
from rest_framework import status | ||
|
||
from cms.djangoapps.contentstore.tests.utils import CourseTestCase | ||
|
||
from ...mixins import PermissionAccessMixin | ||
|
||
|
||
class CourseTextbooksViewTest(CourseTestCase, PermissionAccessMixin): | ||
""" | ||
Tests for CourseTextbooksView. | ||
""" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.url = reverse( | ||
"cms.djangoapps.contentstore:v1:textbooks", | ||
kwargs={"course_id": self.course.id}, | ||
) | ||
|
||
def test_success_response(self): | ||
""" | ||
Check that endpoint is valid and success response. | ||
""" | ||
expected_textbook = [ | ||
{ | ||
"tab_title": "Textbook Name", | ||
"chapters": [ | ||
{"title": "Chapter 1", "url": "/static/book.pdf"}, | ||
{"title": "Chapter 2", "url": "/static/story.pdf"}, | ||
], | ||
"id": "Textbook_Name", | ||
} | ||
] | ||
self.course.pdf_textbooks = expected_textbook | ||
self.save_course() | ||
|
||
response = self.client.get(self.url) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data["textbooks"], expected_textbook) |
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
90 changes: 90 additions & 0 deletions
90
cms/djangoapps/contentstore/rest_api/v1/views/textbooks.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,90 @@ | ||
""" API Views for course textbooks """ | ||
|
||
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_textbooks_context | ||
from cms.djangoapps.contentstore.rest_api.v1.serializers import ( | ||
CourseTextbooksSerializer, | ||
) | ||
from common.djangoapps.student.auth import has_studio_read_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 CourseTextbooksView(DeveloperErrorViewMixin, APIView): | ||
""" | ||
View for course textbooks page. | ||
""" | ||
|
||
@apidocs.schema( | ||
parameters=[ | ||
apidocs.string_parameter( | ||
"course_id", apidocs.ParameterLocation.PATH, description="Course ID" | ||
), | ||
], | ||
responses={ | ||
200: CourseTextbooksSerializer, | ||
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 textbooks. | ||
**Example Request** | ||
GET /api/contentstore/v1/textbooks/{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 textbooks. | ||
**Example Response** | ||
```json | ||
{ | ||
"textbooks": [ | ||
{ | ||
"tab_title": "Textbook Name", | ||
"chapters": [ | ||
{ | ||
"title": "Chapter 1", | ||
"url": "/static/Present_Perfect.pdf" | ||
}, | ||
{ | ||
"title": "Chapter 2", | ||
"url": "/static/Lear.pdf" | ||
} | ||
], | ||
"id": "Textbook_Name" | ||
} | ||
] | ||
} | ||
``` | ||
""" | ||
course_key = CourseKey.from_string(course_id) | ||
store = modulestore() | ||
|
||
if not has_studio_read_access(request.user, course_key): | ||
self.permission_denied(request) | ||
|
||
with store.bulk_operations(course_key): | ||
course = modulestore().get_course(course_key) | ||
textbooks_context = get_textbooks_context(course) | ||
serializer = CourseTextbooksSerializer(textbooks_context) | ||
return Response(serializer.data) |
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