app/soc/views/helpers/response_helpers.py
changeset 83 3f4f7c540b75
child 99 8c38b546a3cf
equal deleted inserted replaced
82:1456e633bf8a 83:3f4f7c540b75
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Helpers used to display various views that are forms.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Pawel Solyga" <pawel.solyga@gmail.com>',
       
    22   ]
       
    23 
       
    24 import os
       
    25 
       
    26 from google.appengine.api import users
       
    27 from django import http
       
    28 from django import shortcuts
       
    29 
       
    30 IS_DEV = os.environ['SERVER_SOFTWARE'].startswith('Dev')
       
    31 
       
    32 # DeadlineExceededError can live in two different places
       
    33 try:
       
    34   # When deployed
       
    35   from google.appengine.runtime import DeadlineExceededError
       
    36 except ImportError:
       
    37   # In the development server
       
    38   from google.appengine.runtime.apiproxy_errors import DeadlineExceededError
       
    39 
       
    40 def respond(request, template, params=None):
       
    41   """Helper to render a response, passing standard stuff to the response.
       
    42 
       
    43   Args:
       
    44     request: The request object.
       
    45     template: The template name; '.html' is appended automatically.
       
    46     params: A dict giving the template parameters; modified in-place.
       
    47 
       
    48   Returns:
       
    49     Whatever render_to_response(template, params) returns.
       
    50 
       
    51   Raises:
       
    52     Whatever render_to_response(template, params) raises.
       
    53   """
       
    54   if params is None:
       
    55     params = {}
       
    56   
       
    57   params['request'] = request
       
    58   params['user'] = users.get_current_user()
       
    59   params['is_admin'] = users.is_current_user_admin()
       
    60   params['is_dev'] = IS_DEV
       
    61   params['sign_in'] = users.create_login_url(request.path)
       
    62   params['sign_out'] = users.create_logout_url(request.path)
       
    63   try:
       
    64     return shortcuts.render_to_response(template, params)
       
    65   except DeadlineExceededError:
       
    66     logging.exception('DeadlineExceededError')
       
    67     return http.HttpResponse('DeadlineExceededError')
       
    68   except MemoryError:
       
    69     logging.exception('MemoryError')
       
    70     return http.HttpResponse('MemoryError')
       
    71   except AssertionError:
       
    72     logging.exception('AssertionError')
       
    73     return http.HttpResponse('AssertionError')