author | Todd Larsen <tlarsen@google.com> |
Wed, 01 Oct 2008 02:11:56 +0000 | |
changeset 229 | a46c238be8db |
parent 201 | bc7f0ac07fcb |
permissions | -rw-r--r-- |
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 |
|
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
|
24 |
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
|
25 |
|
189
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
26 |
|
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
27 |
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
|
28 |
"""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
|
29 |
|
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
30 |
Args: |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
31 |
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
|
32 |
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
|
33 |
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
|
34 |
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
|
35 |
the HTTP request |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
36 |
|
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
37 |
Returns: |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
38 |
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
|
39 |
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
|
40 |
the values list. |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
41 |
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
|
42 |
""" |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
43 |
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
|
44 |
|
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
45 |
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
|
46 |
# 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
|
47 |
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
|
48 |
|
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
49 |
try: |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
50 |
# 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
|
51 |
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
|
52 |
except: |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
53 |
# 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
|
54 |
return None |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
55 |
|
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
56 |
if value_idx < 0: |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
57 |
# 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
|
58 |
return None |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
59 |
|
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
60 |
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
|
61 |
# 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
|
62 |
return None |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
63 |
|
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
64 |
# 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
|
65 |
return values[value_idx] |
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 |
|
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
68 |
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
|
69 |
values=()): |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
70 |
"""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
|
71 |
|
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
72 |
Args: |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
73 |
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
|
74 |
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
|
75 |
|
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
76 |
Returns: |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
77 |
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
|
78 |
Otherwise, returns getSingleIndexedParamValue() result. |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
79 |
""" |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
80 |
if value: |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
81 |
# 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
|
82 |
return value |
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
83 |
|
1cf3e7531382
Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
84 |
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
|
85 |
|
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 |
# 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
|
88 |
# 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
|
89 |
# out of range |
190
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
90 |
|
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 |
def isReferrerSelf(request, |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
93 |
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
|
94 |
"""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
|
95 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
96 |
Args: |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
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
|
101 |
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
|
102 |
it to the HTTP referrer path in the HTTP request object headers |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
103 |
(this is often an link name, for example, that may be changing from |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
104 |
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
|
105 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
106 |
Returns: |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
False otherwise |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
111 |
|
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 |
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
|
114 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
115 |
if not http_from: |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
116 |
# 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
|
117 |
return False |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
118 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
119 |
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
|
120 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
121 |
if not expected_prefix: |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
122 |
# 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
|
123 |
expected_prefix = request.path |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
124 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
125 |
if suffix: |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
126 |
# remove suffix (such as a link name) before comparison |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
127 |
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
|
128 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
129 |
if not suffix.startswith('/'): |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
130 |
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
|
131 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
132 |
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
|
133 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
134 |
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
|
135 |
# 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
|
136 |
return False |
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 |
# 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
|
139 |
return True |
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 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
142 |
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
|
143 |
"""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
|
144 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
145 |
Also appends an optional list of query parameters. Used for |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
146 |
replacing, for example, one link name at the end of a relative |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
147 |
URL path with another. |
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 |
Args: |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
150 |
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
|
151 |
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
|
152 |
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
|
153 |
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
|
154 |
'/' 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
|
155 |
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
|
156 |
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
|
157 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
158 |
Returns: |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
159 |
/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
|
160 |
""" |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
161 |
if not old_suffix: |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
162 |
old_suffix = '' |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
163 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
164 |
old_suffix = '/' + old_suffix |
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 |
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
|
167 |
# 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
|
168 |
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
|
169 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
170 |
if new_suffix: |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
171 |
# 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
|
172 |
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
|
173 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
174 |
if params: |
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
175 |
# 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
|
176 |
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
|
177 |
['%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
|
178 |
|
b1351bf81064
Move isReferrerSelf() and replaceSuffix() into new request_helpers.py module,
Todd Larsen <tlarsen@google.com>
parents:
189
diff
changeset
|
179 |
return path |