app/soc/views/helper/responses.py
changeset 1398 e6a11f0dba68
parent 1357 3dd1507aa723
child 1454 37eb949c3267
equal deleted inserted replaced
1397:c63de68dadec 1398:e6a11f0dba68
    41 import soc.logic
    41 import soc.logic
    42 import soc.logic.models.user
    42 import soc.logic.models.user
    43 import soc.views.helper.requests
    43 import soc.views.helper.requests
    44 
    44 
    45 
    45 
    46 def respond(request, template, context=None, response_args=None):
    46 def respond(request, template, context=None, response_args=None,
       
    47             response_headers=None):
    47   """Helper to render a response, passing standard stuff to the response.
    48   """Helper to render a response, passing standard stuff to the response.
    48 
    49 
    49   Args:
    50   Args:
    50     request: the Django HTTP request object
    51     request: the Django HTTP request object
    51     template: the template (or search list of templates) to render
    52     template: the template (or search list of templates) to render
    52     context: the context supplied to the template (implements dict)
    53     context: the context supplied to the template (implements dict)
    53     response_args: keyword arguments passed to http.HttpResponse()
    54     response_args: keyword arguments passed to http.HttpResponse()
    54       (response_args['content'] is created with
    55       (response_args['content'] is created with
    55       render_to_string(template, dictionary=context) if it is not present)
    56       render_to_string(template, dictionary=context) if it is not present)
       
    57     response_headers: optional dict containing HTTP response header names
       
    58       and corresponding values to set in the HttpResponse object before it
       
    59       is returned; default is None
    56 
    60 
    57   Returns:
    61   Returns:
    58     django.shortcuts.render_to_response(template, context) results
    62     django.shortcuts.render_to_response(template, context) results
    59 
    63 
    60   Raises:
    64   Raises:
    68   if response_args is None:
    72   if response_args is None:
    69     response_args = {}
    73     response_args = {}
    70 
    74 
    71   response_args['content'] = response_args.get(
    75   response_args['content'] = response_args.get(
    72       'content', loader.render_to_string(template, dictionary=context))
    76       'content', loader.render_to_string(template, dictionary=context))
    73   return http.HttpResponse(**response_args)
    77   http_response = http.HttpResponse(**response_args)
       
    78 
       
    79   if response_headers:
       
    80     for key, value in response_headers.iteritems():
       
    81       http_response[key] = value
       
    82 
       
    83   return http_response
    74 
    84 
    75 
    85 
    76 def getUniversalContext(request):
    86 def getUniversalContext(request):
    77   """Constructs a template context dict will many common variables defined.
    87   """Constructs a template context dict will many common variables defined.
    78   
    88