app/soc/views/helpers/request_helpers.py
changeset 189 1cf3e7531382
child 190 b1351bf81064
equal deleted inserted replaced
188:27ed1a09c98d 189:1cf3e7531382
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Helpers for manipulating HTTP requests.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Todd Larsen" <tlarsen@google.com>',
       
    22   ]
       
    23 
       
    24 
       
    25 def getSingleIndexedParamValue(request, param_name, values=()):
       
    26   """Returns a value indexed by a query parameter in the HTTP request.
       
    27   
       
    28   Args:
       
    29     request: the Django HTTP request object
       
    30     param_name: name of the query parameter in the HTTP request
       
    31     values: list (or tuple) of ordered values; one of which is
       
    32       retrieved by the index value of the param_name argument in
       
    33       the HTTP request
       
    34       
       
    35   Returns:
       
    36     None if the query parameter was not present, was not an integer, or
       
    37       was an integer that is not a valid [0..len(values)-1] index into
       
    38       the values list.
       
    39     Otherwise, returns values[int(param_name value)]
       
    40   """
       
    41   value_idx = request.GET.get(param_name)
       
    42   
       
    43   if isinstance(value_idx, (tuple, list)):
       
    44     # keep only the first argument if multiple are present
       
    45     value_idx = value_idx[0]
       
    46 
       
    47   try:
       
    48     # GET parameter 'param_name' should be an integer value index
       
    49     value_idx = int(value_idx)
       
    50   except:
       
    51     # ignore bogus or missing parameter values, so return None (no message)
       
    52     return None
       
    53     
       
    54   if value_idx < 0:
       
    55     # value index out of range, so return None (no value)
       
    56     return None
       
    57 
       
    58   if value_idx >= len(values):
       
    59     # value index out of range, so return None (no value)
       
    60     return None
       
    61 
       
    62   # return value associated with valid value index
       
    63   return values[value_idx]
       
    64 
       
    65 
       
    66 def getSingleIndexedParamValueIfMissing(value, request, param_name,
       
    67                                         values=()):
       
    68   """Returns missing value indexed by a query parameter in the HTTP request.
       
    69   
       
    70   Args:
       
    71     value: an existing value, or a "False" value such as None
       
    72     request, param_name, values: see getSingleIndexParamValue()
       
    73     
       
    74   Returns:
       
    75     value, if value is "non-False"
       
    76     Otherwise, returns getSingleIndexedParamValue() result.
       
    77   """
       
    78   if value:
       
    79     # value already present, so return it
       
    80     return value
       
    81 
       
    82   return getSingleIndexedParamValue(request, param_name, values=values)
       
    83 
       
    84 
       
    85 # TODO(tlarsen):  write getMultipleIndexParamValues() that returns a
       
    86 #   list of values if present, omitting those values that are
       
    87 #   out of range