app/soc/views/helpers/response_helpers.py
author Todd Larsen <tlarsen@google.com>
Fri, 19 Sep 2008 04:28:17 +0000
changeset 167 13e438623ded
parent 135 a7ccde9d9eed
child 169 a9b3d6c9d4f9
permissions -rw-r--r--
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to implement the TODO in the User profile views where the link name portion of the URL needs to change when the link_name Property is updated by the form.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
#
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
#
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
#
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
#
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
128
f6abf00efc62 Updated docs in response_helpers.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 119
diff changeset
    17
"""Helpers used to render response.
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
"""
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
__authors__ = [
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    21
  '"Todd Larsen" <tlarsen@google.com>',
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
  ]
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
    26
import urlparse
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
    27
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    28
from google.appengine.api import users
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    29
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    30
from django import http
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    31
from django.template import loader
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    32
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    33
# DeadlineExceededError can live in two different places
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    34
try:
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    35
  # When deployed
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    36
  from google.appengine.runtime import DeadlineExceededError
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    37
except ImportError:
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    38
  # In the development server
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    39
  from google.appengine.runtime.apiproxy_errors import DeadlineExceededError
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    40
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    41
from soc.logic import system
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    42
from soc.logic.site import id_user
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    43
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    44
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    45
def respond(request, template, context=None, response_args=None):
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    46
  """Helper to render a response, passing standard stuff to the response.
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    47
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    48
  Args:
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    49
    request: the Django HTTP request object
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    50
    template: the template (or search list of templates) to render
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    51
    context: the context supplied to the template (implements dict)
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    52
    response_args: keyword arguments passed to http.HttpResponse()
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    53
      (response_args['content'] is created with
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    54
      render_to_string(template, dictionary=context) if it is not present)
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    55
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    56
  Returns:
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    57
    django.shortcuts.render_to_response(template, context) results
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    58
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    59
  Raises:
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    60
    Any exceptions that django.template.loader.render_to_string() or
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    61
    django.http.HttpResponse() might raise.
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    62
  """
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    63
  context = getUniversalContext(request, context=context)
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    64
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    65
  if response_args is None:
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    66
    response_args = {}
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    67
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    68
  try:
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    69
    response_args['content'] = response_args.get(
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    70
        'content', loader.render_to_string(template, dictionary=context))
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    71
    return http.HttpResponse(**response_args)
83
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    72
  except DeadlineExceededError:
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    73
    logging.exception('DeadlineExceededError')
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    74
    return http.HttpResponse('DeadlineExceededError')
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    75
  except MemoryError:
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    76
    logging.exception('MemoryError')
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    77
    return http.HttpResponse('MemoryError')
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    78
  except AssertionError:
3f4f7c540b75 Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    79
    logging.exception('AssertionError')
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    80
    return http.HttpResponse('AssertionError')
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    81
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    82
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    83
def getUniversalContext(request, context=None):
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    84
  """Constructs a template context dict will many common variables defined.
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    85
  
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    86
  Args:
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    87
    request: the Django HTTP request object
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    88
    context: the template context dict to be updated in-place (pass in a copy
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    89
      if the original must not be modified), or None if a new one is to be
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    90
      created; any existing fields already present in the context dict passed
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    91
      in by the caller are left unaltered 
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    92
      
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    93
  Returns:
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    94
    updated template context dict supplied by the caller, or a new context
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    95
    dict if the caller supplied None
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    96
    
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    97
    {
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    98
      'request': the Django HTTP request object passed in by the caller
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    99
      'id': the logged-in Google Account if there is one
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   100
      'user': the User entity corresponding to the Google Account in
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   101
        context['id']
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   102
      'is_admin': True if users.is_current_user_admin() is True
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   103
      'is_debug': True if system.isDebug() is True
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   104
      'sign_in': a Google Account login URL
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   105
      'sign_out': a Google Account logout URL
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   106
    }
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   107
  """
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   108
  if context is None:
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   109
    context = {}
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   110
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   111
  # set some universal values if caller did not already set them  
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   112
  context['request'] = context.get('request', request)
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   113
  context['id'] = id_user.getIdIfMissing(context.get('id', None))
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   114
  context['user'] = id_user.getUserIfMissing(context.get('user', None),
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   115
                                             context['id'])
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   116
  context['is_admin'] = context.get(
135
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   117
      'is_admin', id_user.isIdDeveloper(id=context['id']))
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   118
  context['is_debug'] = context.get('is_debug', system.isDebug())
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   119
  context['sign_in'] = context.get(
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   120
      'sign_in', users.create_login_url(request.path))
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   121
  context['sign_out'] = context.get(
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   122
      'sign_out', users.create_logout_url(request.path))
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   123
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   124
  return context
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   125
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   126
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   127
def replaceSuffix(path, old_suffix, new_suffix, params=None):
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   128
  """Replace the last part of a URL path with something else.
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   129
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   130
  Also appends an optional list of query parameters.  Used for
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   131
  replacing, for example, one link name at the end of a relative
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   132
  URL path with another.
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   133
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   134
  Args:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   135
    path: HTTP request relative URL path (with no query arguments)
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   136
    old_suffix: expected suffix at the end of request.path component;
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   137
      if any False value (such as None), the empty string '' is used
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   138
    new_suffix: if non-False, appended to request.path along with a
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   139
      '/' separator (after removing old_suffix if necessary)
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   140
    params: an optional dictionary of query parameters to append to
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   141
      the redirect target; appended as ?<key1>=<value1>&<key2>=...
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   142
      
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   143
  Returns:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   144
    /path/with/new_suffix?a=1&b=2
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   145
  """    
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   146
  if not old_suffix:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   147
    old_suffix = ''
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   148
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   149
  old_suffix = '/' + old_suffix
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   150
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   151
  if path.endswith(old_suffix):
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   152
    # also removes any trailing '/' if old_suffix was empty
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   153
    path = path[:-len(old_suffix)]
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   154
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   155
  if new_suffix:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   156
    # if present, appends new_suffix, after '/' separator
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   157
    path = '%s/%s' % (path, new_suffix)
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   158
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   159
  if params:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   160
    # appends any query parameters, after a '?' and separated by '&'
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   161
    path = '%s?%s' % (path, '&'.join(
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   162
        ['%s=%s' % (p,v) for p,v in params.iteritems()]))
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   163
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   164
  return path
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   165
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   166
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   167
def redirectToChangedSuffix(
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   168
    request, old_suffix, new_suffix, params=None):
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   169
  """Changes suffix of URL path and returns an HTTP redirect response.
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   170
  
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   171
  Args:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   172
    request: the Django HTTP request object; redirect path is derived from
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   173
      request.path
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   174
    old_suffix, new_suffix, params:  see replaceSuffix()
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   175
      
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   176
  Returns:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   177
    a Django HTTP redirect response pointing to the altered path
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   178
  """
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   179
  path = replaceSuffix(request.path, old_suffix, new_suffix, params=params)
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   180
  return http.HttpResponseRedirect(path)
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   181
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   182
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   183
def isReferrerSelf(request,
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   184
                   expected_prefix=None, suffix=None):
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   185
  """Returns True if HTTP referrer path starts with the HTTP request path.
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   186
    
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   187
  Args:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   188
    request: the Django HTTP request object; request.path is used if
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   189
      expected_path is not supplied (the most common usage)
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   190
    expected_prefix: optional HTTP path to use instead of the one in
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   191
      request.path; default is None (use request.path)
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   192
    suffix: suffix to remove from the HTTP request path before comparing
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   193
      it to the HTTP referrer path in the HTTP request object headers
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   194
      (this is often an link name, for example, that may be changing from
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   195
      a POST referrer to a GET redirect target) 
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   196
  
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   197
  Returns:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   198
    True if HTTP referrer path begins with the HTTP request path (either
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   199
      request.path or expected_prefix instead if it was supplied), after
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   200
      any suffix was removed from that request path
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   201
    False otherwise
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   202
       
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   203
  """
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   204
  http_from = request.META.get('HTTP_REFERER')
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   205
        
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   206
  if not http_from:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   207
    # no HTTP referrer, so cannot possibly start with expected prefix
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   208
    return False
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   209
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   210
  from_path = urlparse.urlparse(http_from).path
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   211
  
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   212
  if not expected_prefix:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   213
    # use HTTP request path, since expected_prefix was not supplied
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   214
    expected_prefix = request.path
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   215
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   216
  if suffix:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   217
    # remove suffix (such as a link name) before comparison
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   218
    expected_prefix = expected_prefix[:-len(suffix)+1]
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   219
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   220
  if not from_path.startswith(expected_prefix):
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   221
    # expected prefix did not match first part of HTTP referrer path
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   222
    return False
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   223
 
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   224
  # HTTP referrer started with (possibly truncated) expected prefix
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   225
  return True