diff --git a/cms/djangoapps/contentstore/views.py b/cms/djangoapps/contentstore/views.py index b6aeebd602..53923e5c6b 100644 --- a/cms/djangoapps/contentstore/views.py +++ b/cms/djangoapps/contentstore/views.py @@ -45,6 +45,8 @@ from auth.authz import get_user_by_email, add_user_to_course_group, remove_user_ from auth.authz import ADMIN_ROLE_NAME, EDITOR_ROLE_NAME from .utils import get_course_location_for_item +from xmodule.templates import all_templates + log = logging.getLogger(__name__) @@ -126,9 +128,14 @@ def course_index(request, org, course, name): course = modulestore().get_item(location) sections = course.get_children() + # This knowledge of what the 'new template' should be seems like it needs to be kept deeper down in the + # code. We should probably refactor + template = modulestore().get_item(Location('i4x', 'edx', 'templates', 'vertical', 'Empty')) + return render_to_response('overview.html', { 'sections': sections, - 'upload_asset_callback_url': upload_asset_callback_url + 'upload_asset_callback_url': upload_asset_callback_url, + 'create_new_unit_template': template.location }) @@ -144,8 +151,14 @@ def edit_subsection(request, location): if item.location.category != 'sequential': return HttpResponseBadRequest + # This knowledge of what the 'new template' should be seems like it needs to be kept deeper down in the + # code. We should probably refactor + template = modulestore().get_item(Location('i4x', 'edx', 'templates', 'vertical', 'Empty')) + return render_to_response('edit_subsection.html', - {'subsection':item}) + {'subsection':item, + 'create_new_unit_template' : template.location + }) @login_required def edit_unit(request, location): @@ -407,6 +420,20 @@ def delete_item(request): return HttpResponse() +@login_required +@expect_json +def create_item(request): + # parent_location should be the location of the parent container + parent_location = request.POST['parent_id'] + + # which type of item to create + category = request.POST['category'] + + # check permissions for this user within this course + if not has_access(request.user, parent_location): + raise PermissionDenied() + + @login_required @expect_json @@ -446,6 +473,10 @@ def save_item(request): def clone_item(request): parent_location = Location(request.POST['parent_location']) template = Location(request.POST['template']) + + display_name = None + if 'display_name' in request.POST: + display_name = request.POST['display_name'] if not has_access(request.user, parent_location): raise PermissionDenied() @@ -457,6 +488,10 @@ def clone_item(request): # TODO: This needs to be deleted when we have proper storage for static content new_item.metadata['data_dir'] = parent.metadata['data_dir'] + + # replace the display name with an optional parameter passed in from the caller + if display_name is not None: + new_item.metadata['display_name'] = display_name modulestore().update_metadata(new_item.location.url(), new_item.own_metadata) modulestore().update_children(parent_location, parent.definition.get('children', []) + [new_item.location.url()]) diff --git a/cms/static/js/base.js b/cms/static/js/base.js index 8d1af795b3..364cb61f3d 100644 --- a/cms/static/js/base.js +++ b/cms/static/js/base.js @@ -24,19 +24,39 @@ $(document).ready(function() { $('.assets .upload-button').bind('click', showUploadModal); $('.upload-modal .close-button').bind('click', hideModal); $('.unit .item-actions .delete-button').bind('click', deleteUnit); + $('.new-unit-item').bind('click', createNewUnit); }); +function createNewUnit(e) { + e.preventDefault(); + + parent = $(this).data('parent'); + template = $(this).data('template'); + + $.post('/clone_item', + {'parent_location' : parent, + 'template' : template, + 'display_name': 'New Unit', + }, + function(data) { + // redirect to the edit page + window.location = "/edit/" + data['id']; + }); +} + function deleteUnit(e) { e.preventDefault(); - var id = $(this).data('id'); - var _this = $(this); + + if(!confirm('Are you sure you wish to delete this item. It cannot be reversed!')) + return; + + var _li_el = $(this).parents('li.leaf'); + var id = _li_el.data('id'); $.post('/delete_item', {'id': id, 'delete_children' : 'true'}, function(data) { - // remove 'leaf' class containing