app/soc/views/simple.py
author Todd Larsen <tlarsen@google.com>
Fri, 12 Sep 2008 21:54:37 +0000
changeset 139 93a9a22a4c38
parent 125 155f43a0fa68
child 141 e120c24b89e2
permissions -rw-r--r--
Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple view helpers. These cannot go into response_helpers.py because they create a circular reference with simple.py, and Django view import fails. Review URL: http://codereviews.googleopensourceprograms.com/602
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Simple views that depend entirely on the template and context.
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
simpleWithLinkName: a simple template view for URLs with a linkname
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
errorResponse: renders an out_of_band.ErrorResponse page
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
"""
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
__authors__ = [
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
  '"Todd Larsen" <tlarsen@google.com>',
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
  ]
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
139
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
    29
from django.utils.translation import ugettext_lazy
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
from soc.logic import out_of_band
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
from soc.logic.site import id_user
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
from soc.views.helpers import response_helpers
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
from soc.views.helpers import template_helpers
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
def templateWithLinkName(request,
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
                         template='soc/base.html', linkname=None,
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
                         context=None):
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
  """A simple template view that expects a linkname extracted from the URL. 
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
  Args:
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
    request: the standard Django HTTP request object
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
    template: the template to use for rendering the view (or a search list
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
      of templates)
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
    linkname: a site-unique "linkname" (usually extracted from the URL)
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
    context: the context dict supplied to the template, which is modified
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
        (so supply a copy if such modification is not acceptable)
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
      linkname: the linkname parameter is added to the context
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
      linkname_user: if the linkname exists for a User, that User
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
        is added to the context
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
  Returns:
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
    A subclass of django.http.HttpResponse containing the generated page.
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
  """
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
  context['linkname'] = linkname
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
  context = response_helpers.getUniversalContext(request, context=context)
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
  try:
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
    context['linkname_user'] = id_user.getUserIfLinkName(linkname)
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
  except out_of_band.ErrorResponse, error:
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
    return errorResponse(request, error, template, context)
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
  return response_helpers.respond(request, template, context)
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
122
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    67
def public(request, template, linkname, context):
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    68
  """A convenience wrapper around templateWithLinkName() using 'public.html'.
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    69
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    70
  Args:
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    71
    request, linkname, context: see templateWithLinkName()
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    72
    template: the "sibling" template (or a search list of such templates)
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    73
      from which to construct the public.html template name (or names)
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    74
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    75
  Returns:
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    76
    A subclass of django.http.HttpResponse containing the generated page.
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    77
  """
123
45d10b6af158 Fix an overlooked cut-and-paste error.
Todd Larsen <tlarsen@google.com>
parents: 122
diff changeset
    78
  return templateWithLinkName(
122
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    79
      request, linkname=linkname, context=context,
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    80
      template=template_helpers.makeSiblingTemplatesList(
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    81
          template, 'public.html'))
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    82
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
    83
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
DEF_ERROR_TMPL = 'soc/error.html'
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    86
def errorResponse(request, error, template, context):
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    87
  """Displays an error page for an out_of_band.ErrorResponse exception.
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    88
  
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    89
  Args:
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    90
    request: the standard Django HTTP request object
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    91
    error: an out_of_band.ErrorResponse exception
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    92
    template: the "sibling" template (or a search list of such templates)
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    93
      from which to construct the error.html template name (or names)
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    94
    context: the context dict supplied to the template, which is modified
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    95
        (so supply a copy if such modification is not acceptable)
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    96
      error_message: the error message string from error.message
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    97
      error_status: error.response_args['status'], or None if a status code
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    98
        was not supplied to the ErrorResponse
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    99
  """
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   100
  context = response_helpers.getUniversalContext(request, context=context)
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   101
  
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
  # make a list of possible "sibling" templates, then append a default
125
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 123
diff changeset
   103
  error_templates = template_helpers.makeSiblingTemplatesList(
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 123
diff changeset
   104
      template, 'error.html', default_template=DEF_ERROR_TMPL)
119
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   105
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   106
  context['error_status'] = error.response_args.get('status')
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   107
  context['error_message'] = error.message
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   108
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   109
  return response_helpers.respond(request, error_templates, context=context,
50d8d58dcd7c Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   110
                                  response_args=error.response_args)
122
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   111
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   112
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   113
DEF_LOGIN_TMPL = 'soc/login.html'
139
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   114
DEF_LOGIN_MSG_FMT = ugettext_lazy(
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   115
  'Please <a href="%(sign_in)s">sign in</a> to continue.')
122
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   116
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   117
def requestLogin(request, template, context, login_message_fmt=None):
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   118
  """Displays a login request page with custom message and login link.
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   119
  
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   120
  Args:
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   121
    request: the standard Django HTTP request object
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   122
    template: the "sibling" template (or a search list of such templates)
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   123
      from which to construct the login.html template name (or names)
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   124
    login_message_fmt: a custom message format string used to create a
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   125
      message displayed on the login page; the format string can contain
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   126
      named format specifiers for any of the keys in context, but should at
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   127
      least contain %(sign_in)s
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   128
    context: the context dict supplied to the template, which is modified
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   129
        (so supply a copy if such modification is not acceptable); 
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   130
      login_message: the caller can completely construct the message supplied
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   131
        to the login template in lieu of using login_message_fmt
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   132
  """
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   133
  context = response_helpers.getUniversalContext(request, context=context)
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   134
  
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   135
  # make a list of possible "sibling" templates, then append a default
125
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 123
diff changeset
   136
  login_templates = template_helpers.makeSiblingTemplatesList(
155f43a0fa68 An emerging pattern with makeSiblingTemplatesList() is that views calling that
Todd Larsen <tlarsen@google.com>
parents: 123
diff changeset
   137
      template, 'login.html', default_template=DEF_LOGIN_TMPL)
122
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   138
  
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   139
  if not context.get('login_message'):
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   140
    if not login_message_fmt:
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   141
      login_message_fmt = DEF_LOGIN_MSG_FMT
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   142
    context['login_message'] = login_message_fmt % context  
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   143
  
95d0d954fe0b Add a public() convenience wrapper around templateWithLinkName() for the common
Todd Larsen <tlarsen@google.com>
parents: 119
diff changeset
   144
  return response_helpers.respond(request, login_templates, context=context)
139
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   145
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   146
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   147
def getAltResponseIfNotLoggedIn(request, context=None,
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   148
                                template=DEF_LOGIN_TMPL, id=None,
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   149
                                login_message_fmt=DEF_LOGIN_MSG_FMT):
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   150
  """Returns an alternate HTTP response if Google Account is not logged in.
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   151
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   152
  Args:
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   153
    request: the standard django request object
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   154
    context: the context supplied to the template (implements dict)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   155
    template: the "sibling" template (or a search list of such templates)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   156
      from which to construct the public.html template name (or names)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   157
    id: a Google Account (users.User) object, or None, in which case
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   158
      the current logged-in user is used
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   159
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   160
  Returns:
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   161
    None if id is logged in, or a subclass of django.http.HttpResponse
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   162
    which contains the alternate response that should be returned by the
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   163
    calling view.
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   164
  """
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   165
  id = id_user.getIdIfMissing(id)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   166
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   167
  if id:
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   168
    # a Google Account is logged in, so no need to ask user to sign in  
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   169
    return None
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   170
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   171
  # if missing, create default template context for use with any templates
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   172
  context = response_helpers.getUniversalContext(request, context=context)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   173
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   174
  return requestLogin(request, template, context,
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   175
                      login_message_fmt=login_message_fmt)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   176
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   177
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   178
DEF_DEV_LOGIN_MSG_FMT = ugettext_lazy(
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   179
    'Please <a href="%(sign_in)s">sign in</a>'
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   180
    ' as a site developer to view this page.')
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   181
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   182
DEF_DEF_LOGOUT_LOGIN_MSG_FMT = ugettext_lazy(
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   183
    'Please <a href="%(sign_out)s">sign out</a>'
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   184
    ' and <a href="%(sign_in)s">sign in</a>'
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   185
    ' again as a site developer to view this page.')
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   186
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   187
def getAltResponseIfNotDeveloper(request, context=None,
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   188
                                 template=DEF_LOGIN_TMPL, id=None):
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   189
  """Returns an alternate HTTP response if Google Account is not a Developer.
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   190
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   191
  Args:
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   192
    request: the standard django request object
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   193
    context: the context supplied to the template (implements dict)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   194
    template: the "sibling" template (or a search list of such templates)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   195
      from which to construct the public.html template name (or names)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   196
    id: a Google Account (users.User) object, or None, in which case
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   197
      the current logged-in user is used
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   198
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   199
  Returns:
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   200
    None if id is logged in and logged-in user is a Developer, or a
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   201
    subclass of django.http.HttpResponse which contains the alternate
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   202
    response that should be returned by the calling view.
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   203
  """
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   204
  id = id_user.getIdIfMissing(id)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   205
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   206
  # if missing, create default template context for use with any templates
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   207
  context = response_helpers.getUniversalContext(request, context=context)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   208
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   209
  if not id:
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   210
    return requestLogin(request, template, context,
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   211
        login_message_fmt=DEF_DEV_LOGIN_MSG_FMT)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   212
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   213
  if not id_user.isIdDeveloper(id=id):
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   214
    return requestLogin(request, template, context,
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   215
        login_message_fmt=DEF_DEF_LOGOUT_LOGIN_MSG_FMT)
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   216
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   217
  # Google Account is logged in and is a Developer, so no need for sign in
93a9a22a4c38 Add getAltResponseIfNotLoggedIn() and getAltResponseIfNotDeveloper() simple
Todd Larsen <tlarsen@google.com>
parents: 125
diff changeset
   218
  return None