2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2012-09-21 03:03:24 +08:00
|
|
|
import logging
|
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
|
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
|
2012-07-21 03:14:27 +08:00
|
|
|
from django.utils import six
|
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
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
http_method_names = ['get', 'post', 'put', 'delete', 'head', 'options', 'trace']
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
# sanitize keyword arguments
|
|
|
|
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-06-08 00:08:47 +08:00
|
|
|
raise TypeError("%s() received an invalid keyword %r" % (
|
2010-10-18 21:34:47 +08:00
|
|
|
cls.__name__, key))
|
|
|
|
|
|
|
|
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
|
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
|
|
|
|
self.request = request
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
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,
|
|
|
|
'request': self.request
|
|
|
|
}
|
|
|
|
)
|
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())
|
|
|
|
response['Content-Length'] = 0
|
|
|
|
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
|
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
|
|
|
"""
|
2010-12-07 21:57:01 +08:00
|
|
|
return self.response_class(
|
|
|
|
request = self.request,
|
|
|
|
template = self.get_template_names(),
|
|
|
|
context = context,
|
|
|
|
**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.
|
|
|
|
"""
|
|
|
|
permanent = True
|
|
|
|
url = None
|
|
|
|
query_string = False
|
|
|
|
|
|
|
|
def get_redirect_url(self, **kwargs):
|
|
|
|
"""
|
|
|
|
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
|
2011-10-07 04:39:15 +08:00
|
|
|
args = self.request.META.get('QUERY_STRING', '')
|
2010-10-18 21:34:47 +08:00
|
|
|
if args and self.query_string:
|
2012-03-03 00:55:56 +08:00
|
|
|
url = "%s?%s" % (url, args)
|
|
|
|
return url
|
2010-10-18 21:34:47 +08:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
url = self.get_redirect_url(**kwargs)
|
|
|
|
if url:
|
|
|
|
if self.permanent:
|
|
|
|
return http.HttpResponsePermanentRedirect(url)
|
|
|
|
else:
|
|
|
|
return http.HttpResponseRedirect(url)
|
|
|
|
else:
|
2012-02-10 02:58:36 +08:00
|
|
|
logger.warning('Gone: %s', self.request.path,
|
2010-10-18 21:34:47 +08:00
|
|
|
extra={
|
|
|
|
'status_code': 410,
|
|
|
|
'request': self.request
|
|
|
|
})
|
|
|
|
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)
|