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
7 class CommentsAdmin(admin
.ModelAdmin
):
10 {'fields': ('content_type', 'object_pk', 'site')}
13 {'fields': ('user', 'user_name', 'user_email', 'user_url', 'comment')}
16 {'fields': ('submit_date', 'ip_address', 'is_public', 'is_removed')}
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"]
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')
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")
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")
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")
55 def _bulk_flag(self
, request
, queryset
, action
, done_message
):
57 Flag, approve, or remove some comments from an admin action. Actually
58 calls the `action` argument to perform the heavy lifting.
61 for comment
in queryset
:
62 action(request
, comment
)
65 msg
= ungettext(u
'1 comment was successfully %(action)s.',
66 u
'%(count)s comments were successfully %(action)s.',
68 self
.message_user(request
, msg
% {'count': n_comments
, 'action': done_message(n_comments
)})
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).
72 if get_model() is Comment
:
73 admin
.site
.register(Comment
, CommentsAdmin
)