web/lib/django/contrib/comments/admin.py
changeset 38 77b6da96e6f1
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 from django.contrib import admin
       
     2 from django.contrib.comments.models import Comment
       
     3 from django.utils.translation import ugettext_lazy as _, ungettext
       
     4 from django.contrib.comments import get_model
       
     5 from django.contrib.comments.views.moderation import perform_flag, perform_approve, perform_delete
       
     6 
       
     7 class CommentsAdmin(admin.ModelAdmin):
       
     8     fieldsets = (
       
     9         (None,
       
    10            {'fields': ('content_type', 'object_pk', 'site')}
       
    11         ),
       
    12         (_('Content'),
       
    13            {'fields': ('user', 'user_name', 'user_email', 'user_url', 'comment')}
       
    14         ),
       
    15         (_('Metadata'),
       
    16            {'fields': ('submit_date', 'ip_address', 'is_public', 'is_removed')}
       
    17         ),
       
    18      )
       
    19 
       
    20     list_display = ('name', 'content_type', 'object_pk', 'ip_address', 'submit_date', 'is_public', 'is_removed')
       
    21     list_filter = ('submit_date', 'site', 'is_public', 'is_removed')
       
    22     date_hierarchy = 'submit_date'
       
    23     ordering = ('-submit_date',)
       
    24     raw_id_fields = ('user',)
       
    25     search_fields = ('comment', 'user__username', 'user_name', 'user_email', 'user_url', 'ip_address')
       
    26     actions = ["flag_comments", "approve_comments", "remove_comments"]
       
    27 
       
    28     def get_actions(self, request):
       
    29         actions = super(CommentsAdmin, self).get_actions(request)
       
    30         # Only superusers should be able to delete the comments from the DB.
       
    31         if not request.user.is_superuser:
       
    32             actions.pop('delete_selected')
       
    33         if not request.user.has_perm('comments.can_moderate'):
       
    34             actions.pop('approve_comments')
       
    35             actions.pop('remove_comments')
       
    36         return actions
       
    37 
       
    38     def flag_comments(self, request, queryset):
       
    39         self._bulk_flag(request, queryset, perform_flag,
       
    40                         lambda n: ungettext('flagged', 'flagged', n))
       
    41     flag_comments.short_description = _("Flag selected comments")
       
    42 
       
    43     def approve_comments(self, request, queryset):
       
    44         self._bulk_flag(request, queryset, perform_approve,
       
    45                         lambda n: ungettext('approved', 'approved', n))
       
    46     approve_comments.short_description = _("Approve selected comments")
       
    47 
       
    48     def remove_comments(self, request, queryset):
       
    49         self._bulk_flag(request, queryset, perform_delete,
       
    50                         lambda n: ungettext('removed', 'removed', n))
       
    51     remove_comments.short_description = _("Remove selected comments")
       
    52 
       
    53     def _bulk_flag(self, request, queryset, action, done_message):
       
    54         """
       
    55         Flag, approve, or remove some comments from an admin action. Actually
       
    56         calls the `action` argument to perform the heavy lifting.
       
    57         """
       
    58         n_comments = 0
       
    59         for comment in queryset:
       
    60             action(request, comment)
       
    61             n_comments += 1
       
    62 
       
    63         msg = ungettext(u'1 comment was successfully %(action)s.',
       
    64                         u'%(count)s comments were successfully %(action)s.',
       
    65                         n_comments)
       
    66         self.message_user(request, msg % {'count': n_comments, 'action': done_message(n_comments)})
       
    67 
       
    68 # Only register the default admin if the model is the built-in comment model
       
    69 # (this won't be true if there's a custom comment app).
       
    70 if get_model() is Comment:
       
    71     admin.site.register(Comment, CommentsAdmin)