diff options
| author | Tom Christie | 2011-05-10 12:21:48 +0100 |
|---|---|---|
| committer | Tom Christie | 2011-05-10 12:21:48 +0100 |
| commit | 527e4ffdf7f7798dc17757a26d8fd6b155a49bf9 (patch) | |
| tree | 8a7811d692c136e78dd3ad64a6219d30f501d194 /djangorestframework/views.py | |
| parent | 8f58ee489d34b200acfc2666816eb32e47c8cef5 (diff) | |
| download | django-rest-framework-527e4ffdf7f7798dc17757a26d8fd6b155a49bf9.tar.bz2 | |
renderer API work
Diffstat (limited to 'djangorestframework/views.py')
| -rw-r--r-- | djangorestframework/views.py | 104 |
1 files changed, 55 insertions, 49 deletions
diff --git a/djangorestframework/views.py b/djangorestframework/views.py index 02251885..3ce4e1d6 100644 --- a/djangorestframework/views.py +++ b/djangorestframework/views.py @@ -7,11 +7,13 @@ from djangorestframework.mixins import * from djangorestframework import resource, renderers, parsers, authentication, permissions, validators, status -__all__ = ('BaseView', - 'ModelView', - 'InstanceModelView', - 'ListOrModelView', - 'ListOrCreateModelView') +__all__ = ( + 'BaseView', + 'ModelView', + 'InstanceModelView', + 'ListOrModelView', + 'ListOrCreateModelView' +) @@ -78,55 +80,59 @@ class BaseView(RequestMixin, ResponseMixin, AuthMixin, View): # all other authentication is CSRF exempt. @csrf_exempt def dispatch(self, request, *args, **kwargs): - self.request = request - self.args = args - self.kwargs = kwargs - - # Calls to 'reverse' will not be fully qualified unless we set the scheme/host/port here. - prefix = '%s://%s' % (request.is_secure() and 'https' or 'http', request.get_host()) - set_script_prefix(prefix) - try: - # If using a form POST with '_method'/'_content'/'_content_type' overrides, then alter - # self.method, self.content_type, self.RAW_CONTENT & self.CONTENT appropriately. - self.perform_form_overloading() - - # Authenticate and check request is has the relevant permissions - self._check_permissions() - - # Get the appropriate handler method - if self.method.lower() in self.http_method_names: - handler = getattr(self, self.method.lower(), self.http_method_not_allowed) - else: - handler = self.http_method_not_allowed - - response_obj = handler(request, *args, **kwargs) - - # Allow return value to be either Response, or an object, or None - if isinstance(response_obj, Response): - response = response_obj - elif response_obj is not None: - response = Response(status.HTTP_200_OK, response_obj) - else: - response = Response(status.HTTP_204_NO_CONTENT) - - # Pre-serialize filtering (eg filter complex objects into natively serializable types) - response.cleaned_content = self.resource.object_to_serializable(response.raw_content) + self.request = request + self.args = args + self.kwargs = kwargs + + # Calls to 'reverse' will not be fully qualified unless we set the scheme/host/port here. + prefix = '%s://%s' % (request.is_secure() and 'https' or 'http', request.get_host()) + set_script_prefix(prefix) + + try: + # If using a form POST with '_method'/'_content'/'_content_type' overrides, then alter + # self.method, self.content_type, self.RAW_CONTENT & self.CONTENT appropriately. + self.perform_form_overloading() + + # Authenticate and check request is has the relevant permissions + self._check_permissions() + + # Get the appropriate handler method + if self.method.lower() in self.http_method_names: + handler = getattr(self, self.method.lower(), self.http_method_not_allowed) + else: + handler = self.http_method_not_allowed + + response_obj = handler(request, *args, **kwargs) - except ErrorResponse, exc: - response = exc.response + # Allow return value to be either Response, or an object, or None + if isinstance(response_obj, Response): + response = response_obj + elif response_obj is not None: + response = Response(status.HTTP_200_OK, response_obj) + else: + response = Response(status.HTTP_204_NO_CONTENT) + + # Pre-serialize filtering (eg filter complex objects into natively serializable types) + response.cleaned_content = self.resource.object_to_serializable(response.raw_content) + + except ErrorResponse, exc: + response = exc.response + except: + import traceback + traceback.print_exc() + + # Always add these headers. + # + # TODO - this isn't actually the correct way to set the vary header, + # also it's currently sub-obtimal for HTTP caching - need to sort that out. + response.headers['Allow'] = ', '.join(self.allowed_methods) + response.headers['Vary'] = 'Authenticate, Accept' + + return self.render(response) except: import traceback traceback.print_exc() - - # Always add these headers. - # - # TODO - this isn't actually the correct way to set the vary header, - # also it's currently sub-obtimal for HTTP caching - need to sort that out. - response.headers['Allow'] = ', '.join(self.allowed_methods) - response.headers['Vary'] = 'Authenticate, Accept' - - return self.render(response) |
