Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.user 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.)

like image 614
Jason Goldstein Avatar asked Nov 24 '25 19:11

Jason Goldstein


1 Answers

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)
like image 109
Alasdair Avatar answered Nov 26 '25 21:11

Alasdair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!