app/soc/views/helper/requests.py
author Lennard de Rijk <ljvderijk@gmail.com>
Sat, 31 Jan 2009 17:01:56 +0000
changeset 1122 659984867a9a
parent 512 aae25d2b4464
child 1307 091a21cf3627
permissions -rw-r--r--
Removed workflow type as keyfield from program. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
189
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Helpers for manipulating HTTP requests.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
"""
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
__authors__ = [
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
  '"Todd Larsen" <tlarsen@google.com>',
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
  ]
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 269
diff changeset
    24
201
bc7f0ac07fcb Fixed missing urlparse import in request_helpers which cause /user/profile and /site/user/profile not to work at all. Removed not used module imports in response_helpers.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
    25
import urlparse
bc7f0ac07fcb Fixed missing urlparse import in request_helpers which cause /user/profile and /site/user/profile not to work at all. Removed not used module imports in response_helpers.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 190
diff changeset
    26
189
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
def getSingleIndexedParamValue(request, param_name, values=()):
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
  """Returns a value indexed by a query parameter in the HTTP request.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
  
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
  Args:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
    request: the Django HTTP request object
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
    param_name: name of the query parameter in the HTTP request
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
    values: list (or tuple) of ordered values; one of which is
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
      retrieved by the index value of the param_name argument in
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
      the HTTP request
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
      
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
  Returns:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
    None if the query parameter was not present, was not an integer, or
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
      was an integer that is not a valid [0..len(values)-1] index into
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
      the values list.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
    Otherwise, returns values[int(param_name value)]
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
  """
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
  value_idx = request.GET.get(param_name)
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
  
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
  if isinstance(value_idx, (tuple, list)):
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
    # keep only the first argument if multiple are present
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
    value_idx = value_idx[0]
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
  try:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
    # GET parameter 'param_name' should be an integer value index
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
    value_idx = int(value_idx)
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
  except:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
    # ignore bogus or missing parameter values, so return None (no message)
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
    return None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
    
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
  if value_idx < 0:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
    # value index out of range, so return None (no value)
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
    return None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
  if value_idx >= len(values):
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
    # value index out of range, so return None (no value)
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
    return None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
  # return value associated with valid value index
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
  return values[value_idx]
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
def getSingleIndexedParamValueIfMissing(value, request, param_name,
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
                                        values=()):
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
  """Returns missing value indexed by a query parameter in the HTTP request.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
  
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
  Args:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    74
    value: an existing value, or a "False" value such as None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
    request, param_name, values: see getSingleIndexParamValue()
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    76
    
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
  Returns:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
    value, if value is "non-False"
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
    Otherwise, returns getSingleIndexedParamValue() result.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
  """
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
  if value:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
    # value already present, so return it
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
    return value
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
  return getSingleIndexedParamValue(request, param_name, values=values)
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    86
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    87
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    88
# TODO(tlarsen):  write getMultipleIndexParamValues() that returns a
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    89
#   list of values if present, omitting those values that are
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    90
#   out of range
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    91
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    92
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    93
def isReferrerSelf(request,
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    94
                   expected_prefix=None, suffix=None):
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    95
  """Returns True if HTTP referrer path starts with the HTTP request path.
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    96
    
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    97
  Args:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    98
    request: the Django HTTP request object; request.path is used if
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    99
      expected_path is not supplied (the most common usage)
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   100
    expected_prefix: optional HTTP path to use instead of the one in
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   101
      request.path; default is None (use request.path)
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   102
    suffix: suffix to remove from the HTTP request path before comparing
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   103
      it to the HTTP referrer path in the HTTP request object headers
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 365
diff changeset
   104
      (this is often an link ID, for example, that may be changing from
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   105
      a POST referrer to a GET redirect target) 
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   106
  
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   107
  Returns:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   108
    True if HTTP referrer path begins with the HTTP request path (either
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   109
      request.path or expected_prefix instead if it was supplied), after
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   110
      any suffix was removed from that request path
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   111
    False otherwise
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   112
       
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   113
  """
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   114
  http_from = request.META.get('HTTP_REFERER')
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   115
      
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   116
  if not http_from:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   117
    # no HTTP referrer, so cannot possibly start with expected prefix
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   118
    return False
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   119
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   120
  from_path = urlparse.urlparse(http_from).path
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   121
  
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   122
  if not expected_prefix:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   123
    # use HTTP request path, since expected_prefix was not supplied
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   124
    expected_prefix = request.path
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   125
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   126
  if suffix:
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 365
diff changeset
   127
    # remove suffix (such as a link ID) before comparison
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   128
    chars_to_remove = len(suffix)
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   129
    
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   130
    if not suffix.startswith('/'):
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   131
      chars_to_remove = chars_to_remove + 1
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   132
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   133
    expected_prefix = expected_prefix[:-chars_to_remove]
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   134
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   135
  if not from_path.startswith(expected_prefix):
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   136
    # expected prefix did not match first part of HTTP referrer path
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   137
    return False
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   138
 
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   139
  # HTTP referrer started with (possibly truncated) expected prefix
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   140
  return True
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   141
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   142
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   143
def replaceSuffix(path, old_suffix, new_suffix=None, params=None):
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   144
  """Replace the last part of a URL path with something else.
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   145
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   146
  Also appends an optional list of query parameters.  Used for
512
aae25d2b4464 Rename link_name to link_id everywhere, regardless of case (so LINK_NAME
Todd Larsen <tlarsen@google.com>
parents: 365
diff changeset
   147
  replacing, for example, one link ID at the end of a relative
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   148
  URL path with another.
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   149
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   150
  Args:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   151
    path: HTTP request relative URL path (with no query arguments)
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   152
    old_suffix: expected suffix at the end of request.path component;
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   153
      if any False value (such as None), the empty string '' is used
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   154
    new_suffix: if non-False, appended to request.path along with a
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   155
      '/' separator (after removing old_suffix if necessary)
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   156
    params: an optional dictionary of query parameters to append to
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   157
      the redirect target; appended as ?<key1>=<value1>&<key2>=...
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   158
      
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   159
  Returns:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   160
    /path/with/new_suffix?a=1&b=2
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   161
  """    
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   162
  if not old_suffix:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   163
    old_suffix = ''
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   164
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   165
  old_suffix = '/' + old_suffix
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   166
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   167
  if path.endswith(old_suffix):
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   168
    # also removes any trailing '/' if old_suffix was empty
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   169
    path = path[:-len(old_suffix)]
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   170
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   171
  if new_suffix:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   172
    # if present, appends new_suffix, after '/' separator
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   173
    path = '%s/%s' % (path, new_suffix)
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   174
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   175
  if params:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   176
    # appends any query parameters, after a '?' and separated by '&'
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   177
    path = '%s?%s' % (path, '&'.join(
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   178
        ['%s=%s' % (p,v) for p,v in params.iteritems()]))
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   179
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   180
  return path