1 from django.contrib import admin |
1 from django.contrib import admin |
2 from django.contrib.comments.models import Comment |
2 from django.contrib.comments.models import Comment |
3 from django.utils.translation import ugettext_lazy as _ |
3 from django.utils.translation import ugettext_lazy as _, ungettext |
4 from django.contrib.comments import get_model |
4 from django.contrib.comments import get_model |
|
5 from django.contrib.comments.views.moderation import perform_flag, perform_approve, perform_delete |
5 |
6 |
6 class CommentsAdmin(admin.ModelAdmin): |
7 class CommentsAdmin(admin.ModelAdmin): |
7 fieldsets = ( |
8 fieldsets = ( |
8 (None, |
9 (None, |
9 {'fields': ('content_type', 'object_pk', 'site')} |
10 {'fields': ('content_type', 'object_pk', 'site')} |
20 list_filter = ('submit_date', 'site', 'is_public', 'is_removed') |
21 list_filter = ('submit_date', 'site', 'is_public', 'is_removed') |
21 date_hierarchy = 'submit_date' |
22 date_hierarchy = 'submit_date' |
22 ordering = ('-submit_date',) |
23 ordering = ('-submit_date',) |
23 raw_id_fields = ('user',) |
24 raw_id_fields = ('user',) |
24 search_fields = ('comment', 'user__username', 'user_name', 'user_email', 'user_url', 'ip_address') |
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)}) |
25 |
67 |
26 # Only register the default admin if the model is the built-in comment model |
68 # Only register the default admin if the model is the built-in comment model |
27 # (this won't be true if there's a custom comment app). |
69 # (this won't be true if there's a custom comment app). |
28 if get_model() is Comment: |
70 if get_model() is Comment: |
29 admin.site.register(Comment, CommentsAdmin) |
71 admin.site.register(Comment, CommentsAdmin) |