app/soc/views/helpers/response_helpers.py
changeset 190 b1351bf81064
parent 179 4882d6c5630d
child 200 8e3a18cead22
equal deleted inserted replaced
189:1cf3e7531382 190:b1351bf81064
    39   # In the development server
    39   # In the development server
    40   from google.appengine.runtime.apiproxy_errors import DeadlineExceededError
    40   from google.appengine.runtime.apiproxy_errors import DeadlineExceededError
    41 
    41 
    42 from soc.logic import system
    42 from soc.logic import system
    43 from soc.logic.site import id_user
    43 from soc.logic.site import id_user
       
    44 
       
    45 from soc.views.helpers import request_helpers
       
    46 from soc.views.helpers import template_helpers
    44 
    47 
    45 
    48 
    46 def respond(request, template, context=None, response_args=None):
    49 def respond(request, template, context=None, response_args=None):
    47   """Helper to render a response, passing standard stuff to the response.
    50   """Helper to render a response, passing standard stuff to the response.
    48 
    51 
   123       'sign_out', users.create_logout_url(request.path))
   126       'sign_out', users.create_logout_url(request.path))
   124 
   127 
   125   return context
   128   return context
   126 
   129 
   127 
   130 
   128 def replaceSuffix(path, old_suffix, new_suffix=None, params=None):
       
   129   """Replace the last part of a URL path with something else.
       
   130 
       
   131   Also appends an optional list of query parameters.  Used for
       
   132   replacing, for example, one link name at the end of a relative
       
   133   URL path with another.
       
   134 
       
   135   Args:
       
   136     path: HTTP request relative URL path (with no query arguments)
       
   137     old_suffix: expected suffix at the end of request.path component;
       
   138       if any False value (such as None), the empty string '' is used
       
   139     new_suffix: if non-False, appended to request.path along with a
       
   140       '/' separator (after removing old_suffix if necessary)
       
   141     params: an optional dictionary of query parameters to append to
       
   142       the redirect target; appended as ?<key1>=<value1>&<key2>=...
       
   143       
       
   144   Returns:
       
   145     /path/with/new_suffix?a=1&b=2
       
   146   """    
       
   147   if not old_suffix:
       
   148     old_suffix = ''
       
   149 
       
   150   old_suffix = '/' + old_suffix
       
   151 
       
   152   if path.endswith(old_suffix):
       
   153     # also removes any trailing '/' if old_suffix was empty
       
   154     path = path[:-len(old_suffix)]
       
   155 
       
   156   if new_suffix:
       
   157     # if present, appends new_suffix, after '/' separator
       
   158     path = '%s/%s' % (path, new_suffix)
       
   159 
       
   160   if params:
       
   161     # appends any query parameters, after a '?' and separated by '&'
       
   162     path = '%s?%s' % (path, '&'.join(
       
   163         ['%s=%s' % (p,v) for p,v in params.iteritems()]))
       
   164 
       
   165   return path
       
   166 
       
   167 
       
   168 def redirectToChangedSuffix(
   131 def redirectToChangedSuffix(
   169     request, old_suffix, new_suffix=None, params=None):
   132     request, old_suffix, new_suffix=None, params=None):
   170   """Changes suffix of URL path and returns an HTTP redirect response.
   133   """Changes suffix of URL path and returns an HTTP redirect response.
   171   
   134   
   172   Args:
   135   Args:
   173     request: the Django HTTP request object; redirect path is derived from
   136     request: the Django HTTP request object; redirect path is derived from
   174       request.path
   137       request.path
   175     old_suffix, new_suffix, params:  see replaceSuffix()
   138     old_suffix, new_suffix, params:  see request_helpers.replaceSuffix()
   176       
   139       
   177   Returns:
   140   Returns:
   178     a Django HTTP redirect response pointing to the altered path
   141     a Django HTTP redirect response pointing to the altered path
   179   """
   142   """
   180   path = replaceSuffix(request.path, old_suffix, new_suffix, params=params)
   143   path = request_helpers.replaceSuffix(request.path, old_suffix, new_suffix,
       
   144                                        params=params)
   181   return http.HttpResponseRedirect(path)
   145   return http.HttpResponseRedirect(path)
   182 
       
   183 
       
   184 def isReferrerSelf(request,
       
   185                    expected_prefix=None, suffix=None):
       
   186   """Returns True if HTTP referrer path starts with the HTTP request path.
       
   187     
       
   188   Args:
       
   189     request: the Django HTTP request object; request.path is used if
       
   190       expected_path is not supplied (the most common usage)
       
   191     expected_prefix: optional HTTP path to use instead of the one in
       
   192       request.path; default is None (use request.path)
       
   193     suffix: suffix to remove from the HTTP request path before comparing
       
   194       it to the HTTP referrer path in the HTTP request object headers
       
   195       (this is often an link name, for example, that may be changing from
       
   196       a POST referrer to a GET redirect target) 
       
   197   
       
   198   Returns:
       
   199     True if HTTP referrer path begins with the HTTP request path (either
       
   200       request.path or expected_prefix instead if it was supplied), after
       
   201       any suffix was removed from that request path
       
   202     False otherwise
       
   203        
       
   204   """
       
   205   http_from = request.META.get('HTTP_REFERER')
       
   206       
       
   207   if not http_from:
       
   208     # no HTTP referrer, so cannot possibly start with expected prefix
       
   209     return False
       
   210 
       
   211   from_path = urlparse.urlparse(http_from).path
       
   212   
       
   213   if not expected_prefix:
       
   214     # use HTTP request path, since expected_prefix was not supplied
       
   215     expected_prefix = request.path
       
   216 
       
   217   if suffix:
       
   218     # remove suffix (such as a link name) before comparison
       
   219     chars_to_remove = len(suffix)
       
   220     
       
   221     if not suffix.startswith('/'):
       
   222       chars_to_remove = chars_to_remove + 1
       
   223 
       
   224     expected_prefix = expected_prefix[:-chars_to_remove]
       
   225 
       
   226   if not from_path.startswith(expected_prefix):
       
   227     # expected prefix did not match first part of HTTP referrer path
       
   228     return False
       
   229  
       
   230   # HTTP referrer started with (possibly truncated) expected prefix
       
   231   return True