def reorder_tabs_handler(course_item, request): """ Helper function for handling reorder of tabs request """ # Tabs are identified by tab_id or locators. # The locators are used to identify static tabs since they are xmodules. # Although all tabs have tab_ids, newly created static tabs do not know # their tab_ids since the xmodule editor uses only locators to identify new objects. requested_tab_id_locators = request.json["tabs"] # original tab list in original order old_tab_list = course_item.tabs # create a new list in the new order new_tab_list = [] for tab_id_locator in requested_tab_id_locators: tab = get_tab_by_tab_id_locator(old_tab_list, tab_id_locator) if tab is None: return JsonResponse( {"error": "Tab with id_locator '{0}' does not exist.".format(tab_id_locator)}, status=400 ) new_tab_list.append(tab) # the old_tab_list may contain additional tabs that were not rendered in the UI because of # global or course settings. so add those to the end of the list. non_displayed_tabs = set(old_tab_list) - set(new_tab_list) new_tab_list.extend(non_displayed_tabs) # validate the tabs to make sure everything is Ok (e.g., did the client try to reorder unmovable tabs?) try: CourseTabList.validate_tabs(new_tab_list) except InvalidTabsException, exception: return JsonResponse({"error": "New list of tabs is not valid: {0}.".format(str(exception))}, status=400)
def reorder_tabs_handler(course_item, request): """ Helper function for handling reorder of tabs request """ # Tabs are identified by tab_id or locators. # The locators are used to identify static tabs since they are xmodules. # Although all tabs have tab_ids, newly created static tabs do not know # their tab_ids since the xmodule editor uses only locators to identify new objects. requested_tab_id_locators = request.json['tabs'] # original tab list in original order old_tab_list = course_item.tabs # create a new list in the new order new_tab_list = [] for tab_id_locator in requested_tab_id_locators: tab = get_tab_by_tab_id_locator(old_tab_list, tab_id_locator) if tab is None: return JsonResponse( { "error": u"Tab with id_locator '{0}' does not exist.".format( tab_id_locator) }, status=400) new_tab_list.append(tab) # the old_tab_list may contain additional tabs that were not rendered in the UI because of # global or course settings. so add those to the end of the list. non_displayed_tabs = set(old_tab_list) - set(new_tab_list) new_tab_list.extend(non_displayed_tabs) # validate the tabs to make sure everything is Ok (e.g., did the client try to reorder unmovable tabs?) try: CourseTabList.validate_tabs(new_tab_list) except InvalidTabsException as exception: return JsonResponse( { "error": u"New list of tabs is not valid: {0}.".format(str(exception)) }, status=400) # persist the new order of the tabs course_item.tabs = new_tab_list modulestore().update_item(course_item, request.user.id) return JsonResponse()
def reorder_tabs_handler(course_item, tabs_data, user): """ Helper function for handling reorder of static tabs request """ # Tabs are identified by tab_id or locators. # The locators are used to identify static tabs since they are xmodules. # Although all tabs have tab_ids, newly created static tabs do not know # their tab_ids since the xmodule editor uses only locators to identify new objects. requested_tab_id_locators = tabs_data["tabs"] #get original tab list of only static tabs with their original index(position) in the full course tabs list old_tab_dict = {} for idx, tab in enumerate(course_item.tabs): if isinstance(tab, StaticTab): old_tab_dict[tab] = idx old_tab_list = list(old_tab_dict.keys()) new_tab_list = create_new_list(requested_tab_id_locators, old_tab_list) # Creates a full new course tab list of both default and static course tabs # by looping through the new tab list of static only tabs and # putting them in their new position in the list of course item tabs # original_idx gives the list of positions of all static tabs in course tabs originally full_new_tab_list = course_item.tabs original_idx = list(old_tab_dict.values()) for i in range(len(new_tab_list)): full_new_tab_list[original_idx[i]] = new_tab_list[i] # validate the tabs to make sure everything is Ok (e.g., did the client try to reorder unmovable tabs?) try: CourseTabList.validate_tabs(full_new_tab_list) except InvalidTabsException as exception: raise ValidationError({ "error": f"New list of tabs is not valid: {str(exception)}." }) from exception # persist the new order of the tabs course_item.tabs = full_new_tab_list modulestore().update_item(course_item, user.id)
def reorder_tabs_handler(course_item, tabs_data, user): """ Helper function for handling reorder of static tabs request """ # Static tabs are identified by locators (a UsageKey) instead of a tab id like # other tabs. These can be used to identify static tabs since they are xmodules. # Although all tabs have tab_ids, newly created static tabs do not know # their tab_ids since the xmodule editor uses only locators to identify new objects. new_tab_list = create_new_list(tabs_data, course_item.tabs) # validate the tabs to make sure everything is Ok (e.g., did the client try to reorder unmovable tabs?) try: CourseTabList.validate_tabs(new_tab_list) except InvalidTabsException as exception: raise ValidationError({ "error": f"New list of tabs is not valid: {str(exception)}." }) from exception course_item.tabs = new_tab_list modulestore().update_item(course_item, user.id)