comments.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import xadmin
  2. from xadmin.layout import *
  3. from xadmin.util import username_field
  4. from django.conf import settings
  5. from django.contrib.comments.models import Comment
  6. from django.utils.translation import ugettext_lazy as _, ungettext
  7. from django.contrib.comments import get_model
  8. from django.contrib.comments.views.moderation import perform_flag, perform_approve, perform_delete
  9. class UsernameSearch(object):
  10. """The User object may not be auth.User, so we need to provide
  11. a mechanism for issuing the equivalent of a .filter(user__username=...)
  12. search in CommentAdmin.
  13. """
  14. def __str__(self):
  15. return 'user__%s' % username_field
  16. class CommentsAdmin(object):
  17. form_layout = (
  18. Main(
  19. Fieldset(None,
  20. 'content_type', 'object_pk', 'site',
  21. css_class='unsort no_title'
  22. ),
  23. Fieldset('Content',
  24. 'user', 'user_name', 'user_email', 'user_url', 'comment'
  25. ),
  26. ),
  27. Side(
  28. Fieldset(_('Metadata'),
  29. 'submit_date', 'ip_address', 'is_public', 'is_removed'
  30. ),
  31. )
  32. )
  33. list_display = ('name', 'content_type', 'object_pk', 'ip_address', 'submit_date', 'is_public', 'is_removed')
  34. list_filter = ('submit_date', 'site', 'is_public', 'is_removed')
  35. ordering = ('-submit_date',)
  36. search_fields = ('comment', UsernameSearch(), 'user_name', 'user_email', 'user_url', 'ip_address')
  37. actions = ["flag_comments", "approve_comments", "remove_comments"]
  38. model_icon = 'fa fa-comment'
  39. def get_actions(self):
  40. actions = super(CommentsAdmin, self).get_actions()
  41. # Only superusers should be able to delete the comments from the DB.
  42. if not self.user.is_superuser and 'delete_selected' in actions:
  43. actions.pop('delete_selected')
  44. if not self.user.has_perm('comments.can_moderate'):
  45. if 'approve_comments' in actions:
  46. actions.pop('approve_comments')
  47. if 'remove_comments' in actions:
  48. actions.pop('remove_comments')
  49. return actions
  50. def flag_comments(self, request, queryset):
  51. self._bulk_flag(queryset, perform_flag,
  52. lambda n: ungettext('flagged', 'flagged', n))
  53. flag_comments.short_description = _("Flag selected comments")
  54. flag_comments.icon = 'flag'
  55. def approve_comments(self, request, queryset):
  56. self._bulk_flag(queryset, perform_approve,
  57. lambda n: ungettext('approved', 'approved', n))
  58. approve_comments.short_description = _("Approve selected comments")
  59. approve_comments.icon = 'ok'
  60. def remove_comments(self, request, queryset):
  61. self._bulk_flag(queryset, perform_delete,
  62. lambda n: ungettext('removed', 'removed', n))
  63. remove_comments.short_description = _("Remove selected comments")
  64. remove_comments.icon = 'remove-circle'
  65. def _bulk_flag(self, queryset, action, done_message):
  66. """
  67. Flag, approve, or remove some comments from an admin action. Actually
  68. calls the `action` argument to perform the heavy lifting.
  69. """
  70. n_comments = 0
  71. for comment in queryset:
  72. action(self.request, comment)
  73. n_comments += 1
  74. msg = ungettext('1 comment was successfully %(action)s.',
  75. '%(count)s comments were successfully %(action)s.',
  76. n_comments)
  77. self.message_user(msg % {'count': n_comments, 'action': done_message(n_comments)}, 'success')
  78. # Only register the default admin if the model is the built-in comment model
  79. # (this won't be true if there's a custom comment app).
  80. if 'django.contrib.comments' in settings.INSTALLED_APPS and (get_model() is Comment):
  81. xadmin.site.register(Comment, CommentsAdmin)