author | Todd Larsen <tlarsen@google.com> |
Tue, 16 Sep 2008 17:05:07 +0000 | |
changeset 155 | 682c398c5aa7 |
parent 54 | 03e267d67478 |
child 265 | 3c2994f3b85f |
permissions | -rw-r--r-- |
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__ = [ |
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
21 |
'"Todd Larsen" <tlarsen@google.com>', |
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
22 |
] |
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
23 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
24 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
25 |
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
|
26 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
27 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
28 |
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
|
29 |
"""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
|
30 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
31 |
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
|
32 |
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
|
33 |
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
|
34 |
instead. |
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
35 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
36 |
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
|
37 |
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
|
38 |
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
|
39 |
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
|
40 |
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
|
41 |
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
|
42 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
43 |
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
|
44 |
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
|
45 |
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
|
46 |
""" |
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
47 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
48 |
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
|
49 |
"""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
|
50 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
51 |
Args: |
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
52 |
*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
|
53 |
""" |
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
54 |
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
|
55 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
56 |
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
|
57 |
# 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
|
58 |
# 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
|
59 |
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
|
60 |
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
|
61 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
62 |
# 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
|
63 |
# 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
|
64 |
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
|
65 |
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
|
66 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
67 |
# 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
|
68 |
# 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
|
69 |
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
|
70 |
self.fields[field_name].help_text = model_prop.help_text |