-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[BB-7296] Extend settings handler to be accessible via api #32127
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
Closed
pkulkark
wants to merge
1
commit into
openedx:master
from
open-craft:pooja/bb7296-expose-settings-handler-to-api
Closed
Changes from all commits
Commits
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
74 changes: 74 additions & 0 deletions
74
cms/djangoapps/contentstore/rest_api/v0/tests/test_details_settings.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 @@ | ||
| """ | ||
| Tests for the course advanced settings API. | ||
| """ | ||
| import json | ||
|
|
||
| import ddt | ||
| from django.urls import reverse | ||
| from rest_framework import status | ||
|
|
||
| from cms.djangoapps.contentstore.tests.utils import CourseTestCase | ||
|
|
||
|
|
||
| @ddt.ddt | ||
| class CourseDetailsSettingViewTest(CourseTestCase): | ||
| """ | ||
| Tests for DetailsSettings API View. | ||
| """ | ||
|
|
||
| def setUp(self): | ||
| super().setUp() | ||
| self.url = reverse( | ||
| "cms.djangoapps.contentstore:v0:course_details_settings", | ||
| kwargs={"course_id": self.course.id}, | ||
| ) | ||
|
|
||
| def get_and_check_developer_response(self, response): | ||
| """ | ||
| Make basic asserting about the presence of an error response, and return the developer response. | ||
| """ | ||
| content = json.loads(response.content.decode("utf-8")) | ||
| assert "developer_message" in content | ||
| return content["developer_message"] | ||
|
|
||
| def test_permissions_unauthenticated(self): | ||
| """ | ||
| Test that an error is returned in the absence of auth credentials. | ||
| """ | ||
| self.client.logout() | ||
| response = self.client.get(self.url) | ||
| error = self.get_and_check_developer_response(response) | ||
| assert error == "Authentication credentials were not provided." | ||
|
|
||
| def test_permissions_unauthorized(self): | ||
| """ | ||
| Test that an error is returned if the user is unauthorised. | ||
| """ | ||
| client, _ = self.create_non_staff_authed_user_client() | ||
| response = client.get(self.url) | ||
| error = self.get_and_check_developer_response(response) | ||
| assert error == "You do not have permission to perform this action." | ||
|
|
||
| def test_get_course_details(self): | ||
| """ | ||
| Test for get response | ||
| """ | ||
| response = self.client.get(self.url) | ||
| self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
|
||
| def test_patch_course_details(self): | ||
| """ | ||
| Test for patch response | ||
| """ | ||
| data = { | ||
| "start_date": "2030-01-01T00:00:00Z", | ||
| "end_date": "2030-01-31T00:00:00Z", | ||
| "enrollment_start": "2029-12-01T00:00:00Z", | ||
| "enrollment_end": "2030-01-01T00:00:00Z", | ||
| "course_title": "Test Course", | ||
| "short_description": "This is a test course", | ||
| "overview": "This course is for testing purposes", | ||
| "intro_video": None | ||
| } | ||
| response = self.client.patch(self.url, data, content_type='application/json') | ||
| self.assertEqual(response.status_code, status.HTTP_200_OK) |
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
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
76 changes: 76 additions & 0 deletions
76
cms/djangoapps/contentstore/rest_api/v0/views/details_settings.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,76 @@ | ||
| """ API Views for course details settings """ | ||
|
|
||
| import edx_api_doc_tools as apidocs | ||
| from django.core.exceptions import ValidationError as DjangoValidationError | ||
| from opaque_keys.edx.keys import CourseKey | ||
| from rest_framework.request import Request | ||
| from rest_framework.views import APIView | ||
| from xmodule.modulestore.django import modulestore | ||
|
|
||
| from cms.djangoapps.models.settings.encoder import CourseSettingsEncoder | ||
| from common.djangoapps.student.auth import has_studio_read_access, has_studio_write_access | ||
| from common.djangoapps.util.json_request import JsonResponse, JsonResponseBadRequest, expect_json_in_class_view | ||
| from openedx.core.lib.api.view_utils import DeveloperErrorViewMixin, verify_course_exists, view_auth_classes | ||
| from openedx.core.djangoapps.models.course_details import CourseDetails | ||
|
|
||
| from ....utils import update_course_details | ||
|
|
||
|
|
||
| @view_auth_classes(is_authenticated=True) | ||
| class CourseDetailsSettingsView(DeveloperErrorViewMixin, APIView): | ||
| """ | ||
| View for getting and setting the details settings for a course. | ||
| """ | ||
|
|
||
| @apidocs.schema( | ||
| parameters=[ | ||
| apidocs.string_parameter("course_id", apidocs.ParameterLocation.PATH, description="Course ID"), | ||
| ], | ||
| responses={ | ||
| 401: "The requester is not authenticated.", | ||
| 403: "The requester cannot access the specified course.", | ||
| 404: "The requested course does not exist.", | ||
| }, | ||
| ) | ||
| @expect_json_in_class_view | ||
| @verify_course_exists() | ||
| def get(self, request: Request, course_id: str): | ||
| """ | ||
| Get an object containing all the details settings in a course. | ||
| """ | ||
| course_key = CourseKey.from_string(course_id) | ||
| if not has_studio_read_access(request.user, course_key): | ||
| self.permission_denied(request) | ||
| course_details = CourseDetails.fetch(course_key) | ||
| return JsonResponse( | ||
| course_details, | ||
| # encoder serializes dates, old locations, and instances | ||
| encoder=CourseSettingsEncoder | ||
| ) | ||
|
|
||
| @apidocs.schema( | ||
| parameters=[ | ||
| apidocs.string_parameter("course_id", apidocs.ParameterLocation.PATH, description="Course ID"), | ||
| ], | ||
| responses={ | ||
| 401: "The requester is not authenticated.", | ||
| 403: "The requester cannot access the specified course.", | ||
| 404: "The requested course does not exist.", | ||
| }, | ||
| ) | ||
| @expect_json_in_class_view | ||
| @verify_course_exists() | ||
| def patch(self, request: Request, course_id: str): | ||
| """ | ||
| Update a course's details settings. | ||
| """ | ||
| course_key = CourseKey.from_string(course_id) | ||
| if not has_studio_write_access(request.user, course_key): | ||
| self.permission_denied(request) | ||
| course_block = modulestore().get_course(course_key) | ||
| try: | ||
| update_data = update_course_details(request, course_key, request.json, course_block) | ||
| except DjangoValidationError as err: | ||
| return JsonResponseBadRequest({"error": err.message}) | ||
|
|
||
| return JsonResponse(update_data, encoder=CourseSettingsEncoder) | ||
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.
This doesn't tell me anything about what settings can be in the patch. What settings can actually be updated by this API endpoint? Is that already documented somewhere and you can just point to that from here? If not, can you provide a list of the values that can be in the patch body? Also, is it possible to write a serializer for the data being output and input for these views? That would solve both the documentation issue and could be used to perform some basic validation on the input.
Uh oh!
There was an error while loading. Please reload this page.
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.
@feanil This is meant to allow API access to the existing settings_handler which currently only allow session-based requests. It doesn't change any of its functionality. We added serializers for it in a separate django app (c.f. ref) which consumes this api along with the existing advanced_settings api. But you're right, I think we could still add one here like the CourseAdvancedSettingsSerializer. I'll add it and update here.