app/soc/views/helpers/forms_helpers.py
changeset 54 03e267d67478
parent 36 e2f03be8ced9
child 265 3c2994f3b85f
equal deleted inserted replaced
53:57b4279d8c4e 54:03e267d67478
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Helpers used to display various views that are forms.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Todd Larsen" <tlarsen@google.com>',
       
    22   ]
       
    23 
       
    24 
       
    25 from google.appengine.ext.db import djangoforms
       
    26 
       
    27 
       
    28 class DbModelForm(djangoforms.ModelForm):
       
    29   """Subclass of Django ModelForm that fixes some label and help_text issues.
       
    30 
       
    31   The default behavior of ModelForm is to use the verbose_name in all
       
    32   lowercase, capitalizing only the first character, as the displayed field
       
    33   label.  This class uses verbose_name unaltered as the visible field label
       
    34   instead.
       
    35 
       
    36   The Property classes used by the App Engine Datastore do not have a
       
    37   help_text parameter to their constructor.  In a Model class, a help_text
       
    38   attribute *can* be added to the property after it is created, but the
       
    39   help text will not be automatically passed along to the Django ModelForm.
       
    40   This class detects the presence of a help_text attribute and adds it to
       
    41   the corresponding form field object.
       
    42 
       
    43   ugettext_lazy() proxies used for internationalization in the Model will
       
    44   still work correctly with this new behavior, as long as the original
       
    45   strings are used as the translation keys.
       
    46   """
       
    47 
       
    48   def __init__(self, *args, **kwargs):
       
    49     """Fixes label and help_text issues after parent initialization.
       
    50 
       
    51     Args:
       
    52       *args, **kwargs:  passed through to parent __init__() constructor
       
    53     """
       
    54     super(DbModelForm, self).__init__(*args, **kwargs)
       
    55 
       
    56     for field_name in self.fields.iterkeys():
       
    57       # Since fields can be added only to the ModelForm subclass, check to
       
    58       # see if the Model has a corresponding field first.
       
    59       if hasattr(self.Meta.model, field_name):
       
    60         model_prop = getattr(self.Meta.model, field_name)
       
    61 
       
    62         # Check if the Model property defined verbose_name, and copy that
       
    63         # verbatim to the corresponding field label.
       
    64         if hasattr(model_prop, 'verbose_name'):
       
    65           self.fields[field_name].label = model_prop.verbose_name
       
    66 
       
    67         # Check if the Model property added help_text, and copy that verbatim
       
    68         # to the corresponding field help_text.
       
    69         if hasattr(model_prop, 'help_text'):
       
    70           self.fields[field_name].help_text = model_prop.help_text