Fixed #15287 -- Added translation markers to user-facing error messages in class-based generic views. Thanks to szczav for the report and draft patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15533 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2011-02-14 13:05:31 +00:00
parent bb26c328ec
commit 72e4a6865d
3 changed files with 30 additions and 17 deletions

View File

@ -3,11 +3,12 @@ import datetime
from django.db import models
from django.core.exceptions import ImproperlyConfigured
from django.http import Http404
from django.utils.encoding import force_unicode
from django.utils.translation import ugettext as _
from django.views.generic.base import View
from django.views.generic.detail import BaseDetailView, SingleObjectTemplateResponseMixin
from django.views.generic.list import MultipleObjectMixin, MultipleObjectTemplateResponseMixin
class YearMixin(object):
year_format = '%Y'
year = None
@ -29,7 +30,7 @@ class YearMixin(object):
try:
year = self.request.GET['year']
except KeyError:
raise Http404("No year specified")
raise Http404(_(u"No year specified"))
return year
@ -54,7 +55,7 @@ class MonthMixin(object):
try:
month = self.request.GET['month']
except KeyError:
raise Http404("No month specified")
raise Http404(_(u"No month specified"))
return month
def get_next_month(self, date):
@ -95,7 +96,7 @@ class DayMixin(object):
try:
day = self.request.GET['day']
except KeyError:
raise Http404("No day specified")
raise Http404(_(u"No day specified"))
return day
def get_next_day(self, date):
@ -134,7 +135,7 @@ class WeekMixin(object):
try:
week = self.request.GET['week']
except KeyError:
raise Http404("No week specified")
raise Http404(_(u"No week specified"))
return week
@ -194,7 +195,9 @@ class BaseDateListView(MultipleObjectMixin, DateMixin, View):
qs = qs.filter(**{'%s__lte' % date_field: datetime.datetime.now()})
if not allow_empty and not qs:
raise Http404(u"No %s available" % unicode(qs.model._meta.verbose_name_plural))
raise Http404(_(u"No %(verbose_name_plural)s available") % {
'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural)
})
return qs
@ -208,7 +211,9 @@ class BaseDateListView(MultipleObjectMixin, DateMixin, View):
date_list = queryset.dates(date_field, date_type)[::-1]
if date_list is not None and not date_list and not allow_empty:
raise Http404(u"No %s available" % unicode(qs.model._meta.verbose_name_plural))
raise Http404(_(u"No %(verbose_name_plural)s available") % {
'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural)
})
return date_list
@ -458,9 +463,10 @@ class BaseDateDetailView(YearMixin, MonthMixin, DayMixin, DateMixin, BaseDetailV
qs = self.get_queryset()
if not self.get_allow_future() and date > datetime.date.today():
raise Http404("Future %s not available because %s.allow_future is False." % (
qs.model._meta.verbose_name_plural, self.__class__.__name__)
)
raise Http404(_(u"Future %(verbose_name_plural)s not available because %(class_name)s.allow_future is False.") % {
'verbose_name_plural': qs.model._meta.verbose_name_plural,
'class_name': self.__class__.__name__,
})
# Filter down a queryset from self.queryset using the date from the
# URL. This'll get passed as the queryset to DetailView.get_object,
@ -491,7 +497,10 @@ def _date_from_string(year, year_format, month, month_format, day='', day_format
try:
return datetime.date(*time.strptime(datestr, format)[:3])
except ValueError:
raise Http404(u"Invalid date string '%s' given format '%s'" % (datestr, format))
raise Http404(_(u"Invalid date string '%(datestr)s' given format '%(format)s'") % {
'datestr': datestr,
'format': format,
})
def _month_bounds(date):

View File

@ -3,6 +3,7 @@ import re
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
from django.http import Http404
from django.utils.encoding import smart_str
from django.utils.translation import ugettext as _
from django.views.generic.base import TemplateResponseMixin, View
@ -47,8 +48,8 @@ class SingleObjectMixin(object):
try:
obj = queryset.get()
except ObjectDoesNotExist:
raise Http404(u"No %s found matching the query" %
(queryset.model._meta.verbose_name))
raise Http404(_(u"No %(verbose_name)s found matching the query") %
{'verbose_name': queryset.model._meta.verbose_name})
return obj
def get_queryset(self):

View File

@ -4,6 +4,7 @@ from django.core.paginator import Paginator, InvalidPage
from django.core.exceptions import ImproperlyConfigured
from django.http import Http404
from django.utils.encoding import smart_str
from django.utils.translation import ugettext as _
from django.views.generic.base import TemplateResponseMixin, View
@ -43,12 +44,14 @@ class MultipleObjectMixin(object):
if page == 'last':
page_number = paginator.num_pages
else:
raise Http404("Page is not 'last', nor can it be converted to an int.")
raise Http404(_(u"Page is not 'last', nor can it be converted to an int."))
try:
page = paginator.page(page_number)
return (paginator, page, page.object_list, True)
except InvalidPage:
raise Http404(u'Invalid page (%s)' % page_number)
raise Http404(_(u'Invalid page (%(page_number)s)') % {
'page_number': page_number
})
def get_paginate_by(self, queryset):
"""
@ -113,8 +116,8 @@ class BaseListView(MultipleObjectMixin, View):
self.object_list = self.get_queryset()
allow_empty = self.get_allow_empty()
if not allow_empty and len(self.object_list) == 0:
raise Http404(u"Empty list and '%s.allow_empty' is False."
% self.__class__.__name__)
raise Http404(_(u"Empty list and '%(class_name)s.allow_empty' is False.")
% {'class_name': self.__class__.__name__})
context = self.get_context_data(object_list=self.object_list)
return self.render_to_response(context)