app/soc/views/helpers/request_helpers.py
author Todd Larsen <tlarsen@google.com>
Tue, 23 Sep 2008 23:20:23 +0000
changeset 189 1cf3e7531382
child 190 b1351bf81064
permissions -rw-r--r--
Split out HTTP request manipulation functions from template_helpers.py, which is in danger of becoming a "misc" or "util" module unnecessarily.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
189
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
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
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
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
    26
  """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
    27
  
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
  Args:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
    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
    30
    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
    31
    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
    32
      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
    33
      the HTTP request
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
      
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
  Returns:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
    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
    37
      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
    38
      the values list.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
    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
    40
  """
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
  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
    42
  
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
  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
    44
    # 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
    45
    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
    46
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
  try:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
    # 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
    49
    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
    50
  except:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
    # 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
    52
    return None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
    
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
  if value_idx < 0:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
    # 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
    56
    return None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
  if value_idx >= len(values):
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
    # value index out of range, so return None (no value)
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
    return None
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
  # 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
    63
  return values[value_idx]
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
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
    67
                                        values=()):
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
  """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
    69
  
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
  Args:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
    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
    72
    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
    73
    
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    74
  Returns:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
    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
    76
    Otherwise, returns getSingleIndexedParamValue() result.
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
  """
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
  if value:
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
    # 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
    80
    return value
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
  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
    83
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
1cf3e7531382 Split out HTTP request manipulation functions from template_helpers.py, which
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
# 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
    86
#   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
    87
#   out of range