author | Pawel Solyga <Pawel.Solyga@gmail.com> |
Tue, 23 Sep 2008 19:20:57 +0000 | |
changeset 188 | 27ed1a09c98d |
parent 179 | 4882d6c5630d |
child 190 | b1351bf81064 |
permissions | -rw-r--r-- |
83
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
2 |
# |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2008 the Melange authors. |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
4 |
# |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
8 |
# |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
10 |
# |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
16 |
|
128
f6abf00efc62
Updated docs in response_helpers.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
119
diff
changeset
|
17 |
"""Helpers used to render response. |
83
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
18 |
""" |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
19 |
|
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
116
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
21 |
'"Todd Larsen" <tlarsen@google.com>', |
83
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
22 |
'"Pawel Solyga" <pawel.solyga@gmail.com>', |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
23 |
] |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
24 |
|
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
25 |
|
169
a9b3d6c9d4f9
Fix off-by-one corner case discovered during testing of /user/profile.
Todd Larsen <tlarsen@google.com>
parents:
167
diff
changeset
|
26 |
import logging |
167
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
27 |
import urlparse |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
28 |
|
83
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
29 |
from google.appengine.api import users |
116
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
30 |
|
83
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
31 |
from django import http |
119
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
32 |
from django.template import loader |
83
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
33 |
|
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
34 |
# DeadlineExceededError can live in two different places |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
35 |
try: |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
36 |
# When deployed |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
37 |
from google.appengine.runtime import DeadlineExceededError |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
38 |
except ImportError: |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
39 |
# In the development server |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
40 |
from google.appengine.runtime.apiproxy_errors import DeadlineExceededError |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
41 |
|
116
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
42 |
from soc.logic import system |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
43 |
from soc.logic.site import id_user |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
44 |
|
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
45 |
|
119
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
46 |
def respond(request, template, context=None, response_args=None): |
83
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
47 |
"""Helper to render a response, passing standard stuff to the response. |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
48 |
|
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
49 |
Args: |
116
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
50 |
request: the Django HTTP request object |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
51 |
template: the template (or search list of templates) to render |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
52 |
context: the context supplied to the template (implements dict) |
119
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
53 |
response_args: keyword arguments passed to http.HttpResponse() |
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
54 |
(response_args['content'] is created with |
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
55 |
render_to_string(template, dictionary=context) if it is not present) |
83
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
56 |
|
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
57 |
Returns: |
116
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
58 |
django.shortcuts.render_to_response(template, context) results |
83
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
59 |
|
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
60 |
Raises: |
119
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
61 |
Any exceptions that django.template.loader.render_to_string() or |
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
62 |
django.http.HttpResponse() might raise. |
83
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
63 |
""" |
116
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
64 |
context = getUniversalContext(request, context=context) |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
65 |
|
119
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
66 |
if response_args is None: |
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
67 |
response_args = {} |
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
68 |
|
83
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
69 |
try: |
119
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
70 |
response_args['content'] = response_args.get( |
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
71 |
'content', loader.render_to_string(template, dictionary=context)) |
50d8d58dcd7c
Add soc.views.simple containing (initially) two simple template views.
Todd Larsen <tlarsen@google.com>
parents:
116
diff
changeset
|
72 |
return http.HttpResponse(**response_args) |
83
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
73 |
except DeadlineExceededError: |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
74 |
logging.exception('DeadlineExceededError') |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
75 |
return http.HttpResponse('DeadlineExceededError') |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
76 |
except MemoryError: |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
77 |
logging.exception('MemoryError') |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
78 |
return http.HttpResponse('MemoryError') |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
79 |
except AssertionError: |
3f4f7c540b75
Created response helper respond() function that is used to generate base templates and it's child templates (handles sign in/out links, user name etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff
changeset
|
80 |
logging.exception('AssertionError') |
116
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
81 |
return http.HttpResponse('AssertionError') |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
82 |
|
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
83 |
|
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
84 |
def getUniversalContext(request, context=None): |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
85 |
"""Constructs a template context dict will many common variables defined. |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
86 |
|
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
87 |
Args: |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
88 |
request: the Django HTTP request object |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
89 |
context: the template context dict to be updated in-place (pass in a copy |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
90 |
if the original must not be modified), or None if a new one is to be |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
91 |
created; any existing fields already present in the context dict passed |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
92 |
in by the caller are left unaltered |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
93 |
|
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
94 |
Returns: |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
95 |
updated template context dict supplied by the caller, or a new context |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
96 |
dict if the caller supplied None |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
97 |
|
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
98 |
{ |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
99 |
'request': the Django HTTP request object passed in by the caller |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
100 |
'id': the logged-in Google Account if there is one |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
101 |
'user': the User entity corresponding to the Google Account in |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
102 |
context['id'] |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
103 |
'is_admin': True if users.is_current_user_admin() is True |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
104 |
'is_debug': True if system.isDebug() is True |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
105 |
'sign_in': a Google Account login URL |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
106 |
'sign_out': a Google Account logout URL |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
107 |
} |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
108 |
""" |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
109 |
if context is None: |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
110 |
context = {} |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
111 |
|
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
112 |
# set some universal values if caller did not already set them |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
113 |
context['request'] = context.get('request', request) |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
114 |
context['id'] = id_user.getIdIfMissing(context.get('id', None)) |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
115 |
context['user'] = id_user.getUserIfMissing(context.get('user', None), |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
116 |
context['id']) |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
117 |
context['is_admin'] = context.get( |
135
a7ccde9d9eed
Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
131
diff
changeset
|
118 |
'is_admin', id_user.isIdDeveloper(id=context['id'])) |
116
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
119 |
context['is_debug'] = context.get('is_debug', system.isDebug()) |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
120 |
context['sign_in'] = context.get( |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
121 |
'sign_in', users.create_login_url(request.path)) |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
122 |
context['sign_out'] = context.get( |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
123 |
'sign_out', users.create_logout_url(request.path)) |
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
124 |
|
68b5ce15fff9
Factor out a getUniversalContext() function so that views that do not call
Todd Larsen <tlarsen@google.com>
parents:
99
diff
changeset
|
125 |
return context |
167
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
126 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
127 |
|
179
4882d6c5630d
Add custom 404 error message with link to "Create a New User" which is displayed when linkname in User Profile Developer view URL (/site/user/profile/<linkname>) doesn't exist. Add default value (None) to new_suffix parameter of template_helpers replaceSuffix and redirectToChangedSuffix functions.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
169
diff
changeset
|
128 |
def replaceSuffix(path, old_suffix, new_suffix=None, params=None): |
167
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
129 |
"""Replace the last part of a URL path with something else. |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
130 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
131 |
Also appends an optional list of query parameters. Used for |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
132 |
replacing, for example, one link name at the end of a relative |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
133 |
URL path with another. |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
134 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
135 |
Args: |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
136 |
path: HTTP request relative URL path (with no query arguments) |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
137 |
old_suffix: expected suffix at the end of request.path component; |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
138 |
if any False value (such as None), the empty string '' is used |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
139 |
new_suffix: if non-False, appended to request.path along with a |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
140 |
'/' separator (after removing old_suffix if necessary) |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
141 |
params: an optional dictionary of query parameters to append to |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
142 |
the redirect target; appended as ?<key1>=<value1>&<key2>=... |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
143 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
144 |
Returns: |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
145 |
/path/with/new_suffix?a=1&b=2 |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
146 |
""" |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
147 |
if not old_suffix: |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
148 |
old_suffix = '' |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
149 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
150 |
old_suffix = '/' + old_suffix |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
151 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
152 |
if path.endswith(old_suffix): |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
153 |
# also removes any trailing '/' if old_suffix was empty |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
154 |
path = path[:-len(old_suffix)] |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
155 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
156 |
if new_suffix: |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
157 |
# if present, appends new_suffix, after '/' separator |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
158 |
path = '%s/%s' % (path, new_suffix) |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
159 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
160 |
if params: |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
161 |
# appends any query parameters, after a '?' and separated by '&' |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
162 |
path = '%s?%s' % (path, '&'.join( |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
163 |
['%s=%s' % (p,v) for p,v in params.iteritems()])) |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
164 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
165 |
return path |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
166 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
167 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
168 |
def redirectToChangedSuffix( |
179
4882d6c5630d
Add custom 404 error message with link to "Create a New User" which is displayed when linkname in User Profile Developer view URL (/site/user/profile/<linkname>) doesn't exist. Add default value (None) to new_suffix parameter of template_helpers replaceSuffix and redirectToChangedSuffix functions.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
169
diff
changeset
|
169 |
request, old_suffix, new_suffix=None, params=None): |
167
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
170 |
"""Changes suffix of URL path and returns an HTTP redirect response. |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
171 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
172 |
Args: |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
173 |
request: the Django HTTP request object; redirect path is derived from |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
174 |
request.path |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
175 |
old_suffix, new_suffix, params: see replaceSuffix() |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
176 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
177 |
Returns: |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
178 |
a Django HTTP redirect response pointing to the altered path |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
179 |
""" |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
180 |
path = replaceSuffix(request.path, old_suffix, new_suffix, params=params) |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
181 |
return http.HttpResponseRedirect(path) |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
182 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
183 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
184 |
def isReferrerSelf(request, |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
185 |
expected_prefix=None, suffix=None): |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
186 |
"""Returns True if HTTP referrer path starts with the HTTP request path. |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
187 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
188 |
Args: |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
189 |
request: the Django HTTP request object; request.path is used if |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
190 |
expected_path is not supplied (the most common usage) |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
191 |
expected_prefix: optional HTTP path to use instead of the one in |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
192 |
request.path; default is None (use request.path) |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
193 |
suffix: suffix to remove from the HTTP request path before comparing |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
194 |
it to the HTTP referrer path in the HTTP request object headers |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
195 |
(this is often an link name, for example, that may be changing from |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
196 |
a POST referrer to a GET redirect target) |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
197 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
198 |
Returns: |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
199 |
True if HTTP referrer path begins with the HTTP request path (either |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
200 |
request.path or expected_prefix instead if it was supplied), after |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
201 |
any suffix was removed from that request path |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
202 |
False otherwise |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
203 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
204 |
""" |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
205 |
http_from = request.META.get('HTTP_REFERER') |
169
a9b3d6c9d4f9
Fix off-by-one corner case discovered during testing of /user/profile.
Todd Larsen <tlarsen@google.com>
parents:
167
diff
changeset
|
206 |
|
167
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
207 |
if not http_from: |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
208 |
# no HTTP referrer, so cannot possibly start with expected prefix |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
209 |
return False |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
210 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
211 |
from_path = urlparse.urlparse(http_from).path |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
212 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
213 |
if not expected_prefix: |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
214 |
# use HTTP request path, since expected_prefix was not supplied |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
215 |
expected_prefix = request.path |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
216 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
217 |
if suffix: |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
218 |
# remove suffix (such as a link name) before comparison |
169
a9b3d6c9d4f9
Fix off-by-one corner case discovered during testing of /user/profile.
Todd Larsen <tlarsen@google.com>
parents:
167
diff
changeset
|
219 |
chars_to_remove = len(suffix) |
a9b3d6c9d4f9
Fix off-by-one corner case discovered during testing of /user/profile.
Todd Larsen <tlarsen@google.com>
parents:
167
diff
changeset
|
220 |
|
a9b3d6c9d4f9
Fix off-by-one corner case discovered during testing of /user/profile.
Todd Larsen <tlarsen@google.com>
parents:
167
diff
changeset
|
221 |
if not suffix.startswith('/'): |
a9b3d6c9d4f9
Fix off-by-one corner case discovered during testing of /user/profile.
Todd Larsen <tlarsen@google.com>
parents:
167
diff
changeset
|
222 |
chars_to_remove = chars_to_remove + 1 |
a9b3d6c9d4f9
Fix off-by-one corner case discovered during testing of /user/profile.
Todd Larsen <tlarsen@google.com>
parents:
167
diff
changeset
|
223 |
|
a9b3d6c9d4f9
Fix off-by-one corner case discovered during testing of /user/profile.
Todd Larsen <tlarsen@google.com>
parents:
167
diff
changeset
|
224 |
expected_prefix = expected_prefix[:-chars_to_remove] |
167
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
225 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
226 |
if not from_path.startswith(expected_prefix): |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
227 |
# expected prefix did not match first part of HTTP referrer path |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
228 |
return False |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
229 |
|
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
230 |
# HTTP referrer started with (possibly truncated) expected prefix |
13e438623ded
Added replaceSuffix(), isReferrerSelf(), and redirectToChangedSuffix() to
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
231 |
return True |