forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 14
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
feat: [AXIMST-111] create API for course textbooks #2491
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it should be
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is possible
But I used the same format as i was in previous API's, dict instead of list
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this was intentional, idk