aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Christie2012-09-05 09:54:46 +0100
committerTom Christie2012-09-05 09:54:46 +0100
commitda4fa9bded9bbbbacf2a3009e1e211bdd51e287a (patch)
tree6fe9ac0aeaae3b5257342d1b97602503ac7800d1
parentc28b719333b16935e53c76fef79b096cb11322ed (diff)
downloaddjango-rest-framework-da4fa9bded9bbbbacf2a3009e1e211bdd51e287a.tar.bz2
Minor tweaks
-rw-r--r--djangorestframework/mixins.py43
-rw-r--r--djangorestframework/request.py3
-rw-r--r--djangorestframework/views.py21
3 files changed, 35 insertions, 32 deletions
diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py
index 7269f298..2f78876f 100644
--- a/djangorestframework/mixins.py
+++ b/djangorestframework/mixins.py
@@ -2,27 +2,6 @@ from djangorestframework import status
from djangorestframework.response import Response
-class MetadataMixin(object):
- """
- Should be mixed in with any `BaseView`.
- """
- def metadata(self, request, *args, **kwargs):
- content = {
- 'name': self.get_name(),
- 'description': self.get_description(),
- 'renders': self._rendered_media_types,
- 'parses': self._parsed_media_types,
- }
- # TODO: Add 'fields', from serializer info.
- # form = self.get_bound_form()
- # if form is not None:
- # field_name_types = {}
- # for name, field in form.fields.iteritems():
- # field_name_types[name] = field.__class__.__name__
- # content['fields'] = field_name_types
- raise Response(content, status=status.HTTP_200_OK)
-
-
class CreateModelMixin(object):
"""
Create a model instance.
@@ -83,3 +62,25 @@ class DestroyModelMixin(object):
self.object = self.get_object()
self.object.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
+
+
+class MetadataMixin(object):
+ """
+ Return a dicitonary of view metadata.
+ Should be mixed in with any `BaseView`.
+ """
+ def metadata(self, request, *args, **kwargs):
+ content = {
+ 'name': self.get_name(),
+ 'description': self.get_description(),
+ 'renders': self._rendered_media_types,
+ 'parses': self._parsed_media_types,
+ }
+ # TODO: Add 'fields', from serializer info.
+ # form = self.get_bound_form()
+ # if form is not None:
+ # field_name_types = {}
+ # for name, field in form.fields.iteritems():
+ # field_name_types[name] = field.__class__.__name__
+ # content['fields'] = field_name_types
+ raise Response(content, status=status.HTTP_200_OK)
diff --git a/djangorestframework/request.py b/djangorestframework/request.py
index 2e4e8909..99e50353 100644
--- a/djangorestframework/request.py
+++ b/djangorestframework/request.py
@@ -234,6 +234,9 @@ class Request(object):
user = authentication.authenticate(self)
if user:
return user
+ return self._not_authenticated()
+
+ def _not_authenticated(self):
return AnonymousUser()
def __getattr__(self, name):
diff --git a/djangorestframework/views.py b/djangorestframework/views.py
index 9796b362..5f967782 100644
--- a/djangorestframework/views.py
+++ b/djangorestframework/views.py
@@ -224,18 +224,19 @@ class APIView(_View):
def initial(self, request, *args, **kargs):
"""
- This method is a hook for any code that needs to run prior to
- anything else.
- Required if you want to do things like set `request.upload_handlers`
- before the authentication and dispatch handling is run.
+ This method runs prior to anything else in the view.
+ It should return the initial request object.
+
+ You may need to override this if you want to do things like set
+ `request.upload_handlers` before the authentication and dispatch
+ handling is run.
"""
- pass
+ return Request(request, parsers=self.parsers, authentication=self.authentication)
def final(self, request, response, *args, **kargs):
"""
- This method is a hook for any code that needs to run after everything
- else in the view.
- Returns the final response object.
+ This method runs after everything else in the view.
+ It should return the final response object.
"""
if isinstance(response, Response):
response.view = self
@@ -269,14 +270,12 @@ class APIView(_View):
# all other authentication is CSRF exempt.
@csrf_exempt
def dispatch(self, request, *args, **kwargs):
- request = Request(request, parsers=self.parsers, authentication=self.authentication)
- self.request = request
self.args = args
self.kwargs = kwargs
self.headers = self.default_response_headers
try:
- self.initial(request, *args, **kwargs)
+ self.request = self.initial(request, *args, **kwargs)
# Check that the request is allowed
self.check_permissions(request)