PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/comments/admin.py

https://code.google.com/p/mango-py/
Python | 73 lines | 57 code | 9 blank | 7 comment | 13 complexity | 12b8a6682cf28ac5cedaffd2c5da235a MD5 | raw file
 1from django.contrib import admin
 2from django.contrib.comments.models import Comment
 3from django.utils.translation import ugettext_lazy as _, ungettext
 4from django.contrib.comments import get_model
 5from django.contrib.comments.views.moderation import perform_flag, perform_approve, perform_delete
 6
 7class 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 and 'delete_selected' in actions:
32            actions.pop('delete_selected')
33        if not request.user.has_perm('comments.can_moderate'):
34            if 'approve_comments' in actions:
35                actions.pop('approve_comments')
36            if 'remove_comments' in actions:
37                actions.pop('remove_comments')
38        return actions
39
40    def flag_comments(self, request, queryset):
41        self._bulk_flag(request, queryset, perform_flag,
42                        lambda n: ungettext('flagged', 'flagged', n))
43    flag_comments.short_description = _("Flag selected comments")
44
45    def approve_comments(self, request, queryset):
46        self._bulk_flag(request, queryset, perform_approve,
47                        lambda n: ungettext('approved', 'approved', n))
48    approve_comments.short_description = _("Approve selected comments")
49
50    def remove_comments(self, request, queryset):
51        self._bulk_flag(request, queryset, perform_delete,
52                        lambda n: ungettext('removed', 'removed', n))
53    remove_comments.short_description = _("Remove selected comments")
54
55    def _bulk_flag(self, request, queryset, action, done_message):
56        """
57        Flag, approve, or remove some comments from an admin action. Actually
58        calls the `action` argument to perform the heavy lifting.
59        """
60        n_comments = 0
61        for comment in queryset:
62            action(request, comment)
63            n_comments += 1
64
65        msg = ungettext(u'1 comment was successfully %(action)s.',
66                        u'%(count)s comments were successfully %(action)s.',
67                        n_comments)
68        self.message_user(request, msg % {'count': n_comments, 'action': done_message(n_comments)})
69
70# Only register the default admin if the model is the built-in comment model
71# (this won't be true if there's a custom comment app).
72if get_model() is Comment:
73    admin.site.register(Comment, CommentsAdmin)