app/soc/views/helpers/response_helpers.py
changeset 119 50d8d58dcd7c
parent 116 68b5ce15fff9
child 128 f6abf00efc62
equal deleted inserted replaced
118:d2e61a490969 119:50d8d58dcd7c
    24 
    24 
    25 
    25 
    26 from google.appengine.api import users
    26 from google.appengine.api import users
    27 
    27 
    28 from django import http
    28 from django import http
    29 from django import shortcuts
    29 from django.template import loader
    30 
    30 
    31 # DeadlineExceededError can live in two different places
    31 # DeadlineExceededError can live in two different places
    32 try:
    32 try:
    33   # When deployed
    33   # When deployed
    34   from google.appengine.runtime import DeadlineExceededError
    34   from google.appengine.runtime import DeadlineExceededError
    38 
    38 
    39 from soc.logic import system
    39 from soc.logic import system
    40 from soc.logic.site import id_user
    40 from soc.logic.site import id_user
    41 
    41 
    42 
    42 
    43 def respond(request, template, context=None):
    43 def respond(request, template, context=None, response_args=None):
    44   """Helper to render a response, passing standard stuff to the response.
    44   """Helper to render a response, passing standard stuff to the response.
    45 
    45 
    46   Args:
    46   Args:
    47     request: the Django HTTP request object
    47     request: the Django HTTP request object
    48     template: the template (or search list of templates) to render
    48     template: the template (or search list of templates) to render
    49     context: the context supplied to the template (implements dict)
    49     context: the context supplied to the template (implements dict)
       
    50     response_args: keyword arguments passed to http.HttpResponse()
       
    51       (response_args['content'] is created with
       
    52       render_to_string(template, dictionary=context) if it is not present)
    50 
    53 
    51   Returns:
    54   Returns:
    52     django.shortcuts.render_to_response(template, context) results
    55     django.shortcuts.render_to_response(template, context) results
    53 
    56 
    54   Raises:
    57   Raises:
    55     Whatever django.shortcuts.render_to_response(template, context) raises.
    58     Any exceptions that django.template.loader.render_to_string() or
       
    59     django.http.HttpResponse() might raise.
    56   """
    60   """
    57   context = getUniversalContext(request, context=context)
    61   context = getUniversalContext(request, context=context)
    58 
    62 
       
    63   if response_args is None:
       
    64     response_args = {}
       
    65 
    59   try:
    66   try:
    60     return shortcuts.render_to_response(template, context)
    67     response_args['content'] = response_args.get(
       
    68         'content', loader.render_to_string(template, dictionary=context))
       
    69     return http.HttpResponse(**response_args)
    61   except DeadlineExceededError:
    70   except DeadlineExceededError:
    62     logging.exception('DeadlineExceededError')
    71     logging.exception('DeadlineExceededError')
    63     return http.HttpResponse('DeadlineExceededError')
    72     return http.HttpResponse('DeadlineExceededError')
    64   except MemoryError:
    73   except MemoryError:
    65     logging.exception('MemoryError')
    74     logging.exception('MemoryError')