app/soc/views/helper/forms.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Tue, 14 Oct 2008 16:10:07 +0000
changeset 324 05e21c089be6
parent 274 56e1c1721299
child 394 4c60652a3947
permissions -rw-r--r--
Add missing import in soc/views/site/sponsor/list.py which caused exception when app was deployed and first site you visited was "List Site Sponsors". Update files according to recent django update and django backwards incompatibility (for example newforms is changed to forms). Patch by: Pawel Solyga Review by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Helpers used to display various views that are forms.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
"""
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
__authors__ = [
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    21
  '"Chen Lunpeng" <forever.clp@gmail.com>',
36
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
  '"Todd Larsen" <tlarsen@google.com>',
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
  ]
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
from google.appengine.ext.db import djangoforms
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
324
05e21c089be6 Add missing import in soc/views/site/sponsor/list.py which caused exception when app was deployed and first site you visited was "List Site Sponsors". Update files according to recent django update and django backwards incompatibility (for example newforms is changed to forms).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 274
diff changeset
    28
from django import forms
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    29
from django.utils import safestring
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    30
36
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
class DbModelForm(djangoforms.ModelForm):
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
  """Subclass of Django ModelForm that fixes some label and help_text issues.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
  The default behavior of ModelForm is to use the verbose_name in all
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
  lowercase, capitalizing only the first character, as the displayed field
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
  label.  This class uses verbose_name unaltered as the visible field label
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
  instead.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
  The Property classes used by the App Engine Datastore do not have a
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
  help_text parameter to their constructor.  In a Model class, a help_text
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
  attribute *can* be added to the property after it is created, but the
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
  help text will not be automatically passed along to the Django ModelForm.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
  This class detects the presence of a help_text attribute and adds it to
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
  the corresponding form field object.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
  ugettext_lazy() proxies used for internationalization in the Model will
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
  still work correctly with this new behavior, as long as the original
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
  strings are used as the translation keys.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
  """
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
  def __init__(self, *args, **kwargs):
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
    """Fixes label and help_text issues after parent initialization.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
    Args:
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
      *args, **kwargs:  passed through to parent __init__() constructor
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
    """
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
    super(DbModelForm, self).__init__(*args, **kwargs)
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
    for field_name in self.fields.iterkeys():
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
      # Since fields can be added only to the ModelForm subclass, check to
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
      # see if the Model has a corresponding field first.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
      if hasattr(self.Meta.model, field_name):
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
        model_prop = getattr(self.Meta.model, field_name)
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
        # Check if the Model property defined verbose_name, and copy that
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
        # verbatim to the corresponding field label.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
        if hasattr(model_prop, 'verbose_name'):
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
          self.fields[field_name].label = model_prop.verbose_name
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
        # Check if the Model property added help_text, and copy that verbatim
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
        # to the corresponding field help_text.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
        if hasattr(model_prop, 'help_text'):
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    74
          self.fields[field_name].help_text = model_prop.help_text
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    75
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    76
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    77
class SelectQueryArgForm(forms.Form):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    78
  """URL query argument change control implemented as a Django form.
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    79
  """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    80
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    81
  ONCHANGE_JAVASCRIPT_FMT = '''
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    82
<script type="text/javascript"> 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    83
  function changeArg_%(arg_name)s(item) 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    84
  {
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    85
    var idx=item.selectedIndex;
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    86
    item.selected=true;
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    87
    var value=item.value 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    88
    var url = location.href 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    89
    var reg = /%(arg_name)s=\d+/ 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    90
    url = url.replace(reg, "%(arg_name)s="+value) 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    91
    if(url.match(reg))
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    92
      document.location.href = url 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    93
   else
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    94
      document.location.href = "%(page_path)s?%(arg_name)s="+value; 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    95
  }
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    96
</script>
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    97
'''
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    98
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    99
  def __init__(self, page_path, arg_name, choices, field_name,
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   100
               *form_args, **form_kwargs):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   101
    """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   102
    Args:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   103
      page_path: (usually request.path)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   104
      arg_name: the URL query parameter that determines which choice is
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   105
        selected in the selection control
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   106
      choices: list (or tuple) of value/label string two-tuples, for example:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   107
        (('10', '10 items per page'), ('25', '25 items per page'))
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   108
      field_name: name of the selection field in the form
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   109
      *form_args: positional arguments passed on to the Form base
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   110
        class __init__()
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   111
      *form_kwargs: keyword arguments passed on to the Form base
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   112
        class __init__()
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   113
    """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   114
    super(SelectQueryArgForm, self).__init__(*form_args, **form_kwargs)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   115
    
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   116
    self._script = safestring.mark_safe(self.ONCHANGE_JAVASCRIPT_FMT % {
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   117
        'arg_name': arg_name, 'page_path': page_path,})
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   118
 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   119
    onchange_js_call = 'changeArg_%s(this)' % arg_name
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   120
    
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   121
    self.fields[field_name] = forms.ChoiceField(
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   122
        label='', choices=choices,
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   123
        widget=forms.widgets.Select(attrs={'onchange': onchange_js_call}))
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   124
      
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   125
  def as_table(self):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   126
    """Returns form rendered as HTML <tr> rows -- with no <table></table>.
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   127
    
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   128
    Prepends <script> section with onchange function included.
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   129
    """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   130
    return self._script + super(SelectQueryArgForm, self).as_table()
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   131
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   132
  def as_ul(self):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   133
    """Returns form rendered as HTML <li> list items -- with no <ul></ul>.
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   134
    
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   135
    Prepends <script> section with onchange function included.
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   136
    """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   137
    return self._script + super(SelectQueryArgForm, self).as_ul()
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   138
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   139
  def as_p(self):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   140
    """Returns form rendered as HTML <p> paragraphs.
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   141
    
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   142
    Prepends <script> section with onchange function included.
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   143
    """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   144
    return self._script + super(SelectQueryArgForm, self).as_p()
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   145
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   146
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   147
DEF_SELECT_QUERY_ARG_FIELD_NAME_FMT = 'select_query_arg_%(arg_name)s'
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   148
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   149
def makeSelectQueryArgForm(
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   150
    request, arg_name, initial_value, choices,
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   151
    field_name_fmt=DEF_SELECT_QUERY_ARG_FIELD_NAME_FMT):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   152
  """Wrapper that creates a customized SelectQueryArgForm.
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   153
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   154
  Args:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   155
    request: the standard Django HTTP request object
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   156
    arg_name: the URL query parameter that determines which choice is
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   157
      selected in the selection control
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   158
    initial_value: the initial value of the selection control
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   159
    choices: list (or tuple) of value/label string two-tuples, for example:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   160
      (('10', '10 items per page'), ('25', '25 items per page'))
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   161
    field_name_fmt: optional form field name format string; default is
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   162
      DEF_SELECT_QUERY_ARG_FIELD_NAME_FMT; contains these named format
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   163
      specifiers:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   164
        arg_name: replaced with the arg_name argument
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   165
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   166
  Returns:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   167
    a Django form implementing a query argument selection control, for
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   168
    insertion into a template
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   169
  """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   170
  field_name = field_name_fmt % {'arg_name': arg_name}
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   171
  return SelectQueryArgForm(request.path, arg_name, choices, field_name,
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   172
                            initial={field_name: initial_value})