Ticket #11625: comment_mod_actions.2.diff

File comment_mod_actions.2.diff, 7.1 KB (added by Waylan Limberg, 15 years ago)

initial patch with tests

  • django/contrib/comments/admin.py

    diff --git a/django/contrib/comments/admin.py b/django/contrib/comments/admin.py
    index 3b1fb14..fda2651 100644
    a b  
    11from django.contrib import admin
    2 from django.contrib.comments.models import Comment
     2from django.contrib.comments.models import Comment, CommentFlag
    33from django.utils.translation import ugettext_lazy as _
    4 from django.contrib.comments import get_model
     4from django.contrib.comments import get_model, signals
    55
    66class CommentsAdmin(admin.ModelAdmin):
    77    fieldsets = (
    class CommentsAdmin(admin.ModelAdmin):  
    2121    date_hierarchy = 'submit_date'
    2222    ordering = ('-submit_date',)
    2323    search_fields = ('comment', 'user__username', 'user_name', 'user_email', 'user_url', 'ip_address')
     24    actions = ['approve', 'remove']
     25
     26    def approve(self, request, queryset):
     27        """ Approve comments through moderation. """
     28        cnt = 0
     29        for comment in queryset:
     30            # Flag the comment as approved.
     31            flag, created = CommentFlag.objects.get_or_create(
     32                comment = comment,
     33                user    = request.user,
     34                flag    = CommentFlag.MODERATOR_APPROVAL
     35            )
     36
     37            comment.is_removed = False
     38            comment.is_public = True
     39            comment.save()
     40
     41            signals.comment_was_flagged.send(
     42                sender  = comment.__class__,
     43                comment = comment,
     44                flag    = flag,
     45                created = created,
     46                request = request
     47            )
     48            cnt += 1
     49        self.message_user(request, '%s successfully approved.' % self._get_message_bit(cnt))
     50    approve.short_description = 'Approve selected comments'
     51
     52    def remove(self, request, queryset):
     53        """ Remove comments through moderation. """
     54        cnt = 0
     55        for comment in queryset:
     56            # Flag the comment as removed.
     57            flag, created = CommentFlag.objects.get_or_create(
     58                comment = comment,
     59                user    = request.user,
     60                flag    = CommentFlag.MODERATOR_DELETION
     61            )
     62
     63            comment.is_removed = True
     64            comment.save()
     65
     66            signals.comment_was_flagged.send(
     67                sender  = comment.__class__,
     68                comment = comment,
     69                flag    = flag,
     70                created = created,
     71                request = request
     72            )
     73            cnt += 1
     74        self.message_user(request, '%s successfully removed.' % self._get_message_bit(cnt))
     75    remove.short_description = 'Remove selected comments'
     76
     77    def _get_message_bit(self, rows_updated):
     78        if rows_updated == 1:
     79            return '1 comment was'
     80        else:
     81            return '%s comments were' % rows_updated
    2482
    2583# Only register the default admin if the model is the built-in comment model
    2684# (this won't be true if there's a custom comment app).
  • tests/regressiontests/comment_tests/tests/__init__.py

    diff --git a/tests/regressiontests/comment_tests/tests/__init__.py b/tests/regressiontests/comment_tests/tests/__init__.py
    index 449fea4..b2ead81 100644
    a b from regressiontests.comment_tests.tests.templatetag_tests import *  
    8787from regressiontests.comment_tests.tests.comment_view_tests import *
    8888from regressiontests.comment_tests.tests.moderation_view_tests import *
    8989from regressiontests.comment_tests.tests.comment_utils_moderators_tests import *
     90from regressiontests.comment_tests.tests.admin_moderation_tests import *
  • new file tests/regressiontests/comment_tests/tests/admin_moderation_tests.py

    diff --git a/tests/regressiontests/comment_tests/tests/admin_moderation_tests.py b/tests/regressiontests/comment_tests/tests/admin_moderation_tests.py
    new file mode 100644
    index 0000000..24a1c74
    - +  
     1from django.contrib.comments.models import Comment, CommentFlag
     2from regressiontests.comment_tests.tests import CommentTestCase
     3from django.contrib.comments import signals
     4from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
     5
     6class AdminModerationTests(CommentTestCase):
     7    fixtures = ['comment_tests', 'admin-views-users.xml']
     8
     9    def setUp(self):
     10        self.client.login(username='super', password='secret')
     11
     12    def tearDown(self):
     13        self.client.logout()
     14
     15    def test_comment_admin_approve_action(self):
     16        "Tests the builtin aprove action defined as an admin method."
     17        c1, c2, c3, c4 = self.createSomeComments()
     18        c1.is_public = False
     19        c1.is_removed = True
     20        c1.save()
     21        action_data = {
     22            ACTION_CHECKBOX_NAME: [1],
     23            'action' : 'approve',
     24            'index' : c1.pk,
     25        }
     26        response = self.client.post('/admin/comments/comment', action_data)
     27        c = Comment.objects.get(pk=c1.pk)
     28        self.assertTrue(c.is_public)
     29        self.assertFalse(c.is_removed)
     30        self.assertEqual(c.flags.filter(flag=CommentFlag.MODERATOR_APPROVAL, user__username="super").count(), 1)
     31
     32    def test_approve_action_signals(self):
     33        "Test signal is received upon aprove action."
     34        def receive(sender, **kwargs):
     35            received_signals.append(kwargs.get('signal'))
     36
     37        received_signals = []
     38        signals.comment_was_flagged.connect(receive)
     39
     40        self.test_comment_admin_approve_action()
     41        self.assertEqual(received_signals, [signals.comment_was_flagged])
     42   
     43    def test_comment_admin_remove_action(self):
     44        "Tests the builtin remove action defined as an admin method."
     45        c1, c2, c3, c4 = self.createSomeComments()
     46        action_data = {
     47            ACTION_CHECKBOX_NAME: [1],
     48            'action' : 'remove',
     49            'index' : c1.pk,
     50        }
     51        response = self.client.post('/admin/comments/comment/', action_data)
     52        c = Comment.objects.get(pk=c1.pk)
     53        self.assertTrue(c.is_removed)
     54        self.assertEqual(c.flags.filter(flag=CommentFlag.MODERATOR_DELETION, user__username="super").count(), 1)
     55
     56    def test_remove_action_signals(self):
     57        "Test signal is received upon remove action."
     58        def receive(sender, **kwargs):
     59            received_signals.append(kwargs.get('signal'))
     60
     61        received_signals = []
     62        signals.comment_was_flagged.connect(receive)
     63
     64        self.test_comment_admin_remove_action()
     65        self.assertEqual(received_signals, [signals.comment_was_flagged])
     66
  • tests/regressiontests/comment_tests/urls.py

    diff --git a/tests/regressiontests/comment_tests/urls.py b/tests/regressiontests/comment_tests/urls.py
    index 0058689..76ca4b9 100644
    a b  
    11from django.conf.urls.defaults import *
     2from django.contrib import admin
    23
    34urlpatterns = patterns('regressiontests.comment_tests.custom_comments.views',
    45    url(r'^post/$',          'custom_submit_comment'),
    urlpatterns = patterns('regressiontests.comment_tests.custom_comments.views',  
    78    url(r'^approve/(\d+)/$', 'custom_approve_comment'),
    89)
    910
     11urlpatterns += patterns('',
     12    url(r'^admin/', include(admin.site.urls)),
     13)
     14
Back to Top