Skip to content

Commit

Permalink
feat: basic get/post endpoint for v2 xblocks (incomplete). TNL-10873
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Clary committed Jun 30, 2023
1 parent 92301a2 commit e00cda1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions openedx/core/djangoapps/xblock/rest_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
path('xblocks/<str:usage_key_str>/', include([
# get metadata about an XBlock:
path('', views.block_metadata),
# get/post full json fields of an XBlock:
path('fields/', views.BlockFieldsView.as_view()),
# render one of this XBlock's views (e.g. student_view)
re_path(r'^view/(?P<view_name>[\w\-]+)/$', views.render_block_view),
# get the URL needed to call this XBlock's handlers
Expand Down
28 changes: 28 additions & 0 deletions openedx/core/djangoapps/xblock/rest_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
Studio APIs cover use cases like adding/deleting/editing blocks.
"""

from common.djangoapps.util.json_request import JsonResponse, expect_json
from corsheaders.signals import check_request_enabled
from django.contrib.auth import get_user_model
from django.db.transaction import atomic
from django.http import Http404
from django.views.decorators.clickjacking import xframe_options_exempt
from django.views.decorators.csrf import csrf_exempt
from rest_framework import permissions
from rest_framework.decorators import api_view, permission_classes # lint-amnesty, pylint: disable=unused-import
from rest_framework.exceptions import PermissionDenied, AuthenticationFailed, NotFound
from rest_framework.response import Response
from rest_framework.views import APIView
from xblock.django.request import DjangoWebobRequest, webob_to_django_response
from xblock.fields import Scope

from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import UsageKey
from openedx.core.lib.api.view_utils import view_auth_classes
from ..api import (
get_block_display_name,
get_block_metadata,
get_handler_url as _get_handler_url,
load_block,
Expand Down Expand Up @@ -168,3 +173,26 @@ def cors_allow_xblock_handler(sender, request, **kwargs): # lint-amnesty, pylin


check_request_enabled.connect(cors_allow_xblock_handler)


@view_auth_classes()
class BlockFieldsView(APIView):

@atomic
def get(self, request, usage_key_str):
try:
usage_key = UsageKey.from_string(usage_key_str)
except InvalidKeyError as e:
raise NotFound(invalid_not_found_fmt.format(usage_key=usage_key_str)) from e

block = load_block(usage_key, request.user)
block_dict = {
"display_name": get_block_display_name(block),
"data": block.data,
"metadata": block.get_explicitly_set_fields_by_scope(Scope.settings),
}
return JsonResponse(block_dict)

@atomic
def post(self, request, usage_key_str):
pass

0 comments on commit e00cda1

Please sign in to comment.