Code

Ticket #13743: comments_admin_with_tests.diff

File comments_admin_with_tests.diff, 2.8 KB (added by daniellindsley, 3 years ago)

Patch with tests.

Line 
1diff --git a/django/contrib/comments/admin.py b/django/contrib/comments/admin.py
2index 28678e0..4cb9066 100644
3--- a/django/contrib/comments/admin.py
4+++ b/django/contrib/comments/admin.py
5@@ -28,11 +28,13 @@ class CommentsAdmin(admin.ModelAdmin):
6     def get_actions(self, request):
7         actions = super(CommentsAdmin, self).get_actions(request)
8         # Only superusers should be able to delete the comments from the DB.
9-        if not request.user.is_superuser:
10+        if not request.user.is_superuser and 'delete_selected' in actions:
11             actions.pop('delete_selected')
12         if not request.user.has_perm('comments.can_moderate'):
13-            actions.pop('approve_comments')
14-            actions.pop('remove_comments')
15+            if 'approve_comments' in actions:
16+                actions.pop('approve_comments')
17+            if 'remove_comments' in actions:
18+                actions.pop('remove_comments')
19         return actions
20 
21     def flag_comments(self, request, queryset):
22diff --git a/tests/regressiontests/comment_tests/tests/moderation_view_tests.py b/tests/regressiontests/comment_tests/tests/moderation_view_tests.py
23index ef79b4d..65c9567 100644
24--- a/tests/regressiontests/comment_tests/tests/moderation_view_tests.py
25+++ b/tests/regressiontests/comment_tests/tests/moderation_view_tests.py
26@@ -190,3 +190,14 @@ class AdminActionsTests(CommentTestCase):
27         self.client.login(username="normaluser", password="normaluser")
28         response = self.client.get("/admin/comments/comment/")
29         self.assertEquals("approve_comments" in response.content, True)
30+   
31+    def testActionsDisabledDelete(self):
32+        "Tests a CommentAdmin where 'delete_selected' has been disabled."
33+        comments = self.createSomeComments()
34+        self.client.login(username="normaluser", password="normaluser")
35+        response = self.client.get('/admin2/comments/comment/')
36+        self.assertEqual(response.status_code, 200)
37+        self.assert_(
38+            '<option value="delete_selected">' not in response.content,
39+            "Found an unexpected delete_selected in response"
40+        )
41diff --git a/tests/regressiontests/comment_tests/urls_admin.py b/tests/regressiontests/comment_tests/urls_admin.py
42index 341285d..d7e1a4e 100644
43--- a/tests/regressiontests/comment_tests/urls_admin.py
44+++ b/tests/regressiontests/comment_tests/urls_admin.py
45@@ -8,6 +8,12 @@ from django.contrib.comments.models import Comment
46 admin_site = admin.AdminSite()
47 admin_site.register(Comment, CommentsAdmin)
48 
49+# To demonstrate proper functionality even when ``delete_selected`` is removed.
50+admin_site2 = admin.AdminSite()
51+admin_site2.disable_action('delete_selected')
52+admin_site2.register(Comment, CommentsAdmin)
53+
54 urlpatterns = patterns('',
55     (r'^admin/', include(admin_site.urls)),
56+    (r'^admin2/', include(admin_site2.urls)),
57 )