2012-09-21 03:03:24 +08:00
|
|
|
import logging
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2017-02-25 14:48:20 +08:00
|
|
|
from django.http import (
|
|
|
|
HttpResponse, HttpResponseGone, HttpResponseNotAllowed,
|
|
|
|
HttpResponsePermanentRedirect, HttpResponseRedirect,
|
|
|
|
)
|
2010-12-07 21:57:01 +08:00
|
|
|
from django.template.response import TemplateResponse
|
2017-03-01 03:07:57 +08:00
|
|
|
from django.urls import reverse
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.decorators import classonlymethod
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2012-09-21 03:03:24 +08:00
|
|
|
logger = logging.getLogger('django.request')
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2011-01-27 11:14:20 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class ContextMixin:
|
2012-04-07 05:24:33 +08:00
|
|
|
"""
|
|
|
|
A default context mixin that passes the keyword arguments received by
|
2017-01-25 04:36:07 +08:00
|
|
|
get_context_data() as the template context.
|
2012-04-07 05:24:33 +08:00
|
|
|
"""
|
2017-07-06 22:34:54 +08:00
|
|
|
extra_context = None
|
2012-04-07 05:24:33 +08:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-11-14 05:15:49 +08:00
|
|
|
kwargs.setdefault('view', self)
|
2017-07-06 22:34:54 +08:00
|
|
|
if self.extra_context is not None:
|
|
|
|
kwargs.update(self.extra_context)
|
2012-04-07 05:24:33 +08:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class View:
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
Intentionally simple parent class for all views. Only implements
|
|
|
|
dispatch-by-method and simple sanity checking.
|
|
|
|
"""
|
|
|
|
|
2013-05-22 00:01:29 +08:00
|
|
|
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
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.
|
2017-01-07 19:11:46 +08:00
|
|
|
for key, value in kwargs.items():
|
2010-10-18 21:34:47 +08:00
|
|
|
setattr(self, key, value)
|
|
|
|
|
|
|
|
@classonlymethod
|
|
|
|
def as_view(cls, **initkwargs):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Main entry point for a request-response process."""
|
2010-10-18 21:34:47 +08:00
|
|
|
for key in initkwargs:
|
|
|
|
if key in cls.http_method_names:
|
2020-01-05 18:04:51 +08:00
|
|
|
raise TypeError(
|
|
|
|
'The method name %s is not accepted as a keyword argument '
|
|
|
|
'to %s().' % (key, cls.__name__)
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
if not hasattr(cls, key):
|
2012-11-22 22:07:21 +08:00
|
|
|
raise TypeError("%s() received an invalid keyword %r. as_view "
|
|
|
|
"only accepts arguments that are already "
|
|
|
|
"attributes of the class." % (cls.__name__, key))
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def view(request, *args, **kwargs):
|
|
|
|
self = cls(**initkwargs)
|
2018-09-19 16:53:05 +08:00
|
|
|
self.setup(request, *args, **kwargs)
|
|
|
|
if not hasattr(self, 'request'):
|
|
|
|
raise AttributeError(
|
|
|
|
"%s instance has no 'request' attribute. Did you override "
|
|
|
|
"setup() and forget to call super()?" % cls.__name__
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
return self.dispatch(request, *args, **kwargs)
|
2014-12-27 15:16:53 +08:00
|
|
|
view.view_class = cls
|
|
|
|
view.view_initkwargs = initkwargs
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2020-12-12 03:30:50 +08:00
|
|
|
# __name__ and __qualname__ are intentionally left unchanged as
|
|
|
|
# view_class should be used to robustly determine the name of the view
|
|
|
|
# instead.
|
|
|
|
view.__doc__ = cls.__doc__
|
|
|
|
view.__module__ = cls.__module__
|
|
|
|
view.__annotations__ = cls.dispatch.__annotations__
|
|
|
|
# Copy possible attributes set by decorators, e.g. @csrf_exempt, from
|
|
|
|
# the dispatch method.
|
|
|
|
view.__dict__.update(cls.dispatch.__dict__)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
return view
|
|
|
|
|
2018-09-19 16:53:05 +08:00
|
|
|
def setup(self, request, *args, **kwargs):
|
|
|
|
"""Initialize attributes shared by all view methods."""
|
2019-10-10 13:22:43 +08:00
|
|
|
if hasattr(self, 'get') and not hasattr(self, 'head'):
|
|
|
|
self.head = self.get
|
2018-09-19 16:53:05 +08:00
|
|
|
self.request = request
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2010-10-19 08:26:15 +08:00
|
|
|
# Try to dispatch to the right method; if a method doesn't exist,
|
2010-10-18 21:34:47 +08:00
|
|
|
# defer to the error handler. Also defer to the error handler if the
|
|
|
|
# request method isn't on the approved list.
|
|
|
|
if request.method.lower() in self.http_method_names:
|
|
|
|
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
|
|
|
|
else:
|
|
|
|
handler = self.http_method_not_allowed
|
|
|
|
return handler(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def http_method_not_allowed(self, request, *args, **kwargs):
|
2016-03-29 06:33:29 +08:00
|
|
|
logger.warning(
|
|
|
|
'Method Not Allowed (%s): %s', request.method, request.path,
|
|
|
|
extra={'status_code': 405, 'request': request}
|
2010-10-19 08:26:15 +08:00
|
|
|
)
|
2017-02-25 14:48:20 +08:00
|
|
|
return HttpResponseNotAllowed(self._allowed_methods())
|
2012-05-17 19:54:51 +08:00
|
|
|
|
|
|
|
def options(self, request, *args, **kwargs):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Handle responding to requests for the OPTIONS HTTP verb."""
|
2017-02-25 14:48:20 +08:00
|
|
|
response = HttpResponse()
|
2020-07-14 19:32:24 +08:00
|
|
|
response.headers['Allow'] = ', '.join(self._allowed_methods())
|
|
|
|
response.headers['Content-Length'] = '0'
|
2012-05-17 19:54:51 +08:00
|
|
|
return response
|
|
|
|
|
|
|
|
def _allowed_methods(self):
|
|
|
|
return [m.upper() for m in self.http_method_names if hasattr(self, m)]
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class TemplateResponseMixin:
|
2017-01-25 04:36:07 +08:00
|
|
|
"""A mixin that can be used to render a template."""
|
2010-10-18 21:34:47 +08:00
|
|
|
template_name = None
|
2015-01-27 04:57:10 +08:00
|
|
|
template_engine = None
|
2010-12-07 21:57:01 +08:00
|
|
|
response_class = TemplateResponse
|
2013-01-31 04:26:17 +08:00
|
|
|
content_type = None
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2010-12-07 21:57:01 +08:00
|
|
|
def render_to_response(self, context, **response_kwargs):
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Return a response, using the `response_class` for this view, with a
|
|
|
|
template rendered with the given context.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2017-01-25 04:36:07 +08:00
|
|
|
Pass response_kwargs to the constructor of the response class.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2013-01-31 04:26:17 +08:00
|
|
|
response_kwargs.setdefault('content_type', self.content_type)
|
2010-12-07 21:57:01 +08:00
|
|
|
return self.response_class(
|
2013-11-03 17:22:11 +08:00
|
|
|
request=self.request,
|
|
|
|
template=self.get_template_names(),
|
|
|
|
context=context,
|
2015-01-27 04:57:10 +08:00
|
|
|
using=self.template_engine,
|
2010-12-07 21:57:01 +08:00
|
|
|
**response_kwargs
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_template_names(self):
|
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Return a list of template names to be used for the request. Must return
|
|
|
|
a list. May not be called if render_to_response() is overridden.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
if self.template_name is None:
|
2011-02-14 21:05:14 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"TemplateResponseMixin requires either a definition of "
|
|
|
|
"'template_name' or an implementation of 'get_template_names()'")
|
2010-10-18 21:34:47 +08:00
|
|
|
else:
|
|
|
|
return [self.template_name]
|
|
|
|
|
|
|
|
|
2012-04-07 05:24:33 +08:00
|
|
|
class TemplateView(TemplateResponseMixin, ContextMixin, View):
|
2020-08-24 15:00:12 +08:00
|
|
|
"""
|
|
|
|
Render a template. Pass keyword arguments from the URLconf to the context.
|
|
|
|
"""
|
2010-10-18 21:34:47 +08:00
|
|
|
def get(self, request, *args, **kwargs):
|
2020-08-24 15:00:12 +08:00
|
|
|
context = self.get_context_data(**kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
|
|
|
|
class RedirectView(View):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Provide a redirect on any GET request."""
|
2015-01-19 05:43:57 +08:00
|
|
|
permanent = False
|
2010-10-18 21:34:47 +08:00
|
|
|
url = None
|
2013-06-14 18:59:26 +08:00
|
|
|
pattern_name = None
|
2010-10-18 21:34:47 +08:00
|
|
|
query_string = False
|
|
|
|
|
2013-06-14 18:59:26 +08:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Return the URL redirect to. Keyword arguments from the URL pattern
|
|
|
|
match generating the redirect request are provided as kwargs to this
|
|
|
|
method.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
if self.url:
|
2012-03-03 00:55:56 +08:00
|
|
|
url = self.url % kwargs
|
2013-06-14 18:59:26 +08:00
|
|
|
elif self.pattern_name:
|
2017-03-01 03:07:57 +08:00
|
|
|
url = reverse(self.pattern_name, args=args, kwargs=kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2013-06-14 18:59:26 +08:00
|
|
|
args = self.request.META.get('QUERY_STRING', '')
|
|
|
|
if args and self.query_string:
|
|
|
|
url = "%s?%s" % (url, args)
|
|
|
|
return url
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def get(self, request, *args, **kwargs):
|
2013-06-14 18:59:26 +08:00
|
|
|
url = self.get_redirect_url(*args, **kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
if url:
|
|
|
|
if self.permanent:
|
2017-02-25 14:48:20 +08:00
|
|
|
return HttpResponsePermanentRedirect(url)
|
2010-10-18 21:34:47 +08:00
|
|
|
else:
|
2017-02-25 14:48:20 +08:00
|
|
|
return HttpResponseRedirect(url)
|
2010-10-18 21:34:47 +08:00
|
|
|
else:
|
2016-03-29 06:33:29 +08:00
|
|
|
logger.warning(
|
|
|
|
'Gone: %s', request.path,
|
|
|
|
extra={'status_code': 410, 'request': request}
|
|
|
|
)
|
2017-02-25 14:48:20 +08:00
|
|
|
return HttpResponseGone()
|
2011-04-02 16:45:22 +08:00
|
|
|
|
|
|
|
def head(self, request, *args, **kwargs):
|
|
|
|
return self.get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
return self.get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def options(self, request, *args, **kwargs):
|
|
|
|
return self.get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
return self.get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def put(self, request, *args, **kwargs):
|
|
|
|
return self.get(request, *args, **kwargs)
|
2013-05-22 00:01:29 +08:00
|
|
|
|
|
|
|
def patch(self, request, *args, **kwargs):
|
|
|
|
return self.get(request, *args, **kwargs)
|