2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2012-09-21 03:03:24 +08:00
|
|
|
import logging
|
2014-11-22 00:55:58 +08:00
|
|
|
import warnings
|
2011-03-28 10:11:19 +08:00
|
|
|
from functools import update_wrapper
|
2012-09-21 03:03:24 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
from django import http
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2013-06-14 18:59:26 +08:00
|
|
|
from django.core.urlresolvers import reverse, NoReverseMatch
|
2010-12-07 21:57:01 +08:00
|
|
|
from django.template.response import TemplateResponse
|
2010-11-20 13:10:13 +08:00
|
|
|
from django.utils.decorators import classonlymethod
|
2014-11-22 00:55:58 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango19Warning
|
2012-07-21 03:14:27 +08:00
|
|
|
from django.utils import six
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2014-11-22 00:55:58 +08:00
|
|
|
_sentinel = object()
|
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
|
|
|
|
2012-04-07 05:24:33 +08:00
|
|
|
class ContextMixin(object):
|
|
|
|
"""
|
|
|
|
A default context mixin that passes the keyword arguments received by
|
|
|
|
get_context_data as the template context.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2012-08-18 21:46:17 +08:00
|
|
|
if 'view' not in kwargs:
|
|
|
|
kwargs['view'] = self
|
2012-04-07 05:24:33 +08:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
class View(object):
|
|
|
|
"""
|
|
|
|
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.
|
2012-07-21 03:14:27 +08:00
|
|
|
for key, value in six.iteritems(kwargs):
|
2010-10-18 21:34:47 +08:00
|
|
|
setattr(self, key, value)
|
|
|
|
|
|
|
|
@classonlymethod
|
|
|
|
def as_view(cls, **initkwargs):
|
|
|
|
"""
|
|
|
|
Main entry point for a request-response process.
|
|
|
|
"""
|
|
|
|
for key in initkwargs:
|
|
|
|
if key in cls.http_method_names:
|
2012-06-08 00:08:47 +08:00
|
|
|
raise TypeError("You tried to pass in the %s method name as a "
|
|
|
|
"keyword argument to %s(). Don't do that."
|
2010-10-18 21:34:47 +08:00
|
|
|
% (key, cls.__name__))
|
|
|
|
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)
|
2012-02-18 17:50:03 +08:00
|
|
|
if hasattr(self, 'get') and not hasattr(self, 'head'):
|
|
|
|
self.head = self.get
|
2012-11-23 03:10:01 +08:00
|
|
|
self.request = request
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
2010-10-18 21:34:47 +08:00
|
|
|
return self.dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
# take name and docstring from class
|
|
|
|
update_wrapper(view, cls, updated=())
|
|
|
|
|
|
|
|
# and possible attributes set by decorators
|
|
|
|
# like csrf_exempt from dispatch
|
|
|
|
update_wrapper(view, cls.dispatch, assigned=())
|
|
|
|
return view
|
|
|
|
|
|
|
|
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):
|
2012-02-10 02:58:36 +08:00
|
|
|
logger.warning('Method Not Allowed (%s): %s', request.method, request.path,
|
2010-10-19 08:26:15 +08:00
|
|
|
extra={
|
|
|
|
'status_code': 405,
|
2013-12-16 23:45:18 +08:00
|
|
|
'request': request
|
2010-10-19 08:26:15 +08:00
|
|
|
}
|
|
|
|
)
|
2012-05-17 19:54:51 +08:00
|
|
|
return http.HttpResponseNotAllowed(self._allowed_methods())
|
|
|
|
|
|
|
|
def options(self, request, *args, **kwargs):
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
|
|
|
Handles responding to requests for the OPTIONS HTTP verb.
|
|
|
|
"""
|
2012-05-17 19:54:51 +08:00
|
|
|
response = http.HttpResponse()
|
|
|
|
response['Allow'] = ', '.join(self._allowed_methods())
|
2012-10-20 23:40:14 +08:00
|
|
|
response['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
|
|
|
|
|
|
|
|
|
|
|
class TemplateResponseMixin(object):
|
|
|
|
"""
|
|
|
|
A mixin that can be used to render a template.
|
|
|
|
"""
|
|
|
|
template_name = 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
|
|
|
"""
|
2012-06-11 16:34:00 +08:00
|
|
|
Returns a response, using the `response_class` for this
|
|
|
|
view, with a template rendered with the given context.
|
|
|
|
|
|
|
|
If any keyword arguments are provided, they will be
|
|
|
|
passed 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,
|
2010-12-07 21:57:01 +08:00
|
|
|
**response_kwargs
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_template_names(self):
|
|
|
|
"""
|
2010-12-07 21:57:01 +08:00
|
|
|
Returns 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):
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2012-08-18 19:52:05 +08:00
|
|
|
A view that renders a template. This view will also pass into the context
|
|
|
|
any keyword arguments passed by the url conf.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
def get(self, request, *args, **kwargs):
|
2012-08-18 19:52:05 +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):
|
|
|
|
"""
|
|
|
|
A view that provides a redirect on any GET request.
|
|
|
|
"""
|
2014-11-22 00:55:58 +08:00
|
|
|
permanent = _sentinel
|
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
|
|
|
|
|
2014-11-22 00:55:58 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if 'permanent' not in kwargs and self.permanent is _sentinel:
|
|
|
|
warnings.warn(
|
|
|
|
"Default value of 'RedirectView.permanent' will change "
|
|
|
|
"from True to False in Django 1.9. Set an explicit value "
|
|
|
|
"to silence this warning.",
|
|
|
|
RemovedInDjango19Warning,
|
2014-11-27 11:44:56 +08:00
|
|
|
stacklevel=2
|
2014-11-22 00:55:58 +08:00
|
|
|
)
|
|
|
|
self.permanent = True
|
|
|
|
super(RedirectView, self).__init__(*args, **kwargs)
|
|
|
|
|
2014-11-27 11:44:56 +08:00
|
|
|
@classonlymethod
|
|
|
|
def as_view(cls, **initkwargs):
|
|
|
|
if 'permanent' not in initkwargs and cls.permanent is _sentinel:
|
|
|
|
warnings.warn(
|
|
|
|
"Default value of 'RedirectView.permanent' will change "
|
|
|
|
"from True to False in Django 1.9. Set an explicit value "
|
|
|
|
"to silence this warning.",
|
|
|
|
RemovedInDjango19Warning,
|
|
|
|
stacklevel=2
|
|
|
|
)
|
|
|
|
initkwargs['permanent'] = True
|
|
|
|
return super(RedirectView, cls).as_view(**initkwargs)
|
|
|
|
|
2013-06-14 18:59:26 +08:00
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
2010-10-18 21:34:47 +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.
|
|
|
|
"""
|
|
|
|
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:
|
|
|
|
try:
|
|
|
|
url = reverse(self.pattern_name, args=args, kwargs=kwargs)
|
|
|
|
except NoReverseMatch:
|
|
|
|
return None
|
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:
|
|
|
|
return http.HttpResponsePermanentRedirect(url)
|
|
|
|
else:
|
|
|
|
return http.HttpResponseRedirect(url)
|
|
|
|
else:
|
2013-12-16 23:45:18 +08:00
|
|
|
logger.warning('Gone: %s', request.path,
|
2010-10-18 21:34:47 +08:00
|
|
|
extra={
|
|
|
|
'status_code': 410,
|
2013-12-16 23:45:18 +08:00
|
|
|
'request': request
|
2010-10-18 21:34:47 +08:00
|
|
|
})
|
|
|
|
return http.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)
|