-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Upstream Sync with Content Library Blocks
- Loading branch information
1 parent
0e061de
commit fc46b57
Showing
10 changed files
with
651 additions
and
11 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
"""Module for v2 views.""" | ||
|
||
from cms.djangoapps.contentstore.rest_api.v2.views.home import HomePageCoursesViewV2 | ||
from cms.djangoapps.contentstore.rest_api.v2.views.upstream_sync import UpstreamSyncView |
72 changes: 72 additions & 0 deletions
72
cms/djangoapps/contentstore/rest_api/v2/views/upstream_sync.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,72 @@ | ||
""" API Views for syncing upstream content to downstream content """ | ||
|
||
import edx_api_doc_tools as apidocs | ||
from opaque_keys import InvalidKeyError | ||
from opaque_keys.edx.keys import UsageKey | ||
from rest_framework.request import Request | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
from cms.lib.xblock.upstream_sync import sync_from_upstream, BadUpstream, BadDownstream | ||
from common.djangoapps.student.auth import has_studio_write_access | ||
from openedx.core.lib.api.view_utils import ( | ||
DeveloperErrorViewMixin, | ||
view_auth_classes, | ||
) | ||
from xmodule.modulestore.django import modulestore | ||
from xmodule.modulestore.exceptions import ItemNotFoundError | ||
|
||
|
||
@view_auth_classes(is_authenticated=True) | ||
class UpstreamSyncView(DeveloperErrorViewMixin, APIView): | ||
""" | ||
@@TODO | ||
""" | ||
|
||
@apidocs.schema( | ||
parameters=[ | ||
apidocs.string_parameter( | ||
"course_id", apidocs.ParameterLocation.PATH, description="Course ID" | ||
), | ||
], | ||
responses={ | ||
200: None, | ||
400: "Downstream block ID is invalid.", | ||
401: "The requester is not authenticated.", | ||
403: "The requester cannot modify the specified downstream block.", | ||
404: "The specified downstream block does not exist.", | ||
422: "Failed to sync content.", | ||
}, | ||
) | ||
def post(self, request: Request, usage_id: str): | ||
""" | ||
Pull latest updates to the block at {usage_id} from its linked upstream content. | ||
**Example Request** | ||
POST /api/contentstore/v1/upstream_sync/{usage_id} | ||
**Response Values** | ||
If the request is successful, an HTTP 200 "OK" response is returned, with no body. | ||
If the request fails, an HTTP 4xx response is returned, with an error message. | ||
""" | ||
try: | ||
usage_key = UsageKey.from_string(usage_id) | ||
except InvalidKeyError: | ||
return Response("Invalid block key", status=400) | ||
if not has_studio_write_access(request.user, usage_key.context_key): | ||
self.permission_denied(request) | ||
|
||
store = modulestore() | ||
with store.bulk_operations(usage_key.context_key): | ||
try: | ||
downstream = store.get_item(usage_key) | ||
except ItemNotFoundError: | ||
return Response("Block not found", status=404) | ||
try: | ||
sync_from_upstream(downstream, request.user, apply_updates=True) | ||
except (BadUpstream, BadDownstream) as exc: | ||
return Response(str(exc), status=422) | ||
downstream.save() | ||
store.update_item(downstream, request.user.id) | ||
return Response() |
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.