app/soc/views/helper/forms.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sun, 14 Dec 2008 00:33:29 +0000
changeset 739 b245c2b814c5
parent 639 1f92bd41b914
child 741 2dc2c65c5f76
permissions -rw-r--r--
Added support for example texts Initial patch by Benjamin, some minor fixes by Sverre. Patch by: Benjamin Lyaunzbe, Sverre Rabbelier
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>',
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    23
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
36
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
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
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
    28
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
    29
from django import forms
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    30
from django.forms import forms as forms_in
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    31
from django.forms import util
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    32
from django.utils import encoding
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    33
from django.utils import safestring
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    34
from django.utils.encoding import force_unicode
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    35
from django.utils.html import escape
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    36
from django.utils.safestring import mark_safe
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    37
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    38
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    39
class CustomErrorList(util.ErrorList):
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    40
  """A collection of errors that knows how to display itself in various formats.
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    41
  
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    42
  This class has customized as_text method output which puts errors inside <span>
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    43
  with formfielderrorlabel class.
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    44
  """
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    45
  def __unicode__(self):
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    46
    return self.as_text()
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    47
  
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    48
  def as_text(self):
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    49
    """Returns error list rendered as text inside <span>."""
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    50
    if not self:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    51
      return u''
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    52
    errors_text = u'\n'.join([u'%s' % encoding.force_unicode(e) for e in self])
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    53
    return u'<span class="formfielderrorlabel">%(errors)s</span><br />' % \
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
    54
        {'errors': errors_text}
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
    55
36
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
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
    58
  """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
    59
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
  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
    61
  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
    62
  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
    63
  instead.
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
  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
    66
  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
    67
  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
    68
  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
    69
  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
    70
  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
    71
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
  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
    73
  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
    74
  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
    75
  """
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    76
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
  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
    78
    """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
    79
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
    Args:
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
      *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
    82
    """
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
    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
    84
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
    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
    86
      # 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
    87
      # 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
    88
      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
    89
        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
    90
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    91
        # 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
    92
        # 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
    93
        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
    94
          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
    95
e2f03be8ced9 DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    96
        # 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
    97
        # 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
    98
        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
    99
          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
   100
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   101
        # Check if the Model property added example_text, and copy that verbatim
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   102
        # to the corresponding field help_text.
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   103
        if hasattr(model_prop, 'example_text'):
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   104
          self.fields[field_name].example_text = model_prop.example_text
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   105
394
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   106
class BaseForm(DbModelForm):
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   107
  """Subclass of DbModelForm that extends as_table HTML output.
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   108
  
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   109
  BaseForm has additional class names in HTML tags for label and help text
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   110
  and those can be used in CSS files for look customization. The way the Form
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   111
  prints itself also has changed. Help text is displayed in the same row as 
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   112
  label and input.
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   113
  """
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   114
  
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   115
  DEF_NORMAL_ROW = u'<tr title="%(help_text)s"><td class=' \
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   116
      '"%(field_class_type)s">%(label)s</td><td>' \
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   117
      '%(errors)s%(field)s%(required)s%(example_text)s</td></tr>'
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   118
  DEF_ERROR_ROW = u'<tr><td>&nbsp;</td><td class="formfielderror">%s</td></tr>'
394
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   119
  DEF_ROW_ENDER = '</td></tr>'
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   120
  DEF_REQUIRED_HTML = u'<td class="formfieldrequired">(required)</td>'
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   121
  DEF_HELP_TEXT_HTML = u'%s'
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   122
  DEF_EXAMPLE_TEXT_HTML = u'<td class="formfieldexample">e.g. %s</td>'
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   123
459
2cfcedaf7c16 Preparation for generic user views
Sverre Rabbelier <srabbelier@gmail.com>
parents: 394
diff changeset
   124
394
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   125
  def __init__(self, *args, **kwargs):
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   126
    """Parent class initialization.
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   127
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   128
    Args:
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   129
      *args, **kwargs:  passed through to parent __init__() constructor
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   130
    """
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   131
    super(BaseForm, self).__init__(error_class=CustomErrorList, *args, **kwargs)
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   132
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   133
  def _html_output_with_required(self, normal_row, error_row, row_ender,
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   134
          help_text_html, required_html, example_text_html, errors_on_separate_row):
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   135
    """Helper function for outputting HTML.
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   136
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   137
    Used by as_table(), as_ul(), as_p(). Displays information
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   138
    about required fields.
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   139
    """
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   140
    # Errors that should be displayed above all fields.
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   141
    top_errors = self.non_field_errors()
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   142
    output, hidden_fields = [], []
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   143
    for name, field in self.fields.items():
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   144
      bf = forms_in.BoundField(self, field, name)
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   145
      # Escape and cache in local variable.
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   146
      bf_errors = self.error_class([escape(error) for error in bf.errors])
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   147
      if bf.is_hidden:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   148
        if bf_errors:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   149
          top_errors.extend([u'(Hidden field %s) %s' % \
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   150
              (name, force_unicode(e)) for e in bf_errors])
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   151
        hidden_fields.append(unicode(bf))
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   152
      else:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   153
        if errors_on_separate_row and bf_errors:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   154
          output.append(error_row % force_unicode(bf_errors))
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   155
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   156
        if bf.label:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   157
          label = escape(force_unicode(bf.label))
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   158
          # Only add the suffix if the label does not end in
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   159
          # punctuation.
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   160
          if self.label_suffix:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   161
            if label[-1] not in ':?.!':
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   162
              label += self.label_suffix
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   163
          label = bf.label_tag(label) or ''
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   164
        else:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   165
          label = ''
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   166
        if field.help_text:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   167
          help_text = help_text_html % force_unicode(field.help_text)
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   168
        else:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   169
          help_text = u''
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   170
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   171
        if bf_errors:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   172
          field_class_type = u'formfielderrorlabel'
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   173
        else:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   174
          field_class_type = u'formfieldlabel'
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   175
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   176
        if field.required:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   177
          required = required_html
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   178
        else:
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   179
          required = u'<td></td>'
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   180
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   181
        if hasattr(field, 'example_text'):
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   182
          example_text = example_text_html % force_unicode(field.example_text)
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   183
        else:
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   184
          example_text = u'<td></td>'
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   185
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   186
        if errors_on_separate_row and bf_errors:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   187
          errors = u''
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   188
        else:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   189
          errors = force_unicode(bf_errors)
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   190
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   191
        output.append(normal_row % {'field_class_type': field_class_type,
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   192
                                    'errors': errors,
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   193
                                    'label': force_unicode(label),
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   194
                                    'field': unicode(bf),
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   195
                                    'required': required,
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   196
                                    'help_text': help_text,
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   197
                                    'example_text': example_text})
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   198
    if top_errors:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   199
      output.insert(0, error_row % force_unicode(top_errors))
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   200
    if hidden_fields: # Insert any hidden fields in the last row.
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   201
      str_hidden = u''.join(hidden_fields)
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   202
      if output:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   203
        last_row = output[-1]
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   204
        # Chop off the trailing row_ender (e.g. '</td></tr>') and
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   205
        # insert the hidden fields.
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   206
        if not last_row.endswith(row_ender):
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   207
          # This can happen in the as_p() case (and possibly others
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   208
          # that users write): if there are only top errors, we may
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   209
          # not be able to conscript the last row for our purposes,
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   210
          # so insert a new, empty row.
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   211
          last_row = normal_row % {'errors': '', 'label': '',
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   212
                                   'field': '', 'help_text': '', 'example_text': ''}
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   213
          output.append(last_row)
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   214
        output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   215
      else:
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   216
        # If there aren't any rows in the output, just append the
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   217
        # hidden fields.
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   218
        output.append(str_hidden)
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   219
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   220
    return mark_safe(u'\n'.join(output))
394
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   221
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   222
  def as_table(self):
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   223
    """Returns form rendered as HTML <tr> rows -- with no <table></table>."""
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   224
472
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   225
    return self._html_output_with_required(self.DEF_NORMAL_ROW,
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   226
                                           self.DEF_ERROR_ROW,
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   227
                                           self.DEF_ROW_ENDER,
519c298a4f87 Move "(required)" text to third column instead of second column in templatetags, it's much more user friendly that way. Add new version of as_table that support our current error messages format, information about required fields and tooltips.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 459
diff changeset
   228
                                           self.DEF_HELP_TEXT_HTML,
739
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   229
                                           self.DEF_REQUIRED_HTML,
b245c2b814c5 Added support for example texts
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   230
                                           self.DEF_EXAMPLE_TEXT_HTML, True)
394
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   231
4c60652a3947 Add BaseForm class to soc.views.helper.forms module (work in progress). This changes the way as_table function displays the form (for more information have a look into doc string). BaseForm is going to be used for all forms in Melange in future. Right now it's still missing custom form errors labels and "required" text in 3rd column, but that's added as TODO and I'm working on it.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 324
diff changeset
   232
265
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   233
class SelectQueryArgForm(forms.Form):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   234
  """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
   235
  """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   236
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   237
  ONCHANGE_JAVASCRIPT_FMT = '''
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   238
<script type="text/javascript"> 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   239
  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
   240
  {
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   241
    var idx=item.selectedIndex;
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   242
    item.selected=true;
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   243
    var value=item.value 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   244
    var url = location.href 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   245
    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
   246
    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
   247
    if(url.match(reg))
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   248
      document.location.href = url 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   249
   else
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   250
      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
   251
  }
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   252
</script>
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   253
'''
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   254
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   255
  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
   256
               *form_args, **form_kwargs):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   257
    """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   258
    Args:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   259
      page_path: (usually request.path)
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   260
      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
   261
        selected in the selection control
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   262
      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
   263
        (('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
   264
      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
   265
      *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
   266
        class __init__()
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   267
      *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
   268
        class __init__()
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   269
    """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   270
    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
   271
    
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   272
    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
   273
        '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
   274
 
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   275
    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
   276
    
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   277
    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
   278
        label='', choices=choices,
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   279
        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
   280
      
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   281
  def as_table(self):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   282
    """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
   283
    
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   284
    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
   285
    """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   286
    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
   287
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   288
  def as_ul(self):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   289
    """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
   290
    
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   291
    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
   292
    """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   293
    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
   294
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   295
  def as_p(self):
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   296
    """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
   297
    
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   298
    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
   299
    """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   300
    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
   301
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   302
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   303
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
   304
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   305
def makeSelectQueryArgForm(
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   306
    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
   307
    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
   308
  """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
   309
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   310
  Args:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   311
    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
   312
    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
   313
      selected in the selection control
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   314
    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
   315
    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
   316
      (('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
   317
    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
   318
      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
   319
      specifiers:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   320
        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
   321
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   322
  Returns:
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   323
    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
   324
    insertion into a template
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   325
  """
3c2994f3b85f List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents: 54
diff changeset
   326
  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
   327
  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
   328
                            initial={field_name: initial_value})
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   329
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   330
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   331
def collectCleanedFields(form):
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   332
  """Collects all cleaned fields and returns them with the key_name.
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   333
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   334
  Args:
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   335
    form: The form from which the cleaned fields should be collected
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   336
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 612
diff changeset
   337
  Returns: 
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 612
diff changeset
   338
    All the fields that are in the form's cleaned_data property are returned. 
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 612
diff changeset
   339
    If there is a key_name field, it is not included in the returend fields, 
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 612
diff changeset
   340
    instead, it is returned as the first element in the returned tuple. 
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 612
diff changeset
   341
    If no key_name field is present, None is returned as first value instead.
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   342
  """
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   343
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   344
  fields = {}
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   345
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   346
  key_name = None
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   347
  if 'key_name' in form.cleaned_data:
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   348
    key_name = form.cleaned_data.pop('key_name')
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   349
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   350
  for field, value in form.cleaned_data.iteritems():
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   351
    fields[field] = value
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   352
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 472
diff changeset
   353
  return key_name, fields