PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/comments/admin.py

https://code.google.com/p/mango-py/
Python | 73 lines | 57 code | 9 blank | 7 comment | 7 complexity | 12b8a6682cf28ac5cedaffd2c5da235a MD5 | raw file
Possible License(s): BSD-3-Clause
  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
  6. class CommentsAdmin(admin.ModelAdmin):
  7. fieldsets = (
  8. (None,
  9. {'fields': ('content_type', 'object_pk', 'site')}
  10. ),
  11. (_('Content'),
  12. {'fields': ('user', 'user_name', 'user_email', 'user_url', 'comment')}
  13. ),
  14. (_('Metadata'),
  15. {'fields': ('submit_date', 'ip_address', 'is_public', 'is_removed')}
  16. ),
  17. )
  18. list_display = ('name', 'content_type', 'object_pk', 'ip_address', 'submit_date', 'is_public', 'is_removed')
  19. list_filter = ('submit_date', 'site', 'is_public', 'is_removed')
  20. date_hierarchy = 'submit_date'
  21. ordering = ('-submit_date',)
  22. raw_id_fields = ('user',)
  23. search_fields = ('comment', 'user__username', 'user_name', 'user_email', 'user_url', 'ip_address')
  24. actions = ["flag_comments", "approve_comments", "remove_comments"]
  25. def get_actions(self, request):
  26. actions = super(CommentsAdmin, self).get_actions(request)
  27. # Only superusers should be able to delete the comments from the DB.
  28. if not request.user.is_superuser and 'delete_selected' in actions:
  29. actions.pop('delete_selected')
  30. if not request.user.has_perm('comments.can_moderate'):
  31. if 'approve_comments' in actions:
  32. actions.pop('approve_comments')
  33. if 'remove_comments' in actions:
  34. actions.pop('remove_comments')
  35. return actions
  36. def flag_comments(self, request, queryset):
  37. self._bulk_flag(request, queryset, perform_flag,
  38. lambda n: ungettext('flagged', 'flagged', n))
  39. flag_comments.short_description = _("Flag selected comments")
  40. def approve_comments(self, request, queryset):
  41. self._bulk_flag(request, queryset, perform_approve,
  42. lambda n: ungettext('approved', 'approved', n))
  43. approve_comments.short_description = _("Approve selected comments")
  44. def remove_comments(self, request, queryset):
  45. self._bulk_flag(request, queryset, perform_delete,
  46. lambda n: ungettext('removed', 'removed', n))
  47. remove_comments.short_description = _("Remove selected comments")
  48. def _bulk_flag(self, request, queryset, action, done_message):
  49. """
  50. Flag, approve, or remove some comments from an admin action. Actually
  51. calls the `action` argument to perform the heavy lifting.
  52. """
  53. n_comments = 0
  54. for comment in queryset:
  55. action(request, comment)
  56. n_comments += 1
  57. msg = ungettext(u'1 comment was successfully %(action)s.',
  58. u'%(count)s comments were successfully %(action)s.',
  59. n_comments)
  60. self.message_user(request, msg % {'count': n_comments, 'action': done_message(n_comments)})
  61. # Only register the default admin if the model is the built-in comment model
  62. # (this won't be true if there's a custom comment app).
  63. if get_model() is Comment:
  64. admin.site.register(Comment, CommentsAdmin)