app/soc/views/simple.py
changeset 119 50d8d58dcd7c
child 122 95d0d954fe0b
equal deleted inserted replaced
118:d2e61a490969 119:50d8d58dcd7c
       
     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 """Simple views that depend entirely on the template and context.
       
    18 
       
    19 simpleWithLinkName: a simple template view for URLs with a linkname
       
    20 
       
    21 errorResponse: renders an out_of_band.ErrorResponse page
       
    22 """
       
    23 
       
    24 __authors__ = [
       
    25   '"Todd Larsen" <tlarsen@google.com>',
       
    26   ]
       
    27 
       
    28 
       
    29 from django import http
       
    30 from django.template import loader
       
    31 
       
    32 from soc.logic import out_of_band
       
    33 from soc.logic.site import id_user
       
    34 from soc.views.helpers import response_helpers
       
    35 from soc.views.helpers import template_helpers
       
    36 
       
    37 
       
    38 def templateWithLinkName(request,
       
    39                          template='soc/base.html', linkname=None,
       
    40                          context=None):
       
    41   """A simple template view that expects a linkname extracted from the URL. 
       
    42 
       
    43   Args:
       
    44     request: the standard Django HTTP request object
       
    45     template: the template to use for rendering the view (or a search list
       
    46       of templates)
       
    47     linkname: a site-unique "linkname" (usually extracted from the URL)
       
    48     context: the context dict supplied to the template, which is modified
       
    49         (so supply a copy if such modification is not acceptable)
       
    50       linkname: the linkname parameter is added to the context
       
    51       linkname_user: if the linkname exists for a User, that User
       
    52         is added to the context
       
    53 
       
    54   Returns:
       
    55     A subclass of django.http.HttpResponse containing the generated page.
       
    56   """
       
    57   context['linkname'] = linkname
       
    58   context = response_helpers.getUniversalContext(request, context=context)
       
    59 
       
    60   try:
       
    61     context['linkname_user'] = id_user.getUserIfLinkName(linkname)
       
    62   except out_of_band.ErrorResponse, error:
       
    63     return errorResponse(request, error, template, context)
       
    64 
       
    65   return response_helpers.respond(request, template, context)
       
    66 
       
    67 
       
    68 DEF_ERROR_TMPL = 'soc/error.html'
       
    69 
       
    70 def errorResponse(request, error, template, context):
       
    71   """Displays an error page for an out_of_band.ErrorResponse exception.
       
    72   
       
    73   Args:
       
    74     request: the standard Django HTTP request object
       
    75     error: an out_of_band.ErrorResponse exception
       
    76     template: the "sibling" template (or a search list of such templates)
       
    77       from which to construct the error.html template name (or names)
       
    78     context: the context dict supplied to the template, which is modified
       
    79         (so supply a copy if such modification is not acceptable)
       
    80       error_message: the error message string from error.message
       
    81       error_status: error.response_args['status'], or None if a status code
       
    82         was not supplied to the ErrorResponse
       
    83   """
       
    84   context = response_helpers.getUniversalContext(request, context=context)
       
    85   
       
    86   # make a list of possible "sibling" templates, then append a default
       
    87   error_templates = template_helpers.makeSiblingTemplatesList(template,
       
    88                                                               'error.html')
       
    89   error_templates.append(DEF_ERROR_TMPL)
       
    90 
       
    91   context['error_status'] = error.response_args.get('status')
       
    92   context['error_message'] = error.message
       
    93 
       
    94   return response_helpers.respond(request, error_templates, context=context,
       
    95                                   response_args=error.response_args)