Fixed #19704 -- Make use of new ungettext_lazy function at appropriate places

This commit is contained in:
Alexey Boriskin 2013-02-02 00:22:20 +04:00 committed by Claude Paroz
parent d7504a3d7b
commit d18f796a48
3 changed files with 46 additions and 20 deletions

View File

@ -3,7 +3,7 @@ from __future__ import unicode_literals
from django.contrib import admin from django.contrib import admin
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.comments.models import Comment from django.contrib.comments.models import Comment
from django.utils.translation import ugettext_lazy as _, ungettext from django.utils.translation import ugettext_lazy as _, ungettext, ungettext_lazy
from django.contrib.comments import get_model from django.contrib.comments import get_model
from django.contrib.comments.views.moderation import perform_flag, perform_approve, perform_delete from django.contrib.comments.views.moderation import perform_flag, perform_approve, perform_delete
@ -52,17 +52,20 @@ class CommentsAdmin(admin.ModelAdmin):
def flag_comments(self, request, queryset): def flag_comments(self, request, queryset):
self._bulk_flag(request, queryset, perform_flag, self._bulk_flag(request, queryset, perform_flag,
lambda n: ungettext('flagged', 'flagged', n)) ungettext_lazy('%d comment was successfully flagged',
'%d comments were successfully flagged'))
flag_comments.short_description = _("Flag selected comments") flag_comments.short_description = _("Flag selected comments")
def approve_comments(self, request, queryset): def approve_comments(self, request, queryset):
self._bulk_flag(request, queryset, perform_approve, self._bulk_flag(request, queryset, perform_approve,
lambda n: ungettext('approved', 'approved', n)) ungettext_lazy('%d comment was successfully approved',
'%d comments were successfully approved'))
approve_comments.short_description = _("Approve selected comments") approve_comments.short_description = _("Approve selected comments")
def remove_comments(self, request, queryset): def remove_comments(self, request, queryset):
self._bulk_flag(request, queryset, perform_delete, self._bulk_flag(request, queryset, perform_delete,
lambda n: ungettext('removed', 'removed', n)) ungettext_lazy('%d comment was successfully removed',
'%d comments were successfully removed'))
remove_comments.short_description = _("Remove selected comments") remove_comments.short_description = _("Remove selected comments")
def _bulk_flag(self, request, queryset, action, done_message): def _bulk_flag(self, request, queryset, action, done_message):
@ -75,10 +78,7 @@ class CommentsAdmin(admin.ModelAdmin):
action(request, comment) action(request, comment)
n_comments += 1 n_comments += 1
msg = ungettext('1 comment was successfully %(action)s.', self.message_user(request, done_message % n_comments)
'%(count)s comments were successfully %(action)s.',
n_comments)
self.message_user(request, msg % {'count': n_comments, 'action': done_message(n_comments)})
# Only register the default admin if the model is the built-in comment model # Only register the default admin if the model is the built-in comment model
# (this won't be true if there's a custom comment app). # (this won't be true if there's a custom comment app).

View File

@ -3,7 +3,7 @@ from __future__ import unicode_literals
import datetime import datetime
from django.utils.timezone import is_aware, utc from django.utils.timezone import is_aware, utc
from django.utils.translation import ungettext, ugettext from django.utils.translation import ugettext, ungettext_lazy
def timesince(d, now=None, reversed=False): def timesince(d, now=None, reversed=False):
""" """
@ -19,12 +19,12 @@ def timesince(d, now=None, reversed=False):
Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
""" """
chunks = ( chunks = (
(60 * 60 * 24 * 365, lambda n: ungettext('year', 'years', n)), (60 * 60 * 24 * 365, ungettext_lazy('%d year', '%d years')),
(60 * 60 * 24 * 30, lambda n: ungettext('month', 'months', n)), (60 * 60 * 24 * 30, ungettext_lazy('%d month', '%d months')),
(60 * 60 * 24 * 7, lambda n : ungettext('week', 'weeks', n)), (60 * 60 * 24 * 7, ungettext_lazy('%d week', '%d weeks')),
(60 * 60 * 24, lambda n : ungettext('day', 'days', n)), (60 * 60 * 24, ungettext_lazy('%d day', '%d days')),
(60 * 60, lambda n: ungettext('hour', 'hours', n)), (60 * 60, ungettext_lazy('%d hour', '%d hours')),
(60, lambda n: ungettext('minute', 'minutes', n)) (60, ungettext_lazy('%d minute', '%d minutes'))
) )
# Convert datetime.date to datetime.datetime for comparison. # Convert datetime.date to datetime.datetime for comparison.
if not isinstance(d, datetime.datetime): if not isinstance(d, datetime.datetime):
@ -40,19 +40,19 @@ def timesince(d, now=None, reversed=False):
since = delta.days * 24 * 60 * 60 + delta.seconds since = delta.days * 24 * 60 * 60 + delta.seconds
if since <= 0: if since <= 0:
# d is in the future compared to now, stop processing. # d is in the future compared to now, stop processing.
return '0 ' + ugettext('minutes') return ugettext('0 minutes')
for i, (seconds, name) in enumerate(chunks): for i, (seconds, name) in enumerate(chunks):
count = since // seconds count = since // seconds
if count != 0: if count != 0:
break break
s = ugettext('%(number)d %(type)s') % {'number': count, 'type': name(count)} result = name % count
if i + 1 < len(chunks): if i + 1 < len(chunks):
# Now get the second item # Now get the second item
seconds2, name2 = chunks[i + 1] seconds2, name2 = chunks[i + 1]
count2 = (since - (seconds * count)) // seconds2 count2 = (since - (seconds * count)) // seconds2
if count2 != 0: if count2 != 0:
s += ugettext(', %(number)d %(type)s') % {'number': count2, 'type': name2(count2)} result += ugettext(', ') + name2 % count2
return s return result
def timeuntil(d, now=None): def timeuntil(d, now=None):
""" """

View File

@ -1,9 +1,10 @@
from __future__ import absolute_import from __future__ import absolute_import, unicode_literals
from django.contrib.auth.models import User, Permission from django.contrib.auth.models import User, Permission
from django.contrib.comments import signals from django.contrib.comments import signals
from django.contrib.comments.models import Comment, CommentFlag from django.contrib.comments.models import Comment, CommentFlag
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.utils import translation
from . import CommentTestCase from . import CommentTestCase
@ -281,3 +282,28 @@ class AdminActionsTests(CommentTestCase):
response = self.client.get('/admin2/comments/comment/') response = self.client.get('/admin2/comments/comment/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertNotContains(response, '<option value="delete_selected">') self.assertNotContains(response, '<option value="delete_selected">')
def performActionAndCheckMessage(self, action, action_params, expected_message):
response = self.client.post('/admin/comments/comment/',
data={'_selected_action': action_params,
'action': action,
'index': 0},
follow=True)
self.assertContains(response, expected_message)
def testActionsMessageTranslations(self):
c1, c2, c3, c4 = self.createSomeComments()
one_comment = c1.pk
many_comments = [c2.pk, c3.pk, c4.pk]
makeModerator("normaluser")
self.client.login(username="normaluser", password="normaluser")
with translation.override('en'):
#Test approving
self.performActionAndCheckMessage('approve_comments', one_comment, '1 comment was successfully approved')
self.performActionAndCheckMessage('approve_comments', many_comments, '3 comments were successfully approved')
#Test flagging
self.performActionAndCheckMessage('flag_comments', one_comment, '1 comment was successfully flagged')
self.performActionAndCheckMessage('flag_comments', many_comments, '3 comments were successfully flagged')
#Test removing
self.performActionAndCheckMessage('remove_comments', one_comment, '1 comment was successfully removed')
self.performActionAndCheckMessage('remove_comments', many_comments, '3 comments were successfully removed')