2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.contrib import admin
|
2012-10-13 11:44:50 +08:00
|
|
|
from django.contrib.auth import get_user_model
|
2008-08-26 06:14:22 +08:00
|
|
|
from django.contrib.comments.models import Comment
|
2009-10-24 03:22:31 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _, ungettext
|
2009-02-24 06:16:26 +08:00
|
|
|
from django.contrib.comments import get_model
|
2009-10-24 03:22:31 +08:00
|
|
|
from django.contrib.comments.views.moderation import perform_flag, perform_approve, perform_delete
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2012-10-13 11:44:50 +08:00
|
|
|
|
|
|
|
class UsernameSearch(object):
|
|
|
|
"""The User object may not be auth.User, so we need to provide
|
|
|
|
a mechanism for issuing the equivalent of a .filter(user__username=...)
|
|
|
|
search in CommentAdmin.
|
|
|
|
"""
|
|
|
|
def __str__(self):
|
|
|
|
return 'user__%s' % get_user_model().USERNAME_FIELD
|
|
|
|
|
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
class CommentsAdmin(admin.ModelAdmin):
|
2008-07-19 07:54:34 +08:00
|
|
|
fieldsets = (
|
2008-08-26 06:14:22 +08:00
|
|
|
(None,
|
|
|
|
{'fields': ('content_type', 'object_pk', 'site')}
|
|
|
|
),
|
|
|
|
(_('Content'),
|
|
|
|
{'fields': ('user', 'user_name', 'user_email', 'user_url', 'comment')}
|
|
|
|
),
|
|
|
|
(_('Metadata'),
|
|
|
|
{'fields': ('submit_date', 'ip_address', 'is_public', 'is_removed')}
|
|
|
|
),
|
|
|
|
)
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2008-09-16 08:25:21 +08:00
|
|
|
list_display = ('name', 'content_type', 'object_pk', 'ip_address', 'submit_date', 'is_public', 'is_removed')
|
2008-08-26 06:14:22 +08:00
|
|
|
list_filter = ('submit_date', 'site', 'is_public', 'is_removed')
|
2008-07-19 07:54:34 +08:00
|
|
|
date_hierarchy = 'submit_date'
|
2008-09-16 08:25:21 +08:00
|
|
|
ordering = ('-submit_date',)
|
2009-10-08 21:30:31 +08:00
|
|
|
raw_id_fields = ('user',)
|
2012-10-13 11:44:50 +08:00
|
|
|
search_fields = ('comment', UsernameSearch(), 'user_name', 'user_email', 'user_url', 'ip_address')
|
2009-10-24 03:22:31 +08:00
|
|
|
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.
|
2010-12-21 22:56:48 +08:00
|
|
|
if not request.user.is_superuser and 'delete_selected' in actions:
|
2009-10-24 03:22:31 +08:00
|
|
|
actions.pop('delete_selected')
|
|
|
|
if not request.user.has_perm('comments.can_moderate'):
|
2010-12-21 22:56:48 +08:00
|
|
|
if 'approve_comments' in actions:
|
|
|
|
actions.pop('approve_comments')
|
|
|
|
if 'remove_comments' in actions:
|
|
|
|
actions.pop('remove_comments')
|
2009-10-24 03:22:31 +08:00
|
|
|
return actions
|
|
|
|
|
|
|
|
def flag_comments(self, request, queryset):
|
2010-03-12 23:32:06 +08:00
|
|
|
self._bulk_flag(request, queryset, perform_flag,
|
|
|
|
lambda n: ungettext('flagged', 'flagged', n))
|
2009-10-24 03:22:31 +08:00
|
|
|
flag_comments.short_description = _("Flag selected comments")
|
|
|
|
|
|
|
|
def approve_comments(self, request, queryset):
|
2010-03-12 23:32:06 +08:00
|
|
|
self._bulk_flag(request, queryset, perform_approve,
|
|
|
|
lambda n: ungettext('approved', 'approved', n))
|
2009-10-24 03:22:31 +08:00
|
|
|
approve_comments.short_description = _("Approve selected comments")
|
|
|
|
|
|
|
|
def remove_comments(self, request, queryset):
|
2010-03-12 23:32:06 +08:00
|
|
|
self._bulk_flag(request, queryset, perform_delete,
|
|
|
|
lambda n: ungettext('removed', 'removed', n))
|
2009-10-24 03:22:31 +08:00
|
|
|
remove_comments.short_description = _("Remove selected comments")
|
|
|
|
|
2010-03-12 23:32:06 +08:00
|
|
|
def _bulk_flag(self, request, queryset, action, done_message):
|
2009-10-24 03:22:31 +08:00
|
|
|
"""
|
|
|
|
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
|
2010-03-12 23:32:06 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
msg = ungettext('1 comment was successfully %(action)s.',
|
|
|
|
'%(count)s comments were successfully %(action)s.',
|
2009-10-24 03:22:31 +08:00
|
|
|
n_comments)
|
2010-03-12 23:32:06 +08:00
|
|
|
self.message_user(request, msg % {'count': n_comments, 'action': done_message(n_comments)})
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2009-02-24 06:16:26 +08:00
|
|
|
# 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).
|
|
|
|
if get_model() is Comment:
|
|
|
|
admin.site.register(Comment, CommentsAdmin)
|