@classonlymethoddefas_view(cls,**initkwargs):"""Main entry point for a request-response process."""forkeyininitkwargs:ifkeyincls.http_method_names:raiseTypeError("You tried to pass in the %s method name as a ""keyword argument to %s(). Don't do that."%(key,cls.__name__))ifnothasattr(cls,key):raiseTypeError("%s() received an invalid keyword %r. as_view ""only accepts arguments that are already ""attributes of the class."%(cls.__name__,key))defview(request,*args,**kwargs):self=cls(**initkwargs)ifhasattr(self,'get')andnothasattr(self,'head'):self.head=self.getself.request=requestself.args=argsself.kwargs=kwargsreturnself.dispatch(request,*args,**kwargs)view.view_class=clsview.view_initkwargs=initkwargs# take name and docstring from classupdate_wrapper(view,cls,updated=())# and possible attributes set by decorators# like csrf_exempt from dispatchupdate_wrapper(view,cls.dispatch,assigned=())returnview
80
81
82
83
84
85
86
87
88
defdispatch(self,request,*args,**kwargs):# Try to dispatch to the right method; if a method doesn't exist,# defer to the error handler. Also defer to the error handler if the# request method isn't on the approved list.ifrequest.method.lower()inself.http_method_names:handler=getattr(self,request.method.lower(),self.http_method_not_allowed)else:handler=self.http_method_not_allowedreturnhandler(request,*args,**kwargs)
Return ``True`` if the view should display empty lists and ``False``
if a 404 should be raised instead.
97
98
99
100
101
102
defget_allow_empty(self):""" Return ``True`` if the view should display empty lists and ``False`` if a 404 should be raised instead. """returnself.allow_empty
Return `True` if the view should be allowed to display objects from
the future.
240
241
242
243
244
245
defget_allow_future(self):""" Return `True` if the view should be allowed to display objects from the future. """returnself.allow_future
defget_context_data(self,*,object_list=None,**kwargs):"""Get the context for this view."""queryset=object_listifobject_listisnotNoneelseself.object_listpage_size=self.get_paginate_by(queryset)context_object_name=self.get_context_object_name(queryset)ifpage_size:paginator,page,queryset,is_paginated=self.paginate_queryset(queryset,page_size)context={'paginator':paginator,'page_obj':page,'is_paginated':is_paginated,'object_list':queryset}else:context={'paginator':None,'page_obj':None,'is_paginated':False,'object_list':queryset}ifcontext_object_nameisnotNone:context[context_object_name]=querysetcontext.update(kwargs)returnsuper().get_context_data(**context)
Get the name of the item to be used in the context.
104
105
106
107
108
109
110
111
defget_context_object_name(self,object_list):"""Get the name of the item to be used in the context."""ifself.context_object_name:returnself.context_object_nameelifhasattr(object_list,'model'):return'%s_list'%object_list.model._meta.model_nameelse:returnNone
BaseWeekArchiveView
Return (date_list, items, extra_context) for this request.
defget_dated_items(self):"""Return (date_list, items, extra_context) for this request."""year=self.get_year()week=self.get_week()date_field=self.get_date_field()week_format=self.get_week_format()week_start={'%W':'1','%U':'0',}[week_format]date=_date_from_string(year,self.get_year_format(),week_start,'%w',week,week_format)since=self._make_date_lookup_arg(date)until=self._make_date_lookup_arg(self._get_next_week(date))lookup_kwargs={'%s__gte'%date_field:since,'%s__lt'%date_field:until,}qs=self.get_dated_queryset(**lookup_kwargs)return(None,qs,{'week':date,'next_week':self.get_next_week(date),'previous_week':self.get_previous_week(date),})
BaseDateListView
Obtain the list of dates and items.
307
308
309
defget_dated_items(self):"""Obtain the list of dates and items."""raiseNotImplementedError('A DateView must provide an implementation of get_dated_items()')
Get a queryset properly filtered according to `allow_future` and any
extra lookup kwargs.
defget_dated_queryset(self,**lookup):""" Get a queryset properly filtered according to `allow_future` and any extra lookup kwargs. """qs=self.get_queryset().filter(**lookup)date_field=self.get_date_field()allow_future=self.get_allow_future()allow_empty=self.get_allow_empty()paginate_by=self.get_paginate_by(qs)ifnotallow_future:now=timezone.now()ifself.uses_datetime_fieldelsetimezone_today()qs=qs.filter(**{'%s__lte'%date_field:now})ifnotallow_empty:# When pagination is enabled, it's better to do a cheap query# than to load the unpaginated queryset in memory.is_empty=notqsifpaginate_byisNoneelsenotqs.exists()ifis_empty:raiseHttp404(_("No %(verbose_name_plural)s available")%{'verbose_name_plural':qs.model._meta.verbose_name_plural,})returnqs
Get the name of the date field to be used to filter by.
234
235
236
237
238
defget_date_field(self):"""Get the name of the date field to be used to filter by."""ifself.date_fieldisNone:raiseImproperlyConfigured("%s.date_field is required."%self.__class__.__name__)returnself.date_field
Get a date list by calling `queryset.dates/datetimes()`, checking
along the way for empty lists that aren't allowed.
defget_date_list(self,queryset,date_type=None,ordering='ASC'):""" Get a date list by calling `queryset.dates/datetimes()`, checking along the way for empty lists that aren't allowed. """date_field=self.get_date_field()allow_empty=self.get_allow_empty()ifdate_typeisNone:date_type=self.get_date_list_period()ifself.uses_datetime_field:date_list=queryset.datetimes(date_field,date_type,ordering)else:date_list=queryset.dates(date_field,date_type,ordering)ifdate_listisnotNoneandnotdate_listandnotallow_empty:raiseHttp404(_("No %(verbose_name_plural)s available")%{'verbose_name_plural':queryset.model._meta.verbose_name_plural,})returndate_list
Get the aggregation period for the list of dates: 'year', 'month', or
'day'.
344
345
346
347
348
349
defget_date_list_period(self):""" Get the aggregation period for the list of dates: 'year', 'month', or 'day'. """returnself.date_list_period
Get the next valid week.
191
192
193
defget_next_week(self,date):"""Get the next valid week."""return_get_next_prev(self,date,is_previous=False,period='week')
Get the next valid year.
44
45
46
defget_next_year(self,date):"""Get the next valid year."""return_get_next_prev(self,date,is_previous=False,period='year')
BaseDateListView
Return the field or fields to use for ordering the queryset; use the
date field by default.
311
312
313
314
315
316
defget_ordering(self):""" Return the field or fields to use for ordering the queryset; use the date field by default. """return'-%s'%self.get_date_field()ifself.orderingisNoneelseself.ordering
MultipleObjectMixin
Return the field or fields to use for ordering the queryset.
50
51
52
defget_ordering(self):"""Return the field or fields to use for ordering the queryset."""returnself.ordering
Get the number of items to paginate by, or ``None`` for no pagination.
77
78
79
80
81
defget_paginate_by(self,queryset):""" Get the number of items to paginate by, or ``None`` for no pagination. """returnself.paginate_by
Return the maximum number of orphans extend the last page by when
paginating.
90
91
92
93
94
95
defget_paginate_orphans(self):""" Return the maximum number of orphans extend the last page by when paginating. """returnself.paginate_orphans
Return an instance of the paginator for this view.
83
84
85
86
87
88
defget_paginator(self,queryset,per_page,orphans=0,allow_empty_first_page=True,**kwargs):"""Return an instance of the paginator for this view."""returnself.paginator_class(queryset,per_page,orphans=orphans,allow_empty_first_page=allow_empty_first_page,**kwargs)
Get the previous valid week.
195
196
197
defget_previous_week(self,date):"""Get the previous valid week."""return_get_next_prev(self,date,is_previous=True,period='week')
Get the previous valid year.
48
49
50
defget_previous_year(self,date):"""Get the previous valid year."""return_get_next_prev(self,date,is_previous=True,period='year')
Return the list of items for this view.
The return value must be an iterable and may be an instance of
`QuerySet` in which case `QuerySet` specific behavior will be enabled.
defget_queryset(self):""" Return the list of items for this view. The return value must be an iterable and may be an instance of `QuerySet` in which case `QuerySet` specific behavior will be enabled. """ifself.querysetisnotNone:queryset=self.querysetifisinstance(queryset,QuerySet):queryset=queryset.all()elifself.modelisnotNone:queryset=self.model._default_manager.all()else:raiseImproperlyConfigured("%(cls)s is missing a QuerySet. Define ""%(cls)s.model, %(cls)s.queryset, or override ""%(cls)s.get_queryset()."%{'cls':self.__class__.__name__})ordering=self.get_ordering()ifordering:ifisinstance(ordering,str):ordering=(ordering,)queryset=queryset.order_by(*ordering)returnqueryset
Возвращает неделю, для которой в этом представлении должны отображаться данные.
178
179
180
181
182
183
184
185
186
187
188
189
defget_week(self):"""Return the week for which this view should display data."""week=self.weekifweekisNone:try:week=self.kwargs['week']exceptKeyError:try:week=self.request.GET['week']exceptKeyError:raiseHttp404(_("No week specified"))returnweek
Получает строку формата недели в синтаксисе strptime, которая будет использоваться для анализа недели по переменным URL.
171
172
173
174
175
176
defget_week_format(self):""" Get a week format string in strptime syntax to be used to parse the week from url variables. """returnself.week_format
Возвращает год, для которого это представление должно отображать данные.
31
32
33
34
35
36
37
38
39
40
41
42
defget_year(self):"""Return the year for which this view should display data."""year=self.yearifyearisNone:try:year=self.kwargs['year']exceptKeyError:try:year=self.request.GET['year']exceptKeyError:raiseHttp404(_("No year specified"))returnyear
Получает строку в формате года в синтаксисе strptime, которая будет использоваться для анализа года по переменным URL.
24
25
26
27
28
29
defget_year_format(self):""" Get a year format string in strptime syntax to be used to parse the year from url variables. """returnself.year_format
90
91
92
93
94
95
defhttp_method_not_allowed(self,request,*args,**kwargs):logger.warning('Method Not Allowed (%s): %s',request.method,request.path,extra={'status_code':405,'request':request})returnHttpResponseNotAllowed(self._allowed_methods())
Обрабатывает ответы на запросы для запроса HTTP OPTIONS.
97
98
99
100
101
102
defoptions(self,request,*args,**kwargs):"""Handle responding to requests for the OPTIONS HTTP verb."""response=HttpResponse()response['Allow']=', '.join(self._allowed_methods())response['Content-Length']='0'returnresponse
defpaginate_queryset(self,queryset,page_size):"""Paginate the queryset, if needed."""paginator=self.get_paginator(queryset,page_size,orphans=self.get_paginate_orphans(),allow_empty_first_page=self.get_allow_empty())page_kwarg=self.page_kwargpage=self.kwargs.get(page_kwarg)orself.request.GET.get(page_kwarg)or1try:page_number=int(page)exceptValueError:ifpage=='last':page_number=paginator.num_pageselse:raiseHttp404(_("Page is not 'last', nor can it be converted to an int."))try:page=paginator.page(page_number)return(paginator,page,page.object_list,page.has_other_pages())exceptInvalidPagease:raiseHttp404(_('Invalid page (%(page_number)s): %(message)s')%{'page_number':page_number,'message':str(e)})
def_get_current_week(self,date):"""Return the start date of the current interval."""returndate-datetime.timedelta(self._get_weekday(date))
Возвращает дату начала текущего интервала.
63
64
65
def_get_current_year(self,date):"""Return the start date of the current interval."""returndate.replace(month=1,day=1)
Возвращает дату начала следующего интервала.
Интервал определяется датой начала <= дата элемента < следующая дата начала.
199
200
201
202
203
204
205
206
207
def_get_next_week(self,date):""" Return the start date of the next interval. The interval is defined by start date <= item date < next start date. """try:returndate+datetime.timedelta(days=7-self._get_weekday(date))exceptOverflowError:raiseHttp404(_("Date out of range"))
Возвращает дату начала следующего интервала.
Интервал определяется датой начала <= дата элемента < следующая дата начала.
52
53
54
55
56
57
58
59
60
def_get_next_year(self,date):""" Return the start date of the next interval. The interval is defined by start date <= item date < next start date. """try:returndate.replace(year=date.year+1,month=1,day=1)exceptValueError:raiseHttp404(_("Date out of range"))
Return the weekday for a given date.
The first day according to the week format is 0 and the last day is 6.
214
215
216
217
218
219
220
221
222
223
224
225
def_get_weekday(self,date):""" Return the weekday for a given date. The first day according to the week format is 0 and the last day is 6. """week_format=self.get_week_format()ifweek_format=='%W':# week starts on Mondayreturndate.weekday()elifweek_format=='%U':# week starts on Sundayreturn(date.weekday()+1)%7else:raiseValueError("unknown week format: %s"%week_format)
Convert a date into a datetime when the date field is a DateTimeField.
When time zone support is enabled, `date` is assumed to be in the
current time zone, so that displayed items are consistent with the URL.
260
261
262
263
264
265
266
267
268
269
270
def_make_date_lookup_arg(self,value):""" Convert a date into a datetime when the date field is a DateTimeField. When time zone support is enabled, `date` is assumed to be in the current time zone, so that displayed items are consistent with the URL. """ifself.uses_datetime_field:value=datetime.datetime.combine(value,datetime.time.min)ifsettings.USE_TZ:value=timezone.make_aware(value)returnvalue
Get the lookup kwargs for filtering on a single date.
If the date field is a DateTimeField, we can't just filter on
date_field=date because that doesn't take the time into account.
def_make_single_date_lookup(self,date):""" Get the lookup kwargs for filtering on a single date. If the date field is a DateTimeField, we can't just filter on date_field=date because that doesn't take the time into account. """date_field=self.get_date_field()ifself.uses_datetime_field:since=self._make_date_lookup_arg(date)until=self._make_date_lookup_arg(date+datetime.timedelta(days=1))return{'%s__gte'%date_field:since,'%s__lt'%date_field:until,}else:# Skip self._make_date_lookup_arg, it's a no-op in this branch.return{date_field:date}
Конструктор. Вызывается в URLconf; может содержать полезные дополнительные ключевые аргументы и другие вещи.
38
39
40
41
42
43
44
45
46
def__init__(self,**kwargs):""" Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things. """# Go through keyword arguments, and either save their values to our# instance, or raise an error.forkey,valueinkwargs.items():setattr(self,key,value)