app/soc/views/helpers/response_helpers.py
author Todd Larsen <tlarsen@google.com>
Wed, 24 Sep 2008 06:00:28 +0000
changeset 200 8e3a18cead22
parent 190 b1351bf81064
child 201 bc7f0ac07fcb
permissions -rw-r--r--
Replace mock-up of sidebar with first draft of dynamic sidebar functionality.
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
169
a9b3d6c9d4f9 Fix off-by-one corner case discovered during testing of /user/profile.
Todd Larsen <tlarsen@google.com>
parents: 167
diff changeset
    26
import logging
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
    27
import urlparse
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
    28
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
    29
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
    30
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
    31
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
    32
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
    33
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
# 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
    35
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
    36
  # 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
    37
  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
    38
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
    39
  # 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
    40
  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
    41
116
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 import system
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    43
from soc.logic.site import id_user
200
8e3a18cead22 Replace mock-up of sidebar with first draft of dynamic sidebar functionality.
Todd Larsen <tlarsen@google.com>
parents: 190
diff changeset
    44
from soc.logic.site import sidebar
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    45
200
8e3a18cead22 Replace mock-up of sidebar with first draft of dynamic sidebar functionality.
Todd Larsen <tlarsen@google.com>
parents: 190
diff changeset
    46
from soc.views.helpers import html_menu
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 179
diff changeset
    47
from soc.views.helpers import request_helpers
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 179
diff changeset
    48
from soc.views.helpers import template_helpers
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 179
diff changeset
    49
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    50
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    51
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
    52
  """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
    53
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
    54
  Args:
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    55
    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
    56
    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
    57
    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
    58
    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
    59
      (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
    60
      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
    61
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
  Returns:
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    63
    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
    64
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
    65
  Raises:
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    66
    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
    67
    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
    68
  """
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    69
  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
    70
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    71
  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
    72
    response_args = {}
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    73
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
    74
  try:
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents: 116
diff changeset
    75
    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
    76
        '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
    77
    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
    78
  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
    79
    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
    80
    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
    81
  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
    82
    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
    83
    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
    84
  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
    85
    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
    86
    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
    87
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    88
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    89
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
    90
  """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
    91
  
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    92
  Args:
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    93
    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
    94
    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
    95
      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
    96
      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
    97
      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
    98
      
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
    99
  Returns:
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   100
    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
   101
    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
   102
    
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   103
    {
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   104
      '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
   105
      '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
   106
      '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
   107
        context['id']
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   108
      '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
   109
      '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
   110
      '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
   111
      'sign_out': a Google Account logout URL
200
8e3a18cead22 Replace mock-up of sidebar with first draft of dynamic sidebar functionality.
Todd Larsen <tlarsen@google.com>
parents: 190
diff changeset
   112
      'sidebar_menu_html': an HTML string that renders the sidebar menu
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   113
    }
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   114
  """
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   115
  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
   116
    context = {}
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   117
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   118
  # 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
   119
  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
   120
  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
   121
  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
   122
                                             context['id'])
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   123
  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
   124
      '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
   125
  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
   126
  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
   127
      '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
   128
  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
   129
      '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
   130
200
8e3a18cead22 Replace mock-up of sidebar with first draft of dynamic sidebar functionality.
Todd Larsen <tlarsen@google.com>
parents: 190
diff changeset
   131
  if not context.get('sidebar_menu_html'):
8e3a18cead22 Replace mock-up of sidebar with first draft of dynamic sidebar functionality.
Todd Larsen <tlarsen@google.com>
parents: 190
diff changeset
   132
    # pass the currently constructed context as keyword arguments to
8e3a18cead22 Replace mock-up of sidebar with first draft of dynamic sidebar functionality.
Todd Larsen <tlarsen@google.com>
parents: 190
diff changeset
   133
    # all of the sidebar builder functions
8e3a18cead22 Replace mock-up of sidebar with first draft of dynamic sidebar functionality.
Todd Larsen <tlarsen@google.com>
parents: 190
diff changeset
   134
    context['sidebar_menu_html'] = str(html_menu.UlMenu(
8e3a18cead22 Replace mock-up of sidebar with first draft of dynamic sidebar functionality.
Todd Larsen <tlarsen@google.com>
parents: 190
diff changeset
   135
      sidebar.buildSidebar(**context)))
8e3a18cead22 Replace mock-up of sidebar with first draft of dynamic sidebar functionality.
Todd Larsen <tlarsen@google.com>
parents: 190
diff changeset
   136
116
68b5ce15fff9 Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents: 99
diff changeset
   137
  return context
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   138
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   139
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   140
def redirectToChangedSuffix(
179
4882d6c5630d Add custom 404 error message with link to "Create a New User" which is displayed when linkname in User Profile Developer view URL (/site/user/profile/<linkname>) doesn't exist. Add default value (None) to new_suffix parameter of template_helpers replaceSuffix and redirectToChangedSuffix functions.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 169
diff changeset
   141
    request, old_suffix, new_suffix=None, params=None):
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   142
  """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
   143
  
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   144
  Args:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   145
    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
   146
      request.path
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 179
diff changeset
   147
    old_suffix, new_suffix, params:  see request_helpers.replaceSuffix()
167
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
  Returns:
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   150
    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
   151
  """
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 179
diff changeset
   152
  path = request_helpers.replaceSuffix(request.path, old_suffix, new_suffix,
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 179
diff changeset
   153
                                       params=params)
167
13e438623ded Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   154
  return http.HttpResponseRedirect(path)