feat!: Change the way tabs are ordered [BD-38] [TNL-9174] [BB-5076] (#29262)

* feat!: Change the way tabs are ordered
The change imposes a new ordering for tabs based on their new priority. When reordering tabs, this ordering will be maintained.

* fix: Apply suggestions from code review

Co-authored-by: Farhaan Bukhsh <farhaan@opencraft.com>

* fix: review feedback

Co-authored-by: Farhaan Bukhsh <farhaan@opencraft.com>
This commit is contained in:
Kshitij Sobti
2021-11-22 06:17:30 +00:00
committed by GitHub
parent 2e2701d82a
commit e8e8f4acbe
16 changed files with 115 additions and 140 deletions

View File

@@ -6,6 +6,7 @@ import json
from urllib.parse import urlencode
import ddt
import random
from django.urls import reverse
from xmodule.modulestore.tests.factories import ItemFactory
from xmodule.tabs import CourseTabList
@@ -95,36 +96,38 @@ class TabsAPITests(CourseTestCase):
content_type="application/json",
)
def test_reorder_tabs(self):
def test_reorder_static_tabs(self):
"""
Test re-ordering of tabs
Test re-ordering of static tabs in a course.
"""
# get the original tab ids
orig_tab_ids = [tab.tab_id for tab in self.course.tabs]
tab_ids = list(orig_tab_ids)
num_orig_tabs = len(orig_tab_ids)
# get the original tabs
course_tabs = list(self.course.tabs)
num_orig_tabs = len(self.course.tabs)
# make sure we have enough tabs to play around with
assert num_orig_tabs >= 5
# reorder the last two tabs
tab_ids[num_orig_tabs - 1], tab_ids[num_orig_tabs - 2] = tab_ids[num_orig_tabs - 2], tab_ids[num_orig_tabs - 1]
# Randomize the order of static tabs, leaving the rest intact
course_tabs.sort(key=lambda tab: (100 + random.random()) if tab.type == 'static_tab' else tab.priority)
# remove the third to the last tab, the removed tab has to be a static tab
# (the code needs to handle the case where tabs requested for re-ordering is a subset of the tabs in the course)
removed_tab = tab_ids.pop(num_orig_tabs - 3)
self.assertEqual(len(tab_ids), num_orig_tabs - 1)
tabs_data = [
{'tab_locator': str(self.course.id.make_usage_key("static_tab", tab.url_slug))}
for tab in course_tabs
if tab.type == 'static_tab'
]
# Remove one tab randomly. This shouldn't delete the tab.
tabs_data.pop()
# post the request
resp = self.make_reorder_tabs_request([{"tab_id": tab_id} for tab_id in tab_ids if 'static' in tab_id])
# post the request with the reordered static tabs only
resp = self.make_reorder_tabs_request(tabs_data)
assert resp.status_code == 204
# reload the course and verify the new tab order
# Reload the course and verify the new tab order
self.reload_course()
reordered_tab_ids = [tab.tab_id for tab in course_tabs]
new_tab_ids = [tab.tab_id for tab in self.course.tabs]
assert new_tab_ids == tab_ids + [removed_tab]
assert new_tab_ids != orig_tab_ids
assert new_tab_ids == reordered_tab_ids
def test_reorder_tabs_invalid_list(self):
"""

View File

@@ -24,7 +24,7 @@ class AdvancedCourseSettingsView(DeveloperErrorViewMixin, APIView):
class FilterQuery(forms.Form):
"""
Form for validating query marameters passed to advanced course settings view
Form for validating query parameters passed to advanced course settings view
to filter the data it returns.
"""
filter_fields = forms.CharField(strip=True, empty_value=None, required=False)

View File

@@ -177,7 +177,7 @@ class CourseTabReorderView(DeveloperErrorViewMixin, APIView):
return super().handle_exception(exc)
@apidocs.schema(
body=[TabIDLocatorSerializer],
body=TabIDLocatorSerializer(many=True),
parameters=[
apidocs.string_parameter("course_id", apidocs.ParameterLocation.PATH, description="Course ID"),
],
@@ -198,24 +198,12 @@ class CourseTabReorderView(DeveloperErrorViewMixin, APIView):
Move course tabs:
POST /api/contentstore/v0/tabs/{course_id}/reorder [
{
"tab_id": "info"
},
{
"tab_id": "courseware"
},
{
"tab_locator": "block-v1:TstX+DemoX+Demo+type@static_tab+block@d26fcb0e93824fbfa5c9e5f100e2511a"
},
{
"tab_id": "wiki"
"tab_locator": "block-v1:TstX+DemoX+Demo+type@static_tab+block@a011f1bd05af4578ae397ed8cabccf62"
},
{
"tab_id": "discussion"
},
{
"tab_id": "progress"
}
]
@@ -233,7 +221,7 @@ class CourseTabReorderView(DeveloperErrorViewMixin, APIView):
tab_id_locators.is_valid(raise_exception=True)
reorder_tabs_handler(
course_module,
{"tabs": tab_id_locators.validated_data},
tab_id_locators.validated_data,
request.user,
)
return Response(status=status.HTTP_204_NO_CONTENT)