Ticket #15287: patch2.diff
File patch2.diff, 19.0 KB (added by , 14 years ago) |
---|
-
views/generic/base.py
5 5 from django.utils.functional import update_wrapper 6 6 from django.utils.log import getLogger 7 7 from django.utils.decorators import classonlymethod 8 from django.utils.translation import ugettext as _ 8 9 9 10 logger = getLogger('django.request') 10 11 … … 35 36 # sanitize keyword arguments 36 37 for key in initkwargs: 37 38 if key in cls.http_method_names: 38 raise TypeError(u"You tried to pass in the %s method name as a " 39 u"keyword argument to %s(). Don't do that." 40 % (key, cls.__name__)) 39 raise TypeError(_("You tried to pass in the %(kwarg)s method " 40 "name as a keyword argument to " 41 "%(class_name)s(). Don't do that.") 42 % {'kwarg': key, 43 'class_name': cls.__name__}) 41 44 if not hasattr(cls, key): 42 raise TypeError(u"%s() received an invalid keyword %r" % ( 43 cls.__name__, key)) 45 raise TypeError(_("%(class_name)s() received an invalid keyword %(kwarg)s") 46 % {'class_name': cls.__name__, 47 'kwarg': key}) 44 48 45 49 def view(request, *args, **kwargs): 46 50 self = cls(**initkwargs) -
views/generic/date_based.py
6 6 from django.core.xheaders import populate_xheaders 7 7 from django.db.models.fields import DateTimeField 8 8 from django.http import Http404, HttpResponse 9 from django.utils.encoding import force_unicode 10 from django.utils.translation import ugettext as _ 9 11 10 12 import warnings 11 13 warnings.warn( … … 34 36 queryset = queryset.filter(**{'%s__lte' % date_field: datetime.datetime.now()}) 35 37 date_list = queryset.dates(date_field, 'year')[::-1] 36 38 if not date_list and not allow_empty: 37 raise Http404("No %s available" % model._meta.verbose_name) 39 raise Http404(_("No %(verbose_name)s available") 40 % {'verbose_name': force_unicode(model._meta.verbose_name)}) 38 41 39 42 if date_list and num_latest: 40 43 latest = queryset.order_by('-'+date_field)[:num_latest] … … 350 353 elif slug and slug_field: 351 354 lookup_kwargs['%s__exact' % slug_field] = slug 352 355 else: 353 raise AttributeError("Generic detail view must be called with either an object_id or a slug/slugfield") 356 raise AttributeError(_("Generic detail view must be called with either" 357 " an object_id or a slug/slugfield")) 354 358 try: 355 359 obj = queryset.get(**lookup_kwargs) 356 360 except ObjectDoesNotExist: 357 raise Http404("No %s found for" % model._meta.verbose_name) 361 raise Http404(_("No %(verbose_name)s found for") 362 % {'verbose_name': force_unicode(model._meta.verbose_name)}) 358 363 if not template_name: 359 364 template_name = "%s/%s_detail.html" % (model._meta.app_label, model._meta.object_name.lower()) 360 365 if template_name_field: -
views/generic/list.py
5 5 from django.http import Http404 6 6 from django.utils.encoding import smart_str 7 7 from django.views.generic.base import TemplateResponseMixin, View 8 from django.utils.translation import ugettext as _ 9 from django.utils.encoding import force_unicode 8 10 9 11 10 12 class MultipleObjectMixin(object): … … 27 29 elif self.model is not None: 28 30 queryset = self.model._default_manager.all() 29 31 else: 30 raise ImproperlyConfigured( u"'%s' must define 'queryset' or 'model'"31 % self.__class__.__name__)32 raise ImproperlyConfigured(_("'%(class_name)s' must define 'queryset' or 'model'") 33 % {'class_name': self.__class__.__name__}) 32 34 return queryset 33 35 34 36 def paginate_queryset(self, queryset, page_size): … … 43 45 if page == 'last': 44 46 page_number = paginator.num_pages 45 47 else: 46 raise Http404( "Page is not 'last', nor can it be converted to an int.")48 raise Http404(_("Page is not 'last', nor can it be converted to an int.")) 47 49 try: 48 50 page = paginator.page(page_number) 49 51 return (paginator, page, page.object_list, True) 50 52 except InvalidPage: 51 raise Http404(u'Invalid page (%s)' % page_number) 53 raise Http404(_('Invalid page (%(page_number)s)') 54 % {'page_number': page_number}) 52 55 53 56 def get_paginate_by(self, queryset): 54 57 """ … … 114 117 self.object_list = self.get_queryset() 115 118 allow_empty = self.get_allow_empty() 116 119 if not allow_empty and len(self.object_list) == 0: 117 raise Http404( u"Empty list and '%s.allow_empty' is False."118 % self.__class__.__name__)120 raise Http404(_("Empty list and '%(class_name)s.allow_empty' is False.") 121 % {'class_name': self.__class__.__name__}) 119 122 context = self.get_context_data(object_list=self.object_list) 120 123 return self.render_to_response(context) 121 124 -
views/generic/list_detail.py
3 3 from django.core.xheaders import populate_xheaders 4 4 from django.core.paginator import Paginator, InvalidPage 5 5 from django.core.exceptions import ObjectDoesNotExist 6 from django.utils.translation import ugettext as _ 7 from django.utils.encoding import force_unicode 6 8 7 9 import warnings 8 10 warnings.warn( … … 127 129 elif slug and slug_field: 128 130 queryset = queryset.filter(**{slug_field: slug}) 129 131 else: 130 raise AttributeError("Generic detail view must be called with either an object_id or a slug/slug_field.") 132 raise AttributeError(_("Generic detail view must be called with " 133 "either an object_id or a slug/slug_field.")) 131 134 try: 132 135 obj = queryset.get() 133 136 except ObjectDoesNotExist: 134 raise Http404("No %s found matching the query" % (model._meta.verbose_name)) 137 raise Http404(_("No %(verbose_name)s found matching the query") 138 % {'verbose_name': force_unicode(model._meta.verbose_name)}) 135 139 if not template_name: 136 140 template_name = "%s/%s_detail.html" % (model._meta.app_label, model._meta.object_name.lower()) 137 141 if template_name_field: -
views/generic/dates.py
6 6 from django.views.generic.base import View 7 7 from django.views.generic.detail import BaseDetailView, SingleObjectTemplateResponseMixin 8 8 from django.views.generic.list import MultipleObjectMixin, MultipleObjectTemplateResponseMixin 9 from django.utils.translation import ugettext as _ 10 from django.utils.encoding import force_unicode 9 11 10 12 11 13 class YearMixin(object): … … 29 31 try: 30 32 year = self.request.GET['year'] 31 33 except KeyError: 32 raise Http404( "No year specified")34 raise Http404(_("No year specified")) 33 35 return year 34 36 35 37 … … 54 56 try: 55 57 month = self.request.GET['month'] 56 58 except KeyError: 57 raise Http404( "No month specified")59 raise Http404(_("No month specified")) 58 60 return month 59 61 60 62 def get_next_month(self, date): … … 95 97 try: 96 98 day = self.request.GET['day'] 97 99 except KeyError: 98 raise Http404( "No day specified")100 raise Http404(_("No day specified")) 99 101 return day 100 102 101 103 def get_next_day(self, date): … … 134 136 try: 135 137 week = self.request.GET['week'] 136 138 except KeyError: 137 raise Http404( "No week specified")139 raise Http404(_("No week specified")) 138 140 return week 139 141 140 142 … … 150 152 Get the name of the date field to be used to filter by. 151 153 """ 152 154 if self.date_field is None: 153 raise ImproperlyConfigured(u"%s.date_field is required." % self.__class__.__name__) 155 raise ImproperlyConfigured(_("%(class_name)s.date_field is required.") 156 % {'class_name': self.__class__.__name__}) 154 157 return self.date_field 155 158 156 159 def get_allow_future(self): … … 178 181 """ 179 182 Obtain the list of dates and itesm 180 183 """ 181 raise NotImplementedError( 'A DateView must provide an implementation of get_dated_items()')184 raise NotImplementedError(_('A DateView must provide an implementation of get_dated_items()')) 182 185 183 186 def get_dated_queryset(self, **lookup): 184 187 """ … … 194 197 qs = qs.filter(**{'%s__lte' % date_field: datetime.datetime.now()}) 195 198 196 199 if not allow_empty and not qs: 197 raise Http404(u"No %s available" % unicode(qs.model._meta.verbose_name_plural)) 200 raise Http404(_("No %(verbose_name_plural)s available") 201 % {'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural)}) 198 202 199 203 return qs 200 204 … … 208 212 209 213 date_list = queryset.dates(date_field, date_type)[::-1] 210 214 if date_list is not None and not date_list and not allow_empty: 211 raise Http404(u"No %s available" % unicode(qs.model._meta.verbose_name_plural)) 215 raise Http404(_("No %(verbose_name_plural)s available") 216 % {'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural)}) 212 217 213 218 return date_list 214 219 … … 458 463 qs = self.get_queryset() 459 464 460 465 if not self.get_allow_future() and date > datetime.date.today(): 461 raise Http404("Future %s not available because %s.allow_future is False." % ( 462 qs.model._meta.verbose_name_plural, self.__class__.__name__) 466 raise Http404(_("Future %(verbose_name_plural)s are not available " 467 "because %(class_name)s.allow_future is False.") % ( 468 {'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural), 469 'class_name': self.__class__.__name__}) 463 470 ) 464 471 465 472 # Filter down a queryset from self.queryset using the date from the … … 491 498 try: 492 499 return datetime.date(*time.strptime(datestr, format)[:3]) 493 500 except ValueError: 494 raise Http404(u"Invalid date string '%s' given format '%s'" % (datestr, format)) 501 raise Http404(_("Invalid date string '%(date)s' given format '%(format)s'") 502 % {'date': datestr, 503 'format': format}) 495 504 496 505 497 506 def _month_bounds(date): -
views/generic/detail.py
4 4 from django.http import Http404 5 5 from django.utils.encoding import smart_str 6 6 from django.views.generic.base import TemplateResponseMixin, View 7 from django.utils.translation import ugettext as _ 8 from django.utils.encoding import force_unicode 7 9 8 10 9 11 class SingleObjectMixin(object): … … 40 42 41 43 # If none of those are defined, it's an error. 42 44 else: 43 raise AttributeError( u"Generic detail view %s must be called with "44 u"either an object id or a slug."45 raise AttributeError(_("Generic detail view %s must be called with " 46 "either an object id or a slug.") 45 47 % self.__class__.__name__) 46 48 47 49 try: 48 50 obj = queryset.get() 49 51 except ObjectDoesNotExist: 50 raise Http404( u"No %s found matching the query"%51 (queryset.model._meta.verbose_name))52 raise Http404(_("No %(verbose_name)s found matching the query") % 53 {'verbose_name': force_unicode(queryset.model._meta.verbose_name)}) 52 54 return obj 53 55 54 56 def get_queryset(self): … … 60 62 if self.model: 61 63 return self.model._default_manager.all() 62 64 else: 63 raise ImproperlyConfigured( u"%(cls)s is missing a queryset. Define "64 u"%(cls)s.model, %(cls)s.queryset, or override "65 u"%(cls)s.get_object()."% {65 raise ImproperlyConfigured(_("%(cls)s is missing a queryset. Define " 66 "%(cls)s.model, %(cls)s.queryset, or override " 67 "%(cls)s.get_object().") % { 66 68 'cls': self.__class__.__name__ 67 69 }) 68 70 return self.queryset._clone() -
views/generic/create_update.py
3 3 from django.http import Http404, HttpResponse, HttpResponseRedirect 4 4 from django.core.xheaders import populate_xheaders 5 5 from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured 6 from django.utils.translation import ugettext 6 from django.utils.translation import ugettext as _ 7 7 from django.contrib.auth.views import redirect_to_login 8 8 from django.views.generic import GenericViewError 9 9 from django.contrib import messages 10 from django.utils.encoding import force_unicode 10 11 12 11 13 import warnings 12 14 warnings.warn( 13 15 'Function-based generic views have been deprecated; use class-based views instead.', … … 48 50 class_name = model.__name__ + 'Form' 49 51 form_class = ModelFormMetaclass(class_name, (ModelForm,), {'Meta': Meta}) 50 52 return model, form_class 51 raise GenericViewError( "Generic view must be called with either a model or"52 " form_class argument.")53 raise GenericViewError(_("Generic view must be called with either a model" 54 " or form_class argument.")) 53 55 54 56 def redirect(post_save_redirect, obj): 55 57 """ … … 70 72 elif hasattr(obj, 'get_absolute_url'): 71 73 return HttpResponseRedirect(obj.get_absolute_url()) 72 74 else: 73 raise ImproperlyConfigured( 75 raise ImproperlyConfigured(_( 74 76 "No URL to redirect to. Either pass a post_save_redirect" 75 77 " parameter to the generic view or define a get_absolute_url" 76 " method on the Model.") 78 " method on the Model.")) 77 79 78 80 def lookup_object(model, object_id, slug, slug_field): 79 81 """ … … 88 90 elif slug and slug_field: 89 91 lookup_kwargs['%s__exact' % slug_field] = slug 90 92 else: 91 raise GenericViewError( 93 raise GenericViewError(_( 92 94 "Generic view must be called with either an object_id or a" 93 " slug/slug_field.") 95 " slug/slug_field.")) 94 96 try: 95 97 return model.objects.get(**lookup_kwargs) 96 98 except ObjectDoesNotExist: 97 raise Http404("No %s found for %s" 98 % (model._meta.verbose_name, lookup_kwargs)) 99 raise Http404(_("No %(verbose_name)s found for %(lookup_kwargs)s") 100 % {'verbose_name': force_unicode(model._meta.verbose_name), 101 'lookup_kwargs': lookup_kwargs}) 99 102 100 103 def create_object(request, model=None, template_name=None, 101 104 template_loader=loader, extra_context=None, post_save_redirect=None, … … 118 121 if form.is_valid(): 119 122 new_object = form.save() 120 123 121 msg = ugettext("The %(verbose_name)s was created successfully.") %\124 msg = _("The %(verbose_name)s was created successfully.") %\ 122 125 {"verbose_name": model._meta.verbose_name} 123 126 messages.success(request, msg, fail_silently=True) 124 127 return redirect(post_save_redirect, new_object) … … 161 164 form = form_class(request.POST, request.FILES, instance=obj) 162 165 if form.is_valid(): 163 166 obj = form.save() 164 msg = ugettext("The %(verbose_name)s was updated successfully.") %\167 msg = _("The %(verbose_name)s was updated successfully.") %\ 165 168 {"verbose_name": model._meta.verbose_name} 166 169 messages.success(request, msg, fail_silently=True) 167 170 return redirect(post_save_redirect, obj) … … 204 207 205 208 if request.method == 'POST': 206 209 obj.delete() 207 msg = ugettext("The %(verbose_name)s was deleted.") %\208 210 msg = _("The %(verbose_name)s was deleted.") %\ 211 {"verbose_name": model._meta.verbose_name} 209 212 messages.success(request, msg, fail_silently=True) 210 213 return HttpResponseRedirect(post_delete_redirect) 211 214 else: -
views/generic/edit.py
4 4 from django.views.generic.base import TemplateResponseMixin, View 5 5 from django.views.generic.detail import (SingleObjectMixin, 6 6 SingleObjectTemplateResponseMixin, BaseDetailView) 7 from django.utils.translation import ugettext as _ 7 8 8 9 9 10 class FormMixin(object): … … 53 54 url = self.success_url 54 55 else: 55 56 raise ImproperlyConfigured( 56 "No URL to redirect to. Provide a success_url.")57 _("No URL to redirect to. Provide a success_url.")) 57 58 return url 58 59 59 60 def form_valid(self, form): … … 93 94 try: 94 95 url = self.object.get_absolute_url() 95 96 except AttributeError: 96 raise ImproperlyConfigured( 97 raise ImproperlyConfigured(_( 97 98 "No URL to redirect to. Either provide a url or define" 98 " a get_absolute_url method on the Model.") 99 " a get_absolute_url method on the Model.")) 99 100 return url 100 101 101 102 def form_valid(self, form): … … 223 224 return self.success_url 224 225 else: 225 226 raise ImproperlyConfigured( 226 "No URL to redirect to. Provide a success_url.")227 _("No URL to redirect to. Provide a success_url.")) 227 228 228 229 229 230 class BaseDeleteView(DeletionMixin, BaseDetailView):