base.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. from __future__ import unicode_literals
  2. import logging
  3. from functools import update_wrapper
  4. from django import http
  5. from django.core.exceptions import ImproperlyConfigured
  6. from django.core.urlresolvers import reverse, NoReverseMatch
  7. from django.template.response import TemplateResponse
  8. from django.utils.decorators import classonlymethod
  9. from django.utils import six
  10. logger = logging.getLogger('django.request')
  11. class ContextMixin(object):
  12. """
  13. A default context mixin that passes the keyword arguments received by
  14. get_context_data as the template context.
  15. """
  16. def get_context_data(self, **kwargs):
  17. if 'view' not in kwargs:
  18. kwargs['view'] = self
  19. return kwargs
  20. class View(object):
  21. """
  22. Intentionally simple parent class for all views. Only implements
  23. dispatch-by-method and simple sanity checking.
  24. """
  25. http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
  26. def __init__(self, **kwargs):
  27. """
  28. Constructor. Called in the URLconf; can contain helpful extra
  29. keyword arguments, and other things.
  30. """
  31. # Go through keyword arguments, and either save their values to our
  32. # instance, or raise an error.
  33. for key, value in six.iteritems(kwargs):
  34. setattr(self, key, value)
  35. @classonlymethod
  36. def as_view(cls, **initkwargs):
  37. """
  38. Main entry point for a request-response process.
  39. """
  40. # sanitize keyword arguments
  41. for key in initkwargs:
  42. if key in cls.http_method_names:
  43. raise TypeError("You tried to pass in the %s method name as a "
  44. "keyword argument to %s(). Don't do that."
  45. % (key, cls.__name__))
  46. if not hasattr(cls, key):
  47. raise TypeError("%s() received an invalid keyword %r. as_view "
  48. "only accepts arguments that are already "
  49. "attributes of the class." % (cls.__name__, key))
  50. def view(request, *args, **kwargs):
  51. self = cls(**initkwargs)
  52. if hasattr(self, 'get') and not hasattr(self, 'head'):
  53. self.head = self.get
  54. self.request = request
  55. self.args = args
  56. self.kwargs = kwargs
  57. return self.dispatch(request, *args, **kwargs)
  58. # take name and docstring from class
  59. update_wrapper(view, cls, updated=())
  60. # and possible attributes set by decorators
  61. # like csrf_exempt from dispatch
  62. update_wrapper(view, cls.dispatch, assigned=())
  63. return view
  64. def dispatch(self, request, *args, **kwargs):
  65. # Try to dispatch to the right method; if a method doesn't exist,
  66. # defer to the error handler. Also defer to the error handler if the
  67. # request method isn't on the approved list.
  68. if request.method.lower() in self.http_method_names:
  69. handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
  70. else:
  71. handler = self.http_method_not_allowed
  72. return handler(request, *args, **kwargs)
  73. def http_method_not_allowed(self, request, *args, **kwargs):
  74. logger.warning('Method Not Allowed (%s): %s', request.method, request.path,
  75. extra={
  76. 'status_code': 405,
  77. 'request': request
  78. }
  79. )
  80. return http.HttpResponseNotAllowed(self._allowed_methods())
  81. def options(self, request, *args, **kwargs):
  82. """
  83. Handles responding to requests for the OPTIONS HTTP verb.
  84. """
  85. response = http.HttpResponse()
  86. response['Allow'] = ', '.join(self._allowed_methods())
  87. response['Content-Length'] = '0'
  88. return response
  89. def _allowed_methods(self):
  90. return [m.upper() for m in self.http_method_names if hasattr(self, m)]
  91. class TemplateResponseMixin(object):
  92. """
  93. A mixin that can be used to render a template.
  94. """
  95. template_name = None
  96. response_class = TemplateResponse
  97. content_type = None
  98. def render_to_response(self, context, **response_kwargs):
  99. """
  100. Returns a response, using the `response_class` for this
  101. view, with a template rendered with the given context.
  102. If any keyword arguments are provided, they will be
  103. passed to the constructor of the response class.
  104. """
  105. response_kwargs.setdefault('content_type', self.content_type)
  106. return self.response_class(
  107. request=self.request,
  108. template=self.get_template_names(),
  109. context=context,
  110. **response_kwargs
  111. )
  112. def get_template_names(self):
  113. """
  114. Returns a list of template names to be used for the request. Must return
  115. a list. May not be called if render_to_response is overridden.
  116. """
  117. if self.template_name is None:
  118. raise ImproperlyConfigured(
  119. "TemplateResponseMixin requires either a definition of "
  120. "'template_name' or an implementation of 'get_template_names()'")
  121. else:
  122. return [self.template_name]
  123. class TemplateView(TemplateResponseMixin, ContextMixin, View):
  124. """
  125. A view that renders a template. This view will also pass into the context
  126. any keyword arguments passed by the url conf.
  127. """
  128. def get(self, request, *args, **kwargs):
  129. context = self.get_context_data(**kwargs)
  130. return self.render_to_response(context)
  131. class RedirectView(View):
  132. """
  133. A view that provides a redirect on any GET request.
  134. """
  135. permanent = True
  136. url = None
  137. pattern_name = None
  138. query_string = False
  139. def get_redirect_url(self, *args, **kwargs):
  140. """
  141. Return the URL redirect to. Keyword arguments from the
  142. URL pattern match generating the redirect request
  143. are provided as kwargs to this method.
  144. """
  145. if self.url:
  146. url = self.url % kwargs
  147. elif self.pattern_name:
  148. try:
  149. url = reverse(self.pattern_name, args=args, kwargs=kwargs)
  150. except NoReverseMatch:
  151. return None
  152. else:
  153. return None
  154. args = self.request.META.get('QUERY_STRING', '')
  155. if args and self.query_string:
  156. url = "%s?%s" % (url, args)
  157. return url
  158. def get(self, request, *args, **kwargs):
  159. url = self.get_redirect_url(*args, **kwargs)
  160. if url:
  161. if self.permanent:
  162. return http.HttpResponsePermanentRedirect(url)
  163. else:
  164. return http.HttpResponseRedirect(url)
  165. else:
  166. logger.warning('Gone: %s', request.path,
  167. extra={
  168. 'status_code': 410,
  169. 'request': request
  170. })
  171. return http.HttpResponseGone()
  172. def head(self, request, *args, **kwargs):
  173. return self.get(request, *args, **kwargs)
  174. def post(self, request, *args, **kwargs):
  175. return self.get(request, *args, **kwargs)
  176. def options(self, request, *args, **kwargs):
  177. return self.get(request, *args, **kwargs)
  178. def delete(self, request, *args, **kwargs):
  179. return self.get(request, *args, **kwargs)
  180. def put(self, request, *args, **kwargs):
  181. return self.get(request, *args, **kwargs)
  182. def patch(self, request, *args, **kwargs):
  183. return self.get(request, *args, **kwargs)