app/soc/views/helper/responses.py
changeset 272 00cea07656c0
parent 269 0f1acc4c3e1e
child 276 56357a92c110
equal deleted inserted replaced
271:01e90bb21b7e 272:00cea07656c0
       
     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 render response.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Todd Larsen" <tlarsen@google.com>',
       
    22   '"Pawel Solyga" <pawel.solyga@gmail.com>',
       
    23   ]
       
    24 
       
    25 
       
    26 import logging
       
    27 
       
    28 from google.appengine.api import users
       
    29 
       
    30 from django import http
       
    31 from django.template import loader
       
    32 
       
    33 # DeadlineExceededError can live in two different places
       
    34 try:
       
    35   # When deployed
       
    36   from google.appengine.runtime import DeadlineExceededError
       
    37 except ImportError:
       
    38   # In the development server
       
    39   from google.appengine.runtime.apiproxy_errors import DeadlineExceededError
       
    40 
       
    41 from soc.logic import system
       
    42 from soc.logic.site import id_user
       
    43 from soc.logic.site import sidebar
       
    44 from soc.views import helper
       
    45 import soc.views.helper.requests
       
    46 from soc.views.helpers import html_menu
       
    47 
       
    48 
       
    49 def respond(request, template, context=None, response_args=None):
       
    50   """Helper to render a response, passing standard stuff to the response.
       
    51 
       
    52   Args:
       
    53     request: the Django HTTP request object
       
    54     template: the template (or search list of templates) to render
       
    55     context: the context supplied to the template (implements dict)
       
    56     response_args: keyword arguments passed to http.HttpResponse()
       
    57       (response_args['content'] is created with
       
    58       render_to_string(template, dictionary=context) if it is not present)
       
    59 
       
    60   Returns:
       
    61     django.shortcuts.render_to_response(template, context) results
       
    62 
       
    63   Raises:
       
    64     Any exceptions that django.template.loader.render_to_string() or
       
    65     django.http.HttpResponse() might raise.
       
    66   """
       
    67   context = getUniversalContext(request, context=context)
       
    68 
       
    69   if response_args is None:
       
    70     response_args = {}
       
    71 
       
    72   try:
       
    73     response_args['content'] = response_args.get(
       
    74         'content', loader.render_to_string(template, dictionary=context))
       
    75     return http.HttpResponse(**response_args)
       
    76   except DeadlineExceededError:
       
    77     logging.exception('DeadlineExceededError')
       
    78     return http.HttpResponse('DeadlineExceededError')
       
    79   except MemoryError:
       
    80     logging.exception('MemoryError')
       
    81     return http.HttpResponse('MemoryError')
       
    82   except AssertionError:
       
    83     logging.exception('AssertionError')
       
    84     return http.HttpResponse('AssertionError')
       
    85 
       
    86 
       
    87 def getUniversalContext(request, context=None):
       
    88   """Constructs a template context dict will many common variables defined.
       
    89   
       
    90   Args:
       
    91     request: the Django HTTP request object
       
    92     context: the template context dict to be updated in-place (pass in a copy
       
    93       if the original must not be modified), or None if a new one is to be
       
    94       created; any existing fields already present in the context dict passed
       
    95       in by the caller are left unaltered 
       
    96       
       
    97   Returns:
       
    98     updated template context dict supplied by the caller, or a new context
       
    99     dict if the caller supplied None
       
   100     
       
   101     {
       
   102       'request': the Django HTTP request object passed in by the caller
       
   103       'id': the logged-in Google Account if there is one
       
   104       'user': the User entity corresponding to the Google Account in
       
   105         context['id']
       
   106       'is_admin': True if users.is_current_user_admin() is True
       
   107       'is_debug': True if system.isDebug() is True
       
   108       'sign_in': a Google Account login URL
       
   109       'sign_out': a Google Account logout URL
       
   110       'sidebar_menu_html': an HTML string that renders the sidebar menu
       
   111     }
       
   112   """
       
   113   if context is None:
       
   114     context = {}
       
   115 
       
   116   # set some universal values if caller did not already set them  
       
   117   context['request'] = context.get('request', request)
       
   118   context['id'] = id_user.getIdIfMissing(context.get('id', None))
       
   119   context['user'] = id_user.getUserIfMissing(context.get('user', None),
       
   120                                              context['id'])
       
   121   context['is_admin'] = context.get(
       
   122       'is_admin', id_user.isIdDeveloper(id=context['id']))
       
   123   context['is_debug'] = context.get('is_debug', system.isDebug())
       
   124   context['sign_in'] = context.get(
       
   125       'sign_in', users.create_login_url(request.path))
       
   126   context['sign_out'] = context.get(
       
   127       'sign_out', users.create_logout_url(request.path))
       
   128 
       
   129   if not context.get('sidebar_menu_html'):
       
   130     # pass the currently constructed context as keyword arguments to
       
   131     # all of the sidebar builder functions
       
   132     context['sidebar_menu_html'] = str(html_menu.UlMenu(
       
   133       sidebar.buildSidebar(**context)))
       
   134 
       
   135   return context
       
   136 
       
   137 
       
   138 def redirectToChangedSuffix(
       
   139     request, old_suffix, new_suffix=None, params=None):
       
   140   """Changes suffix of URL path and returns an HTTP redirect response.
       
   141   
       
   142   Args:
       
   143     request: the Django HTTP request object; redirect path is derived from
       
   144       request.path
       
   145     old_suffix, new_suffix, params:  see helper.requests.replaceSuffix()
       
   146       
       
   147   Returns:
       
   148     a Django HTTP redirect response pointing to the altered path
       
   149   """
       
   150   path = helper.requests.replaceSuffix(request.path, old_suffix, new_suffix,
       
   151                                        params=params)
       
   152   return http.HttpResponseRedirect(path)