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
  • element + var parent = _this.parents('li.leaf'); + parent.remove(); + }); +} + function toggleSubmodules(e) { e.preventDefault(); $(this).toggleClass('expand').toggleClass('collapse'); diff --git a/cms/templates/widgets/units.html b/cms/templates/widgets/units.html index 12b0d33039..2605277e71 100644 --- a/cms/templates/widgets/units.html +++ b/cms/templates/widgets/units.html @@ -6,13 +6,13 @@ This def will enumerate through a passed in subsection and list all of the units <%def name="enum_units(subsection)">
      % for unit in subsection.get_children(): -
    1. +
    2. @@ -25,3 +25,6 @@ This def will enumerate through a passed in subsection and list all of the units
    + + + diff --git a/cms/urls.py b/cms/urls.py index 7cbb912b06..b57f894acb 100644 --- a/cms/urls.py +++ b/cms/urls.py @@ -11,7 +11,6 @@ urlpatterns = ('', url(r'^$', 'contentstore.views.index', name='index'), url(r'^edit/(?P.*?)$', 'contentstore.views.edit_unit', name='edit_unit'), url(r'^subsection/(?P.*?)$', 'contentstore.views.edit_subsection', name='edit_subsection'), - url(r'^delete/(?P.*?)$', 'contentstore.views.delete_unit', name='delete_unit'), url(r'^preview_component/(?P.*?)$', 'contentstore.views.preview_component', name='preview_component'), url(r'^save_item$', 'contentstore.views.save_item', name='save_item'), url(r'^delete_item$', 'contentstore.views.delete_item', name='delete_item'),