def edit_tabs(request, org, course, coursename): "Edit tabs" location = ['i4x', org, course, 'course', coursename] store = get_modulestore(location) course_item = store.get_item(location) # check that logged in user has permissions to this item if not has_access(request.user, location): raise PermissionDenied() # see tabs have been uninitialized (e.g. supporing courses created before tab support in studio) if course_item.tabs is None or len(course_item.tabs) == 0: initialize_course_tabs(course_item) # first get all static tabs from the tabs list # we do this because this is also the order in which items are displayed in the LMS static_tabs_refs = [t for t in course_item.tabs if t['type'] == 'static_tab'] static_tabs = [] for static_tab_ref in static_tabs_refs: static_tab_loc = Location(location)._replace(category='static_tab', name=static_tab_ref['url_slug']) static_tabs.append(modulestore('direct').get_item(static_tab_loc)) components = [ static_tab.location.url() for static_tab in static_tabs ] return render_to_response('edit-tabs.html', { 'context_course': course_item, 'components': components })
def edit_tabs(request, org, course, coursename): location = ["i4x", org, course, "course", coursename] course_item = modulestore().get_item(location) # check that logged in user has permissions to this item if not has_access(request.user, location): raise PermissionDenied() # see tabs have been uninitialized (e.g. supporing courses created before tab support in studio) if course_item.tabs is None or len(course_item.tabs) == 0: initialize_course_tabs(course_item) # first get all static tabs from the tabs list # we do this because this is also the order in which items are displayed in the LMS static_tabs_refs = [t for t in course_item.tabs if t["type"] == "static_tab"] static_tabs = [] for static_tab_ref in static_tabs_refs: static_tab_loc = Location(location)._replace(category="static_tab", name=static_tab_ref["url_slug"]) static_tabs.append(modulestore("direct").get_item(static_tab_loc)) components = [static_tab.location.url() for static_tab in static_tabs] return render_to_response( "edit-tabs.html", {"active_tab": "pages", "context_course": course_item, "components": components} )
def edit_tabs(request, org, course, coursename): location = ['i4x', org, course, 'course', coursename] store = get_modulestore(location) course_item = store.get_item(location) # check that logged in user has permissions to this item if not has_access(request.user, location): raise PermissionDenied() # see tabs have been uninitialized (e.g. supporing courses created before tab support in studio) if course_item.tabs is None or len(course_item.tabs) == 0: initialize_course_tabs(course_item) # first get all static tabs from the tabs list # we do this because this is also the order in which items are displayed in the LMS static_tabs_refs = [t for t in course_item.tabs if t['type'] == 'static_tab'] static_tabs = [] for static_tab_ref in static_tabs_refs: static_tab_loc = Location(location)._replace(category='static_tab', name=static_tab_ref['url_slug']) static_tabs.append(modulestore('direct').get_item(static_tab_loc)) components = [ static_tab.location.url() for static_tab in static_tabs ] return render_to_response('edit-tabs.html', { 'context_course': course_item, 'components': components })
def reorder_static_tabs(request): "Order the static tabs in the requested order" def get_location_for_tab(tab): tab_locator = BlockUsageLocator(tab) return loc_mapper().translate_locator_to_location(tab_locator) tabs = request.json["tabs"] course_location = loc_mapper().translate_locator_to_location(BlockUsageLocator(tabs[0]), get_course=True) if not has_access(request.user, course_location): raise PermissionDenied() course = get_modulestore(course_location).get_item(course_location) # get list of existing static tabs in course # make sure they are the same lengths (i.e. the number of passed in tabs equals the number # that we know about) otherwise we can drop some! existing_static_tabs = [t for t in course.tabs if t["type"] == "static_tab"] if len(existing_static_tabs) != len(tabs): return HttpResponseBadRequest() # load all reference tabs, return BadRequest if we can't find any of them tab_items = [] for tab in tabs: item = modulestore("direct").get_item(get_location_for_tab(tab)) if item is None: return HttpResponseBadRequest() tab_items.append(item) # now just go through the existing course_tabs and re-order the static tabs reordered_tabs = [] static_tab_idx = 0 for tab in course.tabs: if tab["type"] == "static_tab": reordered_tabs.append( { "type": "static_tab", "name": tab_items[static_tab_idx].display_name, "url_slug": tab_items[static_tab_idx].location.name, } ) static_tab_idx += 1 else: reordered_tabs.append(tab) # OK, re-assemble the static tabs in the new order course.tabs = reordered_tabs # Save the data that we've just changed to the underlying # MongoKeyValueStore before we update the mongo datastore. course.save() modulestore("direct").update_metadata(course.location, own_metadata(course)) # TODO: above two lines are used for the primitive-save case. Maybe factor them out? return HttpResponse()
def reorder_static_tabs(request): "Order the static tabs in the requested order" tabs = request.json['tabs'] course = get_course_for_item(tabs[0]) if not has_access(request.user, course.location): raise PermissionDenied() # get list of existing static tabs in course # make sure they are the same lengths (i.e. the number of passed in tabs equals the number # that we know about) otherwise we can drop some! existing_static_tabs = [ t for t in course.tabs if t['type'] == 'static_tab' ] if len(existing_static_tabs) != len(tabs): return HttpResponseBadRequest() # load all reference tabs, return BadRequest if we can't find any of them tab_items = [] for tab in tabs: item = modulestore('direct').get_item(Location(tab)) if item is None: return HttpResponseBadRequest() tab_items.append(item) # now just go through the existing course_tabs and re-order the static tabs reordered_tabs = [] static_tab_idx = 0 for tab in course.tabs: if tab['type'] == 'static_tab': reordered_tabs.append({ 'type': 'static_tab', 'name': tab_items[static_tab_idx].display_name, 'url_slug': tab_items[static_tab_idx].location.name }) static_tab_idx += 1 else: reordered_tabs.append(tab) # OK, re-assemble the static tabs in the new order course.tabs = reordered_tabs # Save the data that we've just changed to the underlying # MongoKeyValueStore before we update the mongo datastore. course.save() modulestore('direct').update_metadata(course.location, own_metadata(course)) # TODO: above two lines are used for the primitive-save case. Maybe factor them out? return HttpResponse()
def reorder_static_tabs(request): tabs = request.POST['tabs'] course = get_course_for_item(tabs[0]) if not has_access(request.user, course.location): raise PermissionDenied() # get list of existing static tabs in course # make sure they are the same lengths (i.e. the number of passed in tabs equals the number # that we know about) otherwise we can drop some! existing_static_tabs = [ t for t in course.tabs if t['type'] == 'static_tab' ] if len(existing_static_tabs) != len(tabs): return HttpResponseBadRequest() # load all reference tabs, return BadRequest if we can't find any of them tab_items = [] for tab in tabs: item = modulestore('direct').get_item(Location(tab)) if item is None: return HttpResponseBadRequest() tab_items.append(item) # now just go through the existing course_tabs and re-order the static tabs reordered_tabs = [] static_tab_idx = 0 for tab in course.tabs: if tab['type'] == 'static_tab': reordered_tabs.append({ 'type': 'static_tab', 'name': tab_items[static_tab_idx].display_name, 'url_slug': tab_items[static_tab_idx].location.name }) static_tab_idx += 1 else: reordered_tabs.append(tab) # OK, re-assemble the static tabs in the new order course.tabs = reordered_tabs modulestore('direct').update_metadata(course.location, own_metadata(course)) return HttpResponse()
def reorder_static_tabs(request): "Order the static tabs in the requested order" tabs = request.json['tabs'] course = get_course_for_item(tabs[0]) if not has_access(request.user, course.location): raise PermissionDenied() # get list of existing static tabs in course # make sure they are the same lengths (i.e. the number of passed in tabs equals the number # that we know about) otherwise we can drop some! existing_static_tabs = [t for t in course.tabs if t['type'] == 'static_tab'] if len(existing_static_tabs) != len(tabs): return HttpResponseBadRequest() # load all reference tabs, return BadRequest if we can't find any of them tab_items = [] for tab in tabs: item = modulestore('direct').get_item(Location(tab)) if item is None: return HttpResponseBadRequest() tab_items.append(item) # now just go through the existing course_tabs and re-order the static tabs reordered_tabs = [] static_tab_idx = 0 for tab in course.tabs: if tab['type'] == 'static_tab': reordered_tabs.append({'type': 'static_tab', 'name': tab_items[static_tab_idx].display_name, 'url_slug': tab_items[static_tab_idx].location.name}) static_tab_idx += 1 else: reordered_tabs.append(tab) # OK, re-assemble the static tabs in the new order course.tabs = reordered_tabs # Save the data that we've just changed to the underlying # MongoKeyValueStore before we update the mongo datastore. course.save() modulestore('direct').update_metadata(course.location, own_metadata(course)) # TODO: above two lines are used for the primitive-save case. Maybe factor them out? return HttpResponse()
def reorder_static_tabs(request): tabs = request.POST["tabs"] course = get_course_for_item(tabs[0]) if not has_access(request.user, course.location): raise PermissionDenied() # get list of existing static tabs in course # make sure they are the same lengths (i.e. the number of passed in tabs equals the number # that we know about) otherwise we can drop some! existing_static_tabs = [t for t in course.tabs if t["type"] == "static_tab"] if len(existing_static_tabs) != len(tabs): return HttpResponseBadRequest() # load all reference tabs, return BadRequest if we can't find any of them tab_items = [] for tab in tabs: item = modulestore("direct").get_item(Location(tab)) if item is None: return HttpResponseBadRequest() tab_items.append(item) # now just go through the existing course_tabs and re-order the static tabs reordered_tabs = [] static_tab_idx = 0 for tab in course.tabs: if tab["type"] == "static_tab": reordered_tabs.append( { "type": "static_tab", "name": tab_items[static_tab_idx].display_name, "url_slug": tab_items[static_tab_idx].location.name, } ) static_tab_idx += 1 else: reordered_tabs.append(tab) # OK, re-assemble the static tabs in the new order course.tabs = reordered_tabs modulestore("direct").update_metadata(course.location, own_metadata(course)) return HttpResponse()
def tabs_handler(request, tag=None, package_id=None, branch=None, version_guid=None, block=None): """ The restful handler for static tabs. GET html: return page for editing static tabs json: not supported PUT or POST json: update the tab order. It is expected that the request body contains a JSON-encoded dict with entry "tabs". The value for "tabs" is an array of tab locators, indicating the desired order of the tabs. Creating a tab, deleting a tab, or changing its contents is not supported through this method. Instead use the general xblock URL (see item.xblock_handler). """ locator = BlockUsageLocator(package_id=package_id, branch=branch, version_guid=version_guid, block_id=block) if not has_access(request.user, locator): raise PermissionDenied() old_location = loc_mapper().translate_locator_to_location(locator) store = get_modulestore(old_location) course_item = store.get_item(old_location) if 'application/json' in request.META.get('HTTP_ACCEPT', 'application/json'): if request.method == 'GET': raise NotImplementedError('coming soon') else: if 'tabs' in request.json: def get_location_for_tab(tab): """ Returns the location (old-style) for a tab. """ return loc_mapper().translate_locator_to_location(BlockUsageLocator(tab)) tabs = request.json['tabs'] # get list of existing static tabs in course # make sure they are the same lengths (i.e. the number of passed in tabs equals the number # that we know about) otherwise we will inadvertently drop some! existing_static_tabs = [t for t in course_item.tabs if t['type'] == 'static_tab'] if len(existing_static_tabs) != len(tabs): return JsonResponse( {"error": "number of tabs must be {}".format(len(existing_static_tabs))}, status=400 ) # load all reference tabs, return BadRequest if we can't find any of them tab_items = [] for tab in tabs: item = modulestore('direct').get_item(get_location_for_tab(tab)) if item is None: return JsonResponse( {"error": "no tab for found location {}".format(tab)}, status=400 ) tab_items.append(item) # now just go through the existing course_tabs and re-order the static tabs reordered_tabs = [] static_tab_idx = 0 for tab in course_item.tabs: if tab['type'] == 'static_tab': reordered_tabs.append( {'type': 'static_tab', 'name': tab_items[static_tab_idx].display_name, 'url_slug': tab_items[static_tab_idx].location.name, } ) static_tab_idx += 1 else: reordered_tabs.append(tab) # OK, re-assemble the static tabs in the new order course_item.tabs = reordered_tabs modulestore('direct').update_metadata(course_item.location, own_metadata(course_item)) return JsonResponse() else: raise NotImplementedError('Creating or changing tab content is not supported.') elif request.method == 'GET': # assume html # see tabs have been uninitialized (e.g. supporting courses created before tab support in studio) if course_item.tabs is None or len(course_item.tabs) == 0: initialize_course_tabs(course_item) # first get all static tabs from the tabs list # we do this because this is also the order in which items are displayed in the LMS static_tabs_refs = [t for t in course_item.tabs if t['type'] == 'static_tab'] static_tabs = [] for static_tab_ref in static_tabs_refs: static_tab_loc = old_location.replace(category='static_tab', name=static_tab_ref['url_slug']) static_tabs.append(modulestore('direct').get_item(static_tab_loc)) components = [ loc_mapper().translate_location( course_item.location.course_id, static_tab.location, False, True ) for static_tab in static_tabs ] return render_to_response('edit-tabs.html', { 'context_course': course_item, 'components': components, 'course_locator': locator }) else: return HttpResponseNotFound()
def tabs_handler(request, tag=None, package_id=None, branch=None, version_guid=None, block=None): """ The restful handler for static tabs. GET html: return page for editing static tabs json: not supported PUT or POST json: update the tab order. It is expected that the request body contains a JSON-encoded dict with entry "tabs". The value for "tabs" is an array of tab locators, indicating the desired order of the tabs. Creating a tab, deleting a tab, or changing its contents is not supported through this method. Instead use the general xblock URL (see item.xblock_handler). """ locator = BlockUsageLocator(package_id=package_id, branch=branch, version_guid=version_guid, block_id=block) if not has_access(request.user, locator): raise PermissionDenied() old_location = loc_mapper().translate_locator_to_location(locator) store = get_modulestore(old_location) course_item = store.get_item(old_location) if 'application/json' in request.META.get('HTTP_ACCEPT', 'application/json'): if request.method == 'GET': raise NotImplementedError('coming soon') else: if 'tabs' in request.json: def get_location_for_tab(tab): """ Returns the location (old-style) for a tab. """ return loc_mapper().translate_locator_to_location( BlockUsageLocator(tab)) tabs = request.json['tabs'] # get list of existing static tabs in course # make sure they are the same lengths (i.e. the number of passed in tabs equals the number # that we know about) otherwise we will inadvertently drop some! existing_static_tabs = [ t for t in course_item.tabs if t['type'] == 'static_tab' ] if len(existing_static_tabs) != len(tabs): return JsonResponse( { "error": "number of tabs must be {}".format( len(existing_static_tabs)) }, status=400) # load all reference tabs, return BadRequest if we can't find any of them tab_items = [] for tab in tabs: item = modulestore('direct').get_item( get_location_for_tab(tab)) if item is None: return JsonResponse( { "error": "no tab for found location {}".format(tab) }, status=400) tab_items.append(item) # now just go through the existing course_tabs and re-order the static tabs reordered_tabs = [] static_tab_idx = 0 for tab in course_item.tabs: if tab['type'] == 'static_tab': reordered_tabs.append({ 'type': 'static_tab', 'name': tab_items[static_tab_idx].display_name, 'url_slug': tab_items[static_tab_idx].location.name, }) static_tab_idx += 1 else: reordered_tabs.append(tab) # OK, re-assemble the static tabs in the new order course_item.tabs = reordered_tabs modulestore('direct').update_metadata( course_item.location, own_metadata(course_item)) return JsonResponse() else: raise NotImplementedError( 'Creating or changing tab content is not supported.') elif request.method == 'GET': # assume html # see tabs have been uninitialized (e.g. supporting courses created before tab support in studio) if course_item.tabs is None or len(course_item.tabs) == 0: initialize_course_tabs(course_item) # first get all static tabs from the tabs list # we do this because this is also the order in which items are displayed in the LMS static_tabs_refs = [ t for t in course_item.tabs if t['type'] == 'static_tab' ] static_tabs = [] for static_tab_ref in static_tabs_refs: static_tab_loc = old_location.replace( category='static_tab', name=static_tab_ref['url_slug']) static_tabs.append(modulestore('direct').get_item(static_tab_loc)) components = [ loc_mapper().translate_location(course_item.location.course_id, static_tab.location, False, True) for static_tab in static_tabs ] return render_to_response( 'edit-tabs.html', { 'context_course': course_item, 'components': components, 'course_locator': locator }) else: return HttpResponseNotFound()