Skip to content

Commit c988688

Browse files
authored
feat: include the usage_key in API to get ancestor blocks (#37072)
- includes location along with the child ancestor block as it is required in the frontend authoring app for unit scrolling purpose in the breadcrumbs. Required for openedx/frontend-app-authoring#1924
1 parent 8485b2f commit c988688

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

cms/djangoapps/contentstore/rest_api/v1/serializers/vertical_block.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ChildAncestorSerializer(serializers.Serializer):
2828

2929
url = serializers.SerializerMethodField()
3030
display_name = serializers.CharField(source="display_name_with_default")
31+
usage_key = serializers.CharField(source="location") # need it for frontend authoring app
3132

3233
def get_url(self, obj):
3334
"""

cms/djangoapps/contentstore/rest_api/v1/views/tests/test_vertical_block.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,59 @@ def test_success_response(self):
127127
response = self.client.get(url)
128128
self.assertEqual(response.status_code, status.HTTP_200_OK)
129129

130+
def test_ancestor_xblocks_response(self):
131+
"""
132+
Check if the ancestor_xblocks are returned as expected.
133+
"""
134+
expected_ancestor_xblocks = [
135+
{
136+
'children': [
137+
{
138+
'url': (
139+
'/course/course-v1:org.552+course_552+Run_552'
140+
'?show=block-v1%3Aorg.552%2Bcourse_552%2BRun_552'
141+
'%2Btype%40chapter%2Bblock%40Week_1'
142+
),
143+
'display_name': 'Week 1',
144+
'usage_key': (
145+
'block-v1:org.552+course_552+Run_552+type@chapter+block@Week_1'
146+
),
147+
}
148+
],
149+
'title': 'Week 1',
150+
'is_last': False,
151+
},
152+
{
153+
'children': [
154+
{
155+
'url': (
156+
'/course/course-v1:org.552+course_552+Run_552'
157+
'?show=block-v1%3Aorg.552%2Bcourse_552%2BRun_552'
158+
'%2Btype%40sequential%2Bblock%40Lesson_1'
159+
),
160+
'display_name': 'Lesson 1',
161+
'usage_key': (
162+
'block-v1:org.552+course_552+Run_552+type@sequential+block@Lesson_1'
163+
),
164+
}
165+
],
166+
'title': 'Lesson 1',
167+
'is_last': True,
168+
}
169+
]
170+
171+
url = self.get_reverse_url(self.vertical.location)
172+
response = self.client.get(url)
173+
response_ancestor_xblocks = response.json().get("ancestor_xblocks", [])
174+
175+
def sort_key(block):
176+
return block.get("title", "")
177+
178+
self.assertEqual(
179+
sorted(response_ancestor_xblocks, key=sort_key),
180+
sorted(expected_ancestor_xblocks, key=sort_key)
181+
)
182+
130183
def test_not_valid_usage_key_string(self):
131184
"""
132185
Check that invalid 'usage_key_string' raises Http404.

cms/djangoapps/contentstore/rest_api/v1/views/vertical_block.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def get(self, request: Request, usage_key_string: str):
8989
"children": [
9090
{
9191
"url": "/course/course-v1:edX+DemoX+Demo_Course?show=block-v1%3AedX%2BDemoX%2BDemo_Course%2Btype%",
92-
"display_name": "Introduction"
92+
"display_name": "Introduction",
93+
"usage_key": "subsection_id"
9394
},
9495
...
9596
],

0 commit comments

Comments
 (0)