开发者

Django comments needs a Delete Action for non-superusers

I recently upgraded a large Django install from 1.1 to 1.3. In the Comments app, they added a caveat so only Superusers get the Delete Action.

The moderators, who have permissions to Delete, as a result don't see those Actions. This is really inconvenient for them.

The code in question is in contrib.comments.admin starting on line 28:

def get_actions(self, request):
    actions = super(CommentsAdmin, self).get_actions(request)
    # Only superusers should be able to delete the comments from the DB.
    if not request.user.is_superuser and 'delete_selected' in actions:
        actions.pop('delete_selected')

It should instead ask if request.u开发者_如何学Goser has delete permissions.

How can I override this without jacking with the actual Django install?

(And if anyone knows why this was changed, I'd be interested to know.)


In the comments app, there is a "Remove selected comments" action. When you apply the this action, it 'marks' the comment as deleted instead of deleting from the database -- it creates a deleted flag for the comment and sets comment.is_removed = True.

I recommend that you give your moderators the comments.can_moderate permission, and remove comments in that way. If you really want your moderators to be able to delete comments, you could do the following:

  1. subclass the CommentsAdmin in admin.py
  2. override the get_actions method
  3. unregister the CommentsAdmin ModelAdmin, then register your subclass.

To do this, put the following code in one of your apps.

# myapp.admin.py
# The app should come after `django.contrib.comments` 
# in your INSTALLED_APPS tuple

from django.contrib.comments.admin import CommentsAdmin

class MyCommentsAdmin(CommentsAdmin):
    def get_actions(self, request):
        actions = super(MyCommentsAdmin, self).get_actions(request)
        if not request.user.has_perm('comments.can_moderate'):
            if 'approve_comments' in actions:
                actions.pop('approve_comments')
            if 'remove_comments' in actions:
                actions.pop('remove_comments')
        return actions


admin.site.unregister(CommentsAdmin)
admin.site.register(MyCommentsAdmin)


def has_add_permission(self, request):
    return False

def has_delete_permission(self, request, obj=None):
    return False

def get_actions(self, request):
    actions = []
    return actions

This code is disabled delete and add actions. Also remove actions menu.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜