From e9c70633c6eedc02ae50cfb054e709176d8efd07 Mon Sep 17 00:00:00 2001 From: Don Mitchell Date: Tue, 8 Oct 2013 11:43:20 -0400 Subject: [PATCH] Add delete if staff member --- cms/djangoapps/contentstore/tests/test_orphan.py | 14 ++++++++++++++ cms/djangoapps/contentstore/views/item.py | 8 +++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/cms/djangoapps/contentstore/tests/test_orphan.py b/cms/djangoapps/contentstore/tests/test_orphan.py index c6f16fd47b..566cc0cf56 100644 --- a/cms/djangoapps/contentstore/tests/test_orphan.py +++ b/cms/djangoapps/contentstore/tests/test_orphan.py @@ -56,3 +56,17 @@ class TestOrphan(CourseTestCase): self.assertIn(location.url(), orphans) location = self.course.location.replace(category='html', name='OrphanHtml') self.assertIn(location.url(), orphans) + + def test_mongo_orphan_delete(self): + """ + Test that old mongo deletes the orphans + """ + url = reverse( + 'orphan', + kwargs={'course_id': '{}.{}'.format(self.course.location.org, self.course.location.course)} + ) + self.client.delete(url) + orphans = json.loads( + self.client.get(url, HTTP_ACCEPT='application/json').content + ) + self.assertEqual(len(orphans), 0, "Orphans not deleted {}".format(orphans)) diff --git a/cms/djangoapps/contentstore/views/item.py b/cms/djangoapps/contentstore/views/item.py index 816b05c16b..273cc6ebd9 100644 --- a/cms/djangoapps/contentstore/views/item.py +++ b/cms/djangoapps/contentstore/views/item.py @@ -20,6 +20,7 @@ from ..utils import get_modulestore from .access import has_access from .helpers import _xmodule_recurse from xmodule.x_module import XModuleDescriptor +from django.views.decorators.http import require_http_methods __all__ = ['save_item', 'create_item', 'delete_item', 'orphan'] @@ -203,6 +204,7 @@ def delete_item(request): @login_required +@require_http_methods(("GET", "DELETE")) def orphan(request, course_id): """ View for handling orphan related requests. A get gets all of the current orphans. @@ -214,6 +216,10 @@ def orphan(request, course_id): :param request: :param course_id: Locator syntax course_id """ - # dhm: I'd add DELETE but I'm not sure what type of authentication/authorization we'd need if request.method == 'GET': return JsonResponse(modulestore().get_orphans(course_id, DETACHED_CATEGORIES, 'draft')) + if request.method == 'DELETE' and request.user.is_staff: + items = modulestore().get_orphans(course_id, DETACHED_CATEGORIES, 'draft') + for item in items: + modulestore('draft').delete_item(item, True) + return JsonResponse({'deleted': items})