Fixed #13743 -- Fixed CommentsAdmin to not blow up if the delete_selected action is disabled. Thanks, Daniel Lindsley.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14996 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-12-21 14:56:48 +00:00
parent e299ac0cae
commit 7655cd8eac
3 changed files with 22 additions and 3 deletions

View File

@ -28,11 +28,13 @@ class CommentsAdmin(admin.ModelAdmin):
def get_actions(self, request): def get_actions(self, request):
actions = super(CommentsAdmin, self).get_actions(request) actions = super(CommentsAdmin, self).get_actions(request)
# Only superusers should be able to delete the comments from the DB. # Only superusers should be able to delete the comments from the DB.
if not request.user.is_superuser: if not request.user.is_superuser and 'delete_selected' in actions:
actions.pop('delete_selected') actions.pop('delete_selected')
if not request.user.has_perm('comments.can_moderate'): if not request.user.has_perm('comments.can_moderate'):
actions.pop('approve_comments') if 'approve_comments' in actions:
actions.pop('remove_comments') actions.pop('approve_comments')
if 'remove_comments' in actions:
actions.pop('remove_comments')
return actions return actions
def flag_comments(self, request, queryset): def flag_comments(self, request, queryset):

View File

@ -190,3 +190,14 @@ class AdminActionsTests(CommentTestCase):
self.client.login(username="normaluser", password="normaluser") self.client.login(username="normaluser", password="normaluser")
response = self.client.get("/admin/comments/comment/") response = self.client.get("/admin/comments/comment/")
self.assertEquals("approve_comments" in response.content, True) self.assertEquals("approve_comments" in response.content, True)
def testActionsDisabledDelete(self):
"Tests a CommentAdmin where 'delete_selected' has been disabled."
comments = self.createSomeComments()
self.client.login(username="normaluser", password="normaluser")
response = self.client.get('/admin2/comments/comment/')
self.assertEqual(response.status_code, 200)
self.assert_(
'<option value="delete_selected">' not in response.content,
"Found an unexpected delete_selected in response"
)

View File

@ -8,6 +8,12 @@ from django.contrib.comments.models import Comment
admin_site = admin.AdminSite() admin_site = admin.AdminSite()
admin_site.register(Comment, CommentsAdmin) admin_site.register(Comment, CommentsAdmin)
# To demonstrate proper functionality even when ``delete_selected`` is removed.
admin_site2 = admin.AdminSite()
admin_site2.disable_action('delete_selected')
admin_site2.register(Comment, CommentsAdmin)
urlpatterns = patterns('', urlpatterns = patterns('',
(r'^admin/', include(admin_site.urls)), (r'^admin/', include(admin_site.urls)),
(r'^admin2/', include(admin_site2.urls)),
) )