app/soc/views/helper/requests.py
author Sverre Rabbelier <srabbelier@gmail.com>
Wed, 04 Mar 2009 23:10:38 +0000
changeset 1670 4d9bd851a5f5
parent 1631 25ec972b5aa2
child 2867 e8d86272e6ea
permissions -rw-r--r--
Fixed a bug discovered by excepting the proper exception Patch by: Sverre Rabbelier
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
1373
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
    25
import os
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
    26
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
    27
189
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
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
    30
  """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
    31
  
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
  Args:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
    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
    34
    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
    35
    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
    36
      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
    37
      the HTTP request
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
      
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
  Returns:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
    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
    41
      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
    42
      the values list.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
    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
    44
  """
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
  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
    46
  
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
  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
    48
    # 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
    49
    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
    50
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
  try:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
    # 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
    53
    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
    54
  except ValueError:
189
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
    # 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
    56
    return None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
    
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
  if value_idx < 0:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
    # 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
    60
    return None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
  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
    63
    # 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
    64
    return None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
  # 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
    67
  return values[value_idx]
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
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
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
    71
                                        values=()):
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
  """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
    73
  
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    74
  Args:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
    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
    76
    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
    77
    
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
  Returns:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
    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
    80
    Otherwise, returns getSingleIndexedParamValue() result.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
  """
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
  if value:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
    # 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
    84
    return value
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    86
  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
    87
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
# 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
    90
#   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
    91
#   out of range
190
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
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    94
def isReferrerSelf(request,
1373
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
    95
                   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
    96
  """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
    97
    
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    98
  Args:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
    99
    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
   100
      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
   101
    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
   102
      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
   103
    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
   104
      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
   105
      (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
   106
      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
   107
    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
   108
  
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   109
  Returns:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   110
    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
   111
      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
   112
      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
   113
    False otherwise
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   114
       
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
  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
   117
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   118
  if not http_from:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   119
    # 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
   120
    return False
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   121
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
   122
  http_host = 'http://%s/%s' % (os.environ['HTTP_HOST'], url_name)
1373
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
   123
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
   124
  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
   125
    return True
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
   126
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   127
  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
   128
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   129
  if not expected_prefix:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   130
    # 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
   131
    expected_prefix = request.path
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
  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
   134
    # 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
   135
    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
   136
    
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   137
    if not suffix.startswith('/'):
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   138
      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
   139
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   140
    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
   141
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   142
  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
   143
    # 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
   144
    return False
1373
178bd19966fe Fixes the problem of <Entity> Saved not being
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1308
diff changeset
   145
190
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   146
  # 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
   147
  return True
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   148
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
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
   151
  """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
   152
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   153
  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
   154
  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
   155
  URL path with another.
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   156
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   157
  Args:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   158
    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
   159
    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
   160
      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
   161
    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
   162
      '/' 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
   163
    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
   164
      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
   165
      
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   166
  Returns:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   167
    /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
   168
  """    
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   169
  if not old_suffix:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   170
    old_suffix = ''
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   171
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   172
  old_suffix = '/' + old_suffix
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   173
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   174
  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
   175
    # 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
   176
    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
   177
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   178
  if new_suffix:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   179
    # 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
   180
    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
   181
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   182
  if params:
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   183
    # 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
   184
    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
   185
        ['%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
   186
b1351bf81064 Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents: 189
diff changeset
   187
  return path