app/soc/views/helper/requests.py
author Lennard de Rijk <ljvderijk@gmail.com>
Tue, 29 Sep 2009 19:56:58 +0200
changeset 2990 0b6a093c5c81
parent 2879 cb0f9b4646aa
permissions -rw-r--r--
Moved the Tasks decorators to their own module.
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
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
189
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
2879
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
    27
from soc.logic import system
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
    28
189
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
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
    31
  """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
    32
  
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
  Args:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
    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
    35
    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
    36
    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
    37
      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
    38
      the HTTP request
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
      
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
  Returns:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
    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
    42
      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
    43
      the values list.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
    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
    45
  """
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
  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
    47
  
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
  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
    49
    # 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
    50
    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
    51
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
  try:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
    # GET parameter 'param_name' should be an integer value index
1670
4d9bd851a5f5 Fixed a bug discovered by excepting the proper exception
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1631
diff changeset
    54
    value_idx = int(value_idx) if value_idx is not None else -1
1631
25ec972b5aa2 Style fixes and add ValueError exception catching to soc.views.helper.request module
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1381
diff changeset
    55
  except ValueError:
189
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
    # 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
    57
    return None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
    
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
  if value_idx < 0:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
    # 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
    61
    return None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
  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
    64
    # 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
    65
    return None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
  # 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
    68
  return values[value_idx]
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
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
    72
                                        values=()):
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
  """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
    74
  
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
  Args:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    76
    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
    77
    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
    78
    
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
  Returns:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
    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
    81
    Otherwise, returns getSingleIndexedParamValue() result.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
  """
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
  if value:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
    # 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
    85
    return value
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
  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
    88
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    89
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    90
# 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
    91
#   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
    92
#   out of range
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    93
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    94
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    95
def isReferrerSelf(request,
1373
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
    96
                   expected_prefix=None, suffix=None, url_name=None):
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    97
  """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
    98
    
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    99
  Args:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   100
    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
   101
      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
   102
    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
   103
      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
   104
    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
   105
      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
   106
      (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
   107
      a POST referrer to a GET redirect target) 
1373
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
   108
    url_name: url name of the entity that is being created
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   109
  
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   110
  Returns:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   111
    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
   112
      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
   113
      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
   114
    False otherwise
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
  """
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   117
  http_from = request.META.get('HTTP_REFERER')
1373
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
   118
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   119
  if not http_from:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   120
    # 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
   121
    return False
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   122
2879
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
   123
  http_host = 'http://%s/%s' % (system.getHostname(), url_name)
1373
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
   124
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
   125
  if http_from.startswith(http_host):
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
   126
    return True
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
   127
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   128
  from_path = urlparse.urlparse(http_from).path
1373
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
   129
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   130
  if not expected_prefix:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   131
    # 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
   132
    expected_prefix = request.path
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   133
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   134
  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
   135
    # 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
   136
    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
   137
    
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   138
    if not suffix.startswith('/'):
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   139
      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
   140
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   141
    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
   142
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   143
  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
   144
    # 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
   145
    return False
1373
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
   146
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   147
  # 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
   148
  return True
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
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   151
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
   152
  """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
   153
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   154
  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
   155
  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
   156
  URL path with another.
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   157
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   158
  Args:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   159
    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
   160
    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
   161
      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
   162
    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
   163
      '/' 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
   164
    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
   165
      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
   166
      
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   167
  Returns:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   168
    /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
   169
  """    
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   170
  if not old_suffix:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   171
    old_suffix = ''
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   172
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   173
  old_suffix = '/' + old_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 path.endswith(old_suffix):
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   176
    # 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
   177
    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
   178
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   179
  if new_suffix:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   180
    # 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
   181
    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
   182
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   183
  if params:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   184
    # 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
   185
    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
   186
        ['%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
   187
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   188
  return path