summaryrefslogtreecommitdiff
path: root/django/newforms/forms.py
diff options
context:
space:
mode:
authorJeremy Dunck <jdunck@gmail.com>2007-06-18 16:48:27 +0000
committerJeremy Dunck <jdunck@gmail.com>2007-06-18 16:48:27 +0000
commitbdcc95e5cce2754d78055f86d561ba2be92ba854 (patch)
tree08a7e4c86244cb42fe577aec5c03a57b023822c2 /django/newforms/forms.py
parent48c9f87e1f3ba9523d79c09f52c0ccc6221f08bf (diff)
gis: Merged revisions 4786-5490 via svnmerge from
http://code.djangoproject.com/svn/django/trunk git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@5492 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/newforms/forms.py')
-rw-r--r--django/newforms/forms.py65
1 files changed, 39 insertions, 26 deletions
diff --git a/django/newforms/forms.py b/django/newforms/forms.py
index 2b3aa97428..6ebebded4b 100644
--- a/django/newforms/forms.py
+++ b/django/newforms/forms.py
@@ -2,12 +2,15 @@
Form classes
"""
-from django.utils.datastructures import SortedDict, MultiValueDict
+import copy
+
+from django.utils.datastructures import SortedDict
from django.utils.html import escape
+from django.utils.encoding import StrAndUnicode
+
from fields import Field
-from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput
-from util import flatatt, StrAndUnicode, ErrorDict, ErrorList, ValidationError
-import copy
+from widgets import TextInput, Textarea
+from util import flatatt, ErrorDict, ErrorList, ValidationError
__all__ = ('BaseForm', 'Form')
@@ -60,7 +63,7 @@ class BaseForm(StrAndUnicode):
self.auto_id = auto_id
self.prefix = prefix
self.initial = initial or {}
- self.__errors = None # Stores the errors after clean() has been called.
+ self._errors = None # Stores the errors after clean() has been called.
# The base_fields class attribute is the *class-wide* definition of
# fields. Because a particular *instance* of the class might want to
@@ -84,12 +87,12 @@ class BaseForm(StrAndUnicode):
raise KeyError('Key %r not found in Form' % name)
return BoundField(self, field, name)
- def _errors(self):
+ def _get_errors(self):
"Returns an ErrorDict for self.data"
- if self.__errors is None:
+ if self._errors is None:
self.full_clean()
- return self.__errors
- errors = property(_errors)
+ return self._errors
+ errors = property(_get_errors)
def is_valid(self):
"""
@@ -121,7 +124,14 @@ class BaseForm(StrAndUnicode):
else:
if errors_on_separate_row and bf_errors:
output.append(error_row % bf_errors)
- label = bf.label and bf.label_tag(escape(bf.label + ':')) or ''
+ if bf.label:
+ label = escape(bf.label)
+ # Only add a colon if the label does not end in punctuation.
+ if label[-1] not in ':?.!':
+ label += ':'
+ label = bf.label_tag(label) or ''
+ else:
+ label = ''
if field.help_text:
help_text = help_text_html % field.help_text
else:
@@ -161,13 +171,13 @@ class BaseForm(StrAndUnicode):
def full_clean(self):
"""
- Cleans all of self.data and populates self.__errors and self.clean_data.
+ Cleans all of self.data and populates self._errors and
+ self.cleaned_data.
"""
- errors = ErrorDict()
+ self._errors = ErrorDict()
if not self.is_bound: # Stop further processing.
- self.__errors = errors
return
- self.clean_data = {}
+ self.cleaned_data = {}
for name, field in self.fields.items():
# value_from_datadict() gets the data from the dictionary.
# Each widget type knows how to retrieve its own data, because some
@@ -175,19 +185,20 @@ class BaseForm(StrAndUnicode):
value = field.widget.value_from_datadict(self.data, self.add_prefix(name))
try:
value = field.clean(value)
- self.clean_data[name] = value
+ self.cleaned_data[name] = value
if hasattr(self, 'clean_%s' % name):
value = getattr(self, 'clean_%s' % name)()
- self.clean_data[name] = value
+ self.cleaned_data[name] = value
except ValidationError, e:
- errors[name] = e.messages
+ self._errors[name] = e.messages
+ if name in self.cleaned_data:
+ del self.cleaned_data[name]
try:
- self.clean_data = self.clean()
+ self.cleaned_data = self.clean()
except ValidationError, e:
- errors[NON_FIELD_ERRORS] = e.messages
- if errors:
- delattr(self, 'clean_data')
- self.__errors = errors
+ self._errors[NON_FIELD_ERRORS] = e.messages
+ if self._errors:
+ delattr(self, 'cleaned_data')
def clean(self):
"""
@@ -196,7 +207,7 @@ class BaseForm(StrAndUnicode):
not be associated with a particular field; it will have a special-case
association with the field named '__all__'.
"""
- return self.clean_data
+ return self.cleaned_data
class Form(BaseForm):
"A collection of Fields, plus their associated data."
@@ -227,9 +238,9 @@ class BoundField(StrAndUnicode):
value = self.as_widget(self.field.widget)
if not isinstance(value, basestring):
# Some Widget render() methods -- notably RadioSelect -- return a
- # "special" object rather than a string. Call the __str__() on that
+ # "special" object rather than a string. Call __unicode__() on that
# object to get its rendered value.
- value = value.__str__()
+ value = unicode(value)
return value
def _errors(self):
@@ -243,10 +254,12 @@ class BoundField(StrAndUnicode):
def as_widget(self, widget, attrs=None):
attrs = attrs or {}
auto_id = self.auto_id
- if auto_id and not attrs.has_key('id') and not widget.attrs.has_key('id'):
+ if auto_id and 'id' not in attrs and 'id' not in widget.attrs:
attrs['id'] = auto_id
if not self.form.is_bound:
data = self.form.initial.get(self.name, self.field.initial)
+ if callable(data):
+ data = data()
else:
data = self.data
return widget.render(self.html_name, data, attrs=attrs)