Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #19704 -- Make use of new ungettext_lazy function at appropriat…

…e places
  • Loading branch information...
commit d18f796a481e79a3800d4672d6189e4c496cce3d 1 parent d7504a3
Alexey Boriskin authored claudep committed
16  django/contrib/comments/admin.py
@@ -3,7 +3,7 @@
3 3
 from django.contrib import admin
4 4
 from django.contrib.auth import get_user_model
5 5
 from django.contrib.comments.models import Comment
6  
-from django.utils.translation import ugettext_lazy as _, ungettext
  6
+from django.utils.translation import ugettext_lazy as _, ungettext, ungettext_lazy
7 7
 from django.contrib.comments import get_model
8 8
 from django.contrib.comments.views.moderation import perform_flag, perform_approve, perform_delete
9 9
 
@@ -52,17 +52,20 @@ def get_actions(self, request):
52 52
 
53 53
     def flag_comments(self, request, queryset):
54 54
         self._bulk_flag(request, queryset, perform_flag,
55  
-                        lambda n: ungettext('flagged', 'flagged', n))
  55
+                        ungettext_lazy('%d comment was successfully flagged',
  56
+                                       '%d comments were successfully flagged'))
56 57
     flag_comments.short_description = _("Flag selected comments")
57 58
 
58 59
     def approve_comments(self, request, queryset):
59 60
         self._bulk_flag(request, queryset, perform_approve,
60  
-                        lambda n: ungettext('approved', 'approved', n))
  61
+                        ungettext_lazy('%d comment was successfully approved',
  62
+                                       '%d comments were successfully approved'))
61 63
     approve_comments.short_description = _("Approve selected comments")
62 64
 
63 65
     def remove_comments(self, request, queryset):
64 66
         self._bulk_flag(request, queryset, perform_delete,
65  
-                        lambda n: ungettext('removed', 'removed', n))
  67
+                        ungettext_lazy('%d comment was successfully removed',
  68
+                                       '%d comments were successfully removed'))
66 69
     remove_comments.short_description = _("Remove selected comments")
67 70
 
68 71
     def _bulk_flag(self, request, queryset, action, done_message):
@@ -75,10 +78,7 @@ def _bulk_flag(self, request, queryset, action, done_message):
75 78
             action(request, comment)
76 79
             n_comments += 1
77 80
 
78  
-        msg = ungettext('1 comment was successfully %(action)s.',
79  
-                        '%(count)s comments were successfully %(action)s.',
80  
-                        n_comments)
81  
-        self.message_user(request, msg % {'count': n_comments, 'action': done_message(n_comments)})
  81
+        self.message_user(request, done_message % n_comments)
82 82
 
83 83
 # Only register the default admin if the model is the built-in comment model
84 84
 # (this won't be true if there's a custom comment app).
22  django/utils/timesince.py
@@ -3,7 +3,7 @@
3 3
 import datetime
4 4
 
5 5
 from django.utils.timezone import is_aware, utc
6  
-from django.utils.translation import ungettext, ugettext
  6
+from django.utils.translation import ugettext, ungettext_lazy
7 7
 
8 8
 def timesince(d, now=None, reversed=False):
9 9
     """
@@ -19,12 +19,12 @@ def timesince(d, now=None, reversed=False):
19 19
     Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
20 20
     """
21 21
     chunks = (
22  
-      (60 * 60 * 24 * 365, lambda n: ungettext('year', 'years', n)),
23  
-      (60 * 60 * 24 * 30, lambda n: ungettext('month', 'months', n)),
24  
-      (60 * 60 * 24 * 7, lambda n : ungettext('week', 'weeks', n)),
25  
-      (60 * 60 * 24, lambda n : ungettext('day', 'days', n)),
26  
-      (60 * 60, lambda n: ungettext('hour', 'hours', n)),
27  
-      (60, lambda n: ungettext('minute', 'minutes', n))
  22
+        (60 * 60 * 24 * 365, ungettext_lazy('%d year', '%d years')),
  23
+        (60 * 60 * 24 * 30, ungettext_lazy('%d month', '%d months')),
  24
+        (60 * 60 * 24 * 7, ungettext_lazy('%d week', '%d weeks')),
  25
+        (60 * 60 * 24, ungettext_lazy('%d day', '%d days')),
  26
+        (60 * 60, ungettext_lazy('%d hour', '%d hours')),
  27
+        (60, ungettext_lazy('%d minute', '%d minutes'))
28 28
     )
29 29
     # Convert datetime.date to datetime.datetime for comparison.
30 30
     if not isinstance(d, datetime.datetime):
@@ -40,19 +40,19 @@ def timesince(d, now=None, reversed=False):
40 40
     since = delta.days * 24 * 60 * 60 + delta.seconds
41 41
     if since <= 0:
42 42
         # d is in the future compared to now, stop processing.
43  
-        return '0 ' + ugettext('minutes')
  43
+        return ugettext('0 minutes')
44 44
     for i, (seconds, name) in enumerate(chunks):
45 45
         count = since // seconds
46 46
         if count != 0:
47 47
             break
48  
-    s = ugettext('%(number)d %(type)s') % {'number': count, 'type': name(count)}
  48
+    result = name % count
49 49
     if i + 1 < len(chunks):
50 50
         # Now get the second item
51 51
         seconds2, name2 = chunks[i + 1]
52 52
         count2 = (since - (seconds * count)) // seconds2
53 53
         if count2 != 0:
54  
-            s += ugettext(', %(number)d %(type)s') % {'number': count2, 'type': name2(count2)}
55  
-    return s
  54
+            result += ugettext(', ') + name2 % count2
  55
+    return result
56 56
 
57 57
 def timeuntil(d, now=None):
58 58
     """
28  tests/regressiontests/comment_tests/tests/moderation_view_tests.py
... ...
@@ -1,9 +1,10 @@
1  
-from __future__ import absolute_import
  1
+from __future__ import absolute_import, unicode_literals
2 2
 
3 3
 from django.contrib.auth.models import User, Permission
4 4
 from django.contrib.comments import signals
5 5
 from django.contrib.comments.models import Comment, CommentFlag
6 6
 from django.contrib.contenttypes.models import ContentType
  7
+from django.utils import translation
7 8
 
8 9
 from . import CommentTestCase
9 10
 
@@ -281,3 +282,28 @@ def testActionsDisabledDelete(self):
281 282
         response = self.client.get('/admin2/comments/comment/')
282 283
         self.assertEqual(response.status_code, 200)
283 284
         self.assertNotContains(response, '<option value="delete_selected">')
  285
+
  286
+    def performActionAndCheckMessage(self, action, action_params, expected_message):
  287
+        response = self.client.post('/admin/comments/comment/',
  288
+                                    data={'_selected_action': action_params,
  289
+                                          'action': action,
  290
+                                          'index': 0},
  291
+                                    follow=True)
  292
+        self.assertContains(response, expected_message)
  293
+
  294
+    def testActionsMessageTranslations(self):
  295
+        c1, c2, c3, c4 = self.createSomeComments()
  296
+        one_comment = c1.pk
  297
+        many_comments = [c2.pk, c3.pk, c4.pk]
  298
+        makeModerator("normaluser")
  299
+        self.client.login(username="normaluser", password="normaluser")
  300
+        with translation.override('en'):
  301
+            #Test approving
  302
+            self.performActionAndCheckMessage('approve_comments', one_comment, '1 comment was successfully approved')
  303
+            self.performActionAndCheckMessage('approve_comments', many_comments, '3 comments were successfully approved')
  304
+            #Test flagging
  305
+            self.performActionAndCheckMessage('flag_comments', one_comment, '1 comment was successfully flagged')
  306
+            self.performActionAndCheckMessage('flag_comments', many_comments, '3 comments were successfully flagged')
  307
+            #Test removing
  308
+            self.performActionAndCheckMessage('remove_comments', one_comment, '1 comment was successfully removed')
  309
+            self.performActionAndCheckMessage('remove_comments', many_comments, '3 comments were successfully removed')

0 notes on commit d18f796

Please sign in to comment.
Something went wrong with that request. Please try again.