admin.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from __future__ import unicode_literals
  2. from django.contrib import admin
  3. from django.contrib.auth import get_user_model
  4. from django.contrib.comments.models import Comment
  5. from django.utils.translation import ugettext_lazy as _, ungettext_lazy
  6. from django.contrib.comments import get_model
  7. from django.contrib.comments.views.moderation import perform_flag, perform_approve, perform_delete
  8. class UsernameSearch(object):
  9. """The User object may not be auth.User, so we need to provide
  10. a mechanism for issuing the equivalent of a .filter(user__username=...)
  11. search in CommentAdmin.
  12. """
  13. def __str__(self):
  14. return 'user__%s' % get_user_model().USERNAME_FIELD
  15. class CommentsAdmin(admin.ModelAdmin):
  16. fieldsets = (
  17. (None,
  18. {'fields': ('content_type', 'object_pk', 'site')}
  19. ),
  20. (_('Content'),
  21. {'fields': ('user', 'user_name', 'user_email', 'user_url', 'comment')}
  22. ),
  23. (_('Metadata'),
  24. {'fields': ('submit_date', 'ip_address', 'is_public', 'is_removed')}
  25. ),
  26. )
  27. list_display = ('name', 'content_type', 'object_pk', 'ip_address', 'submit_date', 'is_public', 'is_removed')
  28. list_filter = ('submit_date', 'site', 'is_public', 'is_removed')
  29. date_hierarchy = 'submit_date'
  30. ordering = ('-submit_date',)
  31. raw_id_fields = ('user',)
  32. search_fields = ('comment', UsernameSearch(), 'user_name', 'user_email', 'user_url', 'ip_address')
  33. actions = ["flag_comments", "approve_comments", "remove_comments"]
  34. def get_actions(self, request):
  35. actions = super(CommentsAdmin, self).get_actions(request)
  36. # Only superusers should be able to delete the comments from the DB.
  37. if not request.user.is_superuser and 'delete_selected' in actions:
  38. actions.pop('delete_selected')
  39. if not request.user.has_perm('comments.can_moderate'):
  40. if 'approve_comments' in actions:
  41. actions.pop('approve_comments')
  42. if 'remove_comments' in actions:
  43. actions.pop('remove_comments')
  44. return actions
  45. def flag_comments(self, request, queryset):
  46. self._bulk_flag(request, queryset, perform_flag,
  47. ungettext_lazy('%d comment was successfully flagged',
  48. '%d comments were successfully flagged'))
  49. flag_comments.short_description = _("Flag selected comments")
  50. def approve_comments(self, request, queryset):
  51. self._bulk_flag(request, queryset, perform_approve,
  52. ungettext_lazy('%d comment was successfully approved',
  53. '%d comments were successfully approved'))
  54. approve_comments.short_description = _("Approve selected comments")
  55. def remove_comments(self, request, queryset):
  56. self._bulk_flag(request, queryset, perform_delete,
  57. ungettext_lazy('%d comment was successfully removed',
  58. '%d comments were successfully removed'))
  59. remove_comments.short_description = _("Remove selected comments")
  60. def _bulk_flag(self, request, queryset, action, done_message):
  61. """
  62. Flag, approve, or remove some comments from an admin action. Actually
  63. calls the `action` argument to perform the heavy lifting.
  64. """
  65. n_comments = 0
  66. for comment in queryset:
  67. action(request, comment)
  68. n_comments += 1
  69. self.message_user(request, 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)