2008-07-19 07:54:34 +08:00
|
|
|
from django.contrib import admin
|
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
|
|
|
|
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',)
|
2008-08-26 06:14:22 +08:00
|
|
|
search_fields = ('comment', 'user__username', '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.
|
|
|
|
if not request.user.is_superuser:
|
|
|
|
actions.pop('delete_selected')
|
|
|
|
if not request.user.has_perm('comments.can_moderate'):
|
|
|
|
actions.pop('approve_comments')
|
|
|
|
actions.pop('remove_comments')
|
|
|
|
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
|
|
|
|
2009-10-24 03:22:31 +08:00
|
|
|
msg = ungettext(u'1 comment was successfully %(action)s.',
|
|
|
|
u'%(count)s comments were successfully %(action)s.',
|
|
|
|
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)
|