diff --git a/cms/djangoapps/contentstore/views.py b/cms/djangoapps/contentstore/views.py index 1df2c44ff7..78a1a41bc1 100644 --- a/cms/djangoapps/contentstore/views.py +++ b/cms/djangoapps/contentstore/views.py @@ -209,10 +209,6 @@ def preview_component(request, location): }) -@login_required -def delete_unit(request, location): - pass - def user_author_string(user): '''Get an author string for commits by this user. Format: @@ -382,12 +378,33 @@ def get_module_previews(request, descriptor): preview_html.append(module.get_html()) return preview_html +def _delete_item(item, recurse=False): + if recurse: + children = item.get_children() + for child in children: + _delete_item(child) + + modulestore().delete_item(item.location); + @login_required @expect_json def delete_item(request): item_location = request.POST['id'] - modulestore().delete_item(item_location) + + # check permissions for this user within this course + if not has_access(request.user, item_location): + raise PermissionDenied() + + # optional parameter to delete all children (default False) + delete_children = False + if 'delete_children' in request.POST: + delete_children = request.POST['delete_children'] in ['true', 'True'] + + item = modulestore().get_item(item_location) + + _delete_item(item) + return HttpResponse() diff --git a/cms/static/js/base.js b/cms/static/js/base.js index 16a7f87202..86846ee5e4 100644 --- a/cms/static/js/base.js +++ b/cms/static/js/base.js @@ -20,8 +20,24 @@ $(document).ready(function() { $('.unit-history ol a').bind('click', showHistoryModal); $modal.bind('click', hideHistoryModal); $modalCover.bind('click', hideHistoryModal); + + $('.unit .item-actions .delete-button').bind('click', deleteUnit); }); +function deleteUnit(e) { + e.preventDefault(); + var id = $(this).data('id'); + var _this = $(this); + + $.post('/delete_item', + {'id': id, 'delete_children' : 'true'}, + function(data) { + // remove 'leaf' class containing