web/lib/django/contrib/comments/admin.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
--- a/web/lib/django/contrib/comments/admin.py	Wed May 19 17:43:59 2010 +0200
+++ b/web/lib/django/contrib/comments/admin.py	Tue May 25 02:43:45 2010 +0200
@@ -1,7 +1,8 @@
 from django.contrib import admin
 from django.contrib.comments.models import Comment
-from django.utils.translation import ugettext_lazy as _
+from django.utils.translation import ugettext_lazy as _, ungettext
 from django.contrib.comments import get_model
+from django.contrib.comments.views.moderation import perform_flag, perform_approve, perform_delete
 
 class CommentsAdmin(admin.ModelAdmin):
     fieldsets = (
@@ -22,6 +23,47 @@
     ordering = ('-submit_date',)
     raw_id_fields = ('user',)
     search_fields = ('comment', 'user__username', 'user_name', 'user_email', 'user_url', 'ip_address')
+    actions = ["flag_comments", "approve_comments", "remove_comments"]
+
+    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:
+            actions.pop('delete_selected')
+        if not request.user.has_perm('comments.can_moderate'):
+            actions.pop('approve_comments')
+            actions.pop('remove_comments')
+        return actions
+
+    def flag_comments(self, request, queryset):
+        self._bulk_flag(request, queryset, perform_flag,
+                        lambda n: ungettext('flagged', 'flagged', n))
+    flag_comments.short_description = _("Flag selected comments")
+
+    def approve_comments(self, request, queryset):
+        self._bulk_flag(request, queryset, perform_approve,
+                        lambda n: ungettext('approved', 'approved', n))
+    approve_comments.short_description = _("Approve selected comments")
+
+    def remove_comments(self, request, queryset):
+        self._bulk_flag(request, queryset, perform_delete,
+                        lambda n: ungettext('removed', 'removed', n))
+    remove_comments.short_description = _("Remove selected comments")
+
+    def _bulk_flag(self, request, queryset, action, done_message):
+        """
+        Flag, approve, or remove some comments from an admin action. Actually
+        calls the `action` argument to perform the heavy lifting.
+        """
+        n_comments = 0
+        for comment in queryset:
+            action(request, comment)
+            n_comments += 1
+
+        msg = ungettext(u'1 comment was successfully %(action)s.',
+                        u'%(count)s comments were successfully %(action)s.',
+                        n_comments)
+        self.message_user(request, msg % {'count': n_comments, 'action': done_message(n_comments)})
 
 # Only register the default admin if the model is the built-in comment model
 # (this won't be true if there's a custom comment app).