Skip to content
This repository has been archived by the owner on Feb 21, 2020. It is now read-only.

Commit

Permalink
Add render method for composing an HttpResponse from the handler
Browse files Browse the repository at this point in the history
This provides a clean way to override/extend building an HttpResponse
in subclasses.
  • Loading branch information
bruth committed Jan 2, 2013
1 parent afec7c6 commit bc5227e
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions restlib2/resources.py
Expand Up @@ -241,11 +241,30 @@ def dispatch(self, request, *args, **kwargs):
method_handler = getattr(self, request.method.lower()) method_handler = getattr(self, request.method.lower())
response = method_handler(request, *args, **kwargs) response = method_handler(request, *args, **kwargs)


# If the return value of the handler is not a response, pass
# the return value into the render method.
if not isinstance(response, HttpResponse):
response = self.render(request, response, *args, **kwargs)

# Process the response, check if the response is overridden and # Process the response, check if the response is overridden and
# use that instead. # use that instead.
# TODO not sure if this is sound for a simple resource
return self.process_response(request, response) return self.process_response(request, response)


def render(self, request, content, *args, **kwargs):
"Renders the response based on the content returned from the handler."
response = HttpResponse()

if request.method != methods.HEAD:
if isinstance(content, (basestring, file)):
response.content = content
else:
accept_type = getattr(request, '_accept_type', None)
if accept_type and serializers.supports_encoding(accept_type):
response.content = serializers.encode(accept_type, content)
response['Content-Type'] = accept_type

return response

# ## Request Method Handlers # ## Request Method Handlers
# ### _HEAD_ Request Handler # ### _HEAD_ Request Handler
# Default handler for _HEAD_ requests. For this to be available, # Default handler for _HEAD_ requests. For this to be available,
Expand Down Expand Up @@ -525,17 +544,6 @@ def content_language_supported(self, request, response, *args, **kwargs):
return True return True


# Utility methods # Utility methods
def write(self, request, response, content, accept_type=None):
if content is None:
response.status_code = codes.no_content
elif isinstance(content, basestring) or isinstance(content, file):
response.content = content
else:
accept_type = accept_type or getattr(request, '_accept_type', None)
if accept_type and serializers.supports_encoding(accept_type):
response.content = serializers.encode(accept_type, content)
response['Content-Type'] = accept_type

def get_cache(self, request, response): def get_cache(self, request, response):
"Returns the cache to be used for various components." "Returns the cache to be used for various components."
from django.core.cache import cache from django.core.cache import cache
Expand Down Expand Up @@ -729,16 +737,12 @@ def process_request(self, request, *args, **kwargs):
# ## Process the normal response returned by the handler # ## Process the normal response returned by the handler
def process_response(self, request, response): def process_response(self, request, response):
if not isinstance(response, HttpResponse): if not isinstance(response, HttpResponse):
content = response response = HttpResponse(response)
response = HttpResponse()

if request.method != methods.HEAD:
self.write(request, response, content)


if request.method == methods.HEAD: if request.method == methods.HEAD:
response.content = '' response.content = ''


if request.method == methods.GET or request.method == methods.HEAD: if request.method in (methods.GET, methods.HEAD):
self.response_cache_control(request, response) self.response_cache_control(request, response)


if self.use_etags: if self.use_etags:
Expand Down

0 comments on commit bc5227e

Please sign in to comment.