author | Lennard de Rijk <ljvderijk@gmail.com> |
Mon, 16 Mar 2009 15:33:27 +0000 | |
changeset 1889 | 4356a9201d20 |
parent 1783 | a456d120c902 |
child 2077 | fd2e83a297c7 |
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 |
# |
1308
35b75ffcbb37
Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1307
diff
changeset
|
3 |
# Copyright 2008 the Melange authors. |
36
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 |
265
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
30 |
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
|
31 |
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
|
32 |
|
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
|
33 |
|
741
2dc2c65c5f76
Converted as_table to be a template tag
Sverre Rabbelier <srabbelier@gmail.com>
parents:
739
diff
changeset
|
34 |
class BaseForm(djangoforms.ModelForm): |
36
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
35 |
"""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
|
36 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
37 |
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
|
38 |
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
|
39 |
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
|
40 |
instead. |
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
41 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
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
|
46 |
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
|
47 |
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
|
48 |
|
970
8b5611d5b053
Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents:
866
diff
changeset
|
49 |
ugettext() proxies used for internationalization in the Model will |
36
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
50 |
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
|
51 |
strings are used as the translation keys. |
1451
ef134d062b83
Add generic datepicker support
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1360
diff
changeset
|
52 |
|
ef134d062b83
Add generic datepicker support
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1360
diff
changeset
|
53 |
Also set class date-pick or datetime-pick for DateField or DateTimeField. |
36
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
54 |
""" |
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
55 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
56 |
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
|
57 |
"""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
|
58 |
|
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
59 |
Args: |
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
60 |
*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
|
61 |
""" |
1783
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
62 |
|
753
7d6dead86b59
Since DbModelForm has been renamed to BaseForm change super() call from BaseForm to djangoforms.ModelForm in soc.views.helper.forms module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
741
diff
changeset
|
63 |
super(djangoforms.ModelForm, self).__init__(*args, **kwargs) |
36
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
64 |
|
1783
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
65 |
renames = { |
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
66 |
'verbose_name': 'label', |
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
67 |
'help_text': 'help_text', |
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
68 |
'example_text': 'example_text', |
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
69 |
'group': 'group', |
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
70 |
} |
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
71 |
|
36
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
72 |
for field_name in self.fields.iterkeys(): |
1783
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
73 |
field = self.fields[field_name] |
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
74 |
|
36
e2f03be8ced9
DbModelForm class that fixes verbose_name and help_text handling for Django
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
75 |
# 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
|
76 |
# 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
|
77 |
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
|
78 |
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
|
79 |
|
1783
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
80 |
for old, new in renames.iteritems(): |
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
81 |
value = getattr(model_prop, old, None) |
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
82 |
if value and not getattr(field, new, None): |
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
83 |
setattr(field, new, value) |
1360
f62c462037b6
Added grouping support to all forms, converted role as example
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1308
diff
changeset
|
84 |
|
1451
ef134d062b83
Add generic datepicker support
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1360
diff
changeset
|
85 |
if isinstance(self.fields[field_name], forms.DateField): |
1783
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
86 |
field.widget.attrs['class'] = 'date-pick' |
1451
ef134d062b83
Add generic datepicker support
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1360
diff
changeset
|
87 |
|
ef134d062b83
Add generic datepicker support
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1360
diff
changeset
|
88 |
if isinstance(self.fields[field_name], forms.DateTimeField): |
1783
a456d120c902
Do not override fields in the model if they are already set
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1451
diff
changeset
|
89 |
field.widget.attrs['class'] = 'datetime-pick' |
1451
ef134d062b83
Add generic datepicker support
Sverre Rabbelier <srabbelier@gmail.com>
parents:
1360
diff
changeset
|
90 |
|
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
|
91 |
|
265
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
92 |
class SelectQueryArgForm(forms.Form): |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
93 |
"""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
|
94 |
""" |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
95 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
96 |
ONCHANGE_JAVASCRIPT_FMT = ''' |
1167
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
97 |
<script type="text/javascript"> |
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
98 |
function changeArg_%(arg_name)s(item) |
265
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
99 |
{ |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
100 |
var idx=item.selectedIndex; |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
101 |
item.selected=true; |
1167
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
102 |
var value=item.value; |
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
103 |
var url = location.href; |
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
104 |
var ourreg = /%(arg_name)s=\d+/; |
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
105 |
var anyreg = /\w+=\d+/; |
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
106 |
if(url.match(ourreg)) |
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
107 |
url = url.replace(ourreg, "%(arg_name)s="+value); |
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
108 |
else if(url.match(anyreg)) |
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
109 |
url = url + "&%(arg_name)s=" + value; |
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
110 |
else |
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
111 |
url = "%(page_path)s?%(arg_name)s="+value; |
346117bd30cf
Fix the JS but mentioned in the previous commit
Sverre Rabbelier <srabbelier@gmail.com>
parents:
970
diff
changeset
|
112 |
document.location.href = url; |
265
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
113 |
} |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
114 |
</script> |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
115 |
''' |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
116 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
117 |
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
|
118 |
*form_args, **form_kwargs): |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
119 |
""" |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
120 |
Args: |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
121 |
page_path: (usually request.path) |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
122 |
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
|
123 |
selected in the selection control |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
124 |
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
|
125 |
(('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
|
126 |
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
|
127 |
*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
|
128 |
class __init__() |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
129 |
*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
|
130 |
class __init__() |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
131 |
""" |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
132 |
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
|
133 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
134 |
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
|
135 |
'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
|
136 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
137 |
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
|
138 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
139 |
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
|
140 |
label='', choices=choices, |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
141 |
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
|
142 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
143 |
def as_table(self): |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
144 |
"""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
|
145 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
146 |
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
|
147 |
""" |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
148 |
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
|
149 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
150 |
def as_ul(self): |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
151 |
"""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
|
152 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
153 |
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
|
154 |
""" |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
155 |
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
|
156 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
157 |
def as_p(self): |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
158 |
"""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
|
159 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
160 |
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
|
161 |
""" |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
162 |
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
|
163 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
164 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
165 |
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
|
166 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
167 |
def makeSelectQueryArgForm( |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
168 |
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
|
169 |
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
|
170 |
"""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
|
171 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
172 |
Args: |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
173 |
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
|
174 |
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
|
175 |
selected in the selection control |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
176 |
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
|
177 |
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
|
178 |
(('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
|
179 |
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
|
180 |
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
|
181 |
specifiers: |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
182 |
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
|
183 |
|
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
184 |
Returns: |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
185 |
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
|
186 |
insertion into a template |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
187 |
""" |
3c2994f3b85f
List views should have a selectable pagination "page" length:
Todd Larsen <tlarsen@google.com>
parents:
54
diff
changeset
|
188 |
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
|
189 |
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
|
190 |
initial={field_name: initial_value}) |
612
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
191 |
|
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
192 |
|
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
193 |
def collectCleanedFields(form): |
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
194 |
"""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
|
195 |
|
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
196 |
Args: |
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
197 |
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
|
198 |
|
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
|
199 |
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
|
200 |
All the fields that are in the form's cleaned_data property are returned. |
866
6c918ceba37a
Fixed a typo in the comments of soc/views/helper/forms.py
Lennard de Rijk <ljvderijk@gmail.com>
parents:
753
diff
changeset
|
201 |
If there is a key_name field, it is not included in the returned fields, |
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
|
202 |
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
|
203 |
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
|
204 |
""" |
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
205 |
|
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
206 |
fields = {} |
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
207 |
|
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
208 |
key_name = None |
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
209 |
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
|
210 |
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
|
211 |
|
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
212 |
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
|
213 |
fields[field] = value |
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
214 |
|
3cca81b1e5a1
Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents:
472
diff
changeset
|
215 |
return key_name, fields |