diff options
| author | Jason Pellerin <jpellerin@gmail.com> | 2006-12-04 20:29:44 +0000 |
|---|---|---|
| committer | Jason Pellerin <jpellerin@gmail.com> | 2006-12-04 20:29:44 +0000 |
| commit | b7a897eebbd4797c58b004eda7c6a30f9971eac2 (patch) | |
| tree | fd79ac06b0f20909a770a46e2b692fbe506441a2 /django/newforms | |
| parent | 040f2272e0aec724a36e7abda445b61ee065a8f1 (diff) | |
[multi-db] Merged trunk to [4000]. Some tests still failing.
git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@4156 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/newforms')
| -rw-r--r-- | django/newforms/__init__.py | 3 | ||||
| -rw-r--r-- | django/newforms/fields.py | 125 | ||||
| -rw-r--r-- | django/newforms/forms.py | 104 | ||||
| -rw-r--r-- | django/newforms/widgets.py | 76 |
4 files changed, 268 insertions, 40 deletions
diff --git a/django/newforms/__init__.py b/django/newforms/__init__.py index f0eca9a950..2a472d7b39 100644 --- a/django/newforms/__init__.py +++ b/django/newforms/__init__.py @@ -2,8 +2,6 @@ Django validation and HTML form handling. TODO: - Validation not tied to a particular field - <select> and validation of lists Default value for field Field labels Nestable Forms @@ -12,6 +10,7 @@ TODO: "This form field requires foo.js" and form.js_includes() """ +from util import ValidationError from widgets import * from fields import * from forms import Form diff --git a/django/newforms/fields.py b/django/newforms/fields.py index ea401fdfab..79558bbf9e 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -3,7 +3,7 @@ Field classes """ from util import ValidationError, DEFAULT_ENCODING -from widgets import TextInput, CheckboxInput +from widgets import TextInput, CheckboxInput, Select, SelectMultiple import datetime import re import time @@ -12,12 +12,19 @@ __all__ = ( 'Field', 'CharField', 'IntegerField', 'DEFAULT_DATE_INPUT_FORMATS', 'DateField', 'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField', - 'RegexField', 'EmailField', 'BooleanField', + 'RegexField', 'EmailField', 'URLField', 'BooleanField', + 'ChoiceField', 'MultipleChoiceField', + 'ComboField', ) # These values, if given to to_python(), will trigger the self.required check. EMPTY_VALUES = (None, '') +try: + set # Only available in Python 2.4+ +except NameError: + from sets import Set as set # Python 2.3 fallback + class Field(object): widget = TextInput # Default widget to use when rendering this type of Field. @@ -28,9 +35,9 @@ class Field(object): widget = widget() self.widget = widget - def to_python(self, value): + def clean(self, value): """ - Validates the given value and returns its "normalized" value as an + Validates the given value and returns its "cleaned" value as an appropriate Python object. Raises ValidationError for any errors. @@ -44,9 +51,9 @@ class CharField(Field): Field.__init__(self, required, widget) self.max_length, self.min_length = max_length, min_length - def to_python(self, value): + def clean(self, value): "Validates max_length and min_length. Returns a Unicode object." - Field.to_python(self, value) + Field.clean(self, value) if value in EMPTY_VALUES: value = u'' if not isinstance(value, basestring): value = unicode(str(value), DEFAULT_ENCODING) @@ -59,12 +66,12 @@ class CharField(Field): return value class IntegerField(Field): - def to_python(self, value): + def clean(self, value): """ Validates that int() can be called on the input. Returns the result of int(). """ - super(IntegerField, self).to_python(value) + super(IntegerField, self).clean(value) try: return int(value) except (ValueError, TypeError): @@ -83,12 +90,12 @@ class DateField(Field): Field.__init__(self, required, widget) self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS - def to_python(self, value): + def clean(self, value): """ Validates that the input can be converted to a date. Returns a Python datetime.date object. """ - Field.to_python(self, value) + Field.clean(self, value) if value in EMPTY_VALUES: return None if isinstance(value, datetime.datetime): @@ -119,12 +126,12 @@ class DateTimeField(Field): Field.__init__(self, required, widget) self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS - def to_python(self, value): + def clean(self, value): """ Validates that the input can be converted to a datetime. Returns a Python datetime.datetime object. """ - Field.to_python(self, value) + Field.clean(self, value) if value in EMPTY_VALUES: return None if isinstance(value, datetime.datetime): @@ -151,12 +158,12 @@ class RegexField(Field): self.regex = regex self.error_message = error_message or u'Enter a valid value.' - def to_python(self, value): + def clean(self, value): """ Validates that the input matches the regular expression. Returns a Unicode object. """ - Field.to_python(self, value) + Field.clean(self, value) if value in EMPTY_VALUES: value = u'' if not isinstance(value, basestring): value = unicode(str(value), DEFAULT_ENCODING) @@ -175,10 +182,96 @@ class EmailField(RegexField): def __init__(self, required=True, widget=None): RegexField.__init__(self, email_re, u'Enter a valid e-mail address.', required, widget) +url_re = re.compile( + r'^https?://' # http:// or https:// + r'(?:[A-Z0-9-]+\.)+[A-Z]{2,6}' # domain + r'(?::\d+)?' # optional port + r'(?:/?|/\S+)$', re.IGNORECASE) + +class URLField(RegexField): + def __init__(self, required=True, verify_exists=False, widget=None): + RegexField.__init__(self, url_re, u'Enter a valid URL.', required, widget) + self.verify_exists = verify_exists + + def clean(self, value): + value = RegexField.clean(self, value) + if self.verify_exists: + import urllib2 + try: + u = urllib2.urlopen(value) + except ValueError: + raise ValidationError(u'Enter a valid URL.') + except: # urllib2.URLError, httplib.InvalidURL, etc. + raise ValidationError(u'This URL appears to be a broken link.') + return value + class BooleanField(Field): widget = CheckboxInput - def to_python(self, value): + def clean(self, value): "Returns a Python boolean object." - Field.to_python(self, value) + Field.clean(self, value) return bool(value) + +class ChoiceField(Field): + def __init__(self, choices=(), required=True, widget=Select): + if isinstance(widget, type): + widget = widget(choices=choices) + Field.__init__(self, required, widget) + self.choices = choices + + def clean(self, value): + """ + Validates that the input is in self.choices. + """ + value = Field.clean(self, value) + if value in EMPTY_VALUES: value = u'' + if not isinstance(value, basestring): + value = unicode(str(value), DEFAULT_ENCODING) + elif not isinstance(value, unicode): + value = unicode(value, DEFAULT_ENCODING) + valid_values = set([str(k) for k, v in self.choices]) + if value not in valid_values: + raise ValidationError(u'Select a valid choice. %s is not one of the available choices.' % value) + return value + +class MultipleChoiceField(ChoiceField): + def __init__(self, choices=(), required=True, widget=SelectMultiple): + ChoiceField.__init__(self, choices, required, widget) + + def clean(self, value): + """ + Validates that the input is a list or tuple. + """ + if not isinstance(value, (list, tuple)): + raise ValidationError(u'Enter a list of values.') + if self.required and not value: + raise ValidationError(u'This field is required.') + new_value = [] + for val in value: + if not isinstance(val, basestring): + value = unicode(str(val), DEFAULT_ENCODING) + elif not isinstance(val, unicode): + value = unicode(val, DEFAULT_ENCODING) + new_value.append(value) + # Validate that each value in the value list is in self.choices. + valid_values = set([k for k, v in self.choices]) + for val in new_value: + if val not in valid_values: + raise ValidationError(u'Select a valid choice. %s is not one of the available choices.' % val) + return new_value + +class ComboField(Field): + def __init__(self, fields=(), required=True, widget=None): + Field.__init__(self, required, widget) + self.fields = fields + + def clean(self, value): + """ + Validates the given value against all of self.fields, which is a + list of Field instances. + """ + Field.clean(self, value) + for field in self.fields: + value = field.clean(value) + return value diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 34e13c6542..e490d0d5f9 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -6,6 +6,13 @@ from fields import Field from widgets import TextInput, Textarea from util import ErrorDict, ErrorList, ValidationError +NON_FIELD_ERRORS = '__all__' + +def pretty_name(name): + "Converts 'first_name' to 'First name'" + name = name[0].upper() + name[1:] + return name.replace('_', ' ') + class DeclarativeFieldsMetaclass(type): "Metaclass that converts Field attributes to a dictionary called 'fields'." def __new__(cls, name, bases, attrs): @@ -18,22 +25,33 @@ class Form(object): def __init__(self, data=None): # TODO: prefix stuff self.data = data or {} - self.__data_python = None # Stores the data after to_python() has been called. - self.__errors = None # Stores the errors after to_python() has been called. + self.clean_data = None # Stores the data after clean() has been called. + self.__errors = None # Stores the errors after clean() has been called. + + def __str__(self): + return self.as_table() def __iter__(self): for name, field in self.fields.items(): yield BoundField(self, field, name) - def to_python(self): + def __getitem__(self, name): + "Returns a BoundField with the given name." + try: + field = self.fields[name] + except KeyError: + raise KeyError('Key %r not found in Form' % name) + return BoundField(self, field, name) + + def clean(self): if self.__errors is None: - self._validate() - return self.__data_python + self.full_clean() + return self.clean_data def errors(self): "Returns an ErrorDict for self.data" if self.__errors is None: - self._validate() + self.full_clean() return self.__errors def is_valid(self): @@ -44,27 +62,75 @@ class Form(object): """ return not bool(self.errors()) - def __getitem__(self, name): - "Returns a BoundField with the given name." - try: - field = self.fields[name] - except KeyError: - raise KeyError('Key %r not found in Form' % name) - return BoundField(self, field, name) + def as_table(self): + "Returns this form rendered as an HTML <table>." + output = u'\n'.join(['<tr><td>%s:</td><td>%s</td></tr>' % (pretty_name(name), BoundField(self, field, name)) for name, field in self.fields.items()]) + return '<table>\n%s\n</table>' % output - def _validate(self): - data_python = {} + def as_ul(self): + "Returns this form rendered as an HTML <ul>." + output = u'\n'.join(['<li>%s: %s</li>' % (pretty_name(name), BoundField(self, field, name)) for name, field in self.fields.items()]) + return '<ul>\n%s\n</ul>' % output + + def as_table_with_errors(self): + "Returns this form rendered as an HTML <table>, with errors." + output = [] + if self.errors().get(NON_FIELD_ERRORS): + # Errors not corresponding to a particular field are displayed at the top. + output.append('<tr><td colspan="2"><ul>%s</ul></td></tr>' % '\n'.join(['<li>%s</li>' % e for e in self.errors()[NON_FIELD_ERRORS]])) + for name, field in self.fields.items(): + bf = BoundField(self, field, name) + if bf.errors: + output.append('<tr><td colspan="2"><ul>%s</ul></td></tr>' % '\n'.join(['<li>%s</li>' % e for e in bf.errors])) + output.append('<tr><td>%s:</td><td>%s</td></tr>' % (pretty_name(name), bf)) + return '<table>\n%s\n</table>' % '\n'.join(output) + + def as_ul_with_errors(self): + "Returns this form rendered as an HTML <ul>, with errors." + output = [] + if self.errors().get(NON_FIELD_ERRORS): + # Errors not corresponding to a particular field are displayed at the top. + output.append('<li><ul>%s</ul></li>' % '\n'.join(['<li>%s</li>' % e for e in self.errors()[NON_FIELD_ERRORS]])) + for name, field in self.fields.items(): + bf = BoundField(self, field, name) + line = '<li>' + if bf.errors: + line += '<ul>%s</ul>' % '\n'.join(['<li>%s</li>' % e for e in bf.errors]) + line += '%s: %s</li>' % (pretty_name(name), bf) + output.append(line) + return '<ul>\n%s\n</ul>' % '\n'.join(output) + + def full_clean(self): + """ + Cleans all of self.data and populates self.__errors and self.clean_data. + """ + self.clean_data = {} errors = ErrorDict() for name, field in self.fields.items(): + value = self.data.get(name, None) try: - value = field.to_python(self.data.get(name, None)) - data_python[name] = value + value = field.clean(value) + self.clean_data[name] = value + if hasattr(self, 'clean_%s' % name): + value = getattr(self, 'clean_%s' % name)() + self.clean_data[name] = value except ValidationError, e: errors[name] = e.messages - if not errors: # Only set self.data_python if there weren't errors. - self.__data_python = data_python + try: + self.clean_data = self.clean() + except ValidationError, e: + errors[NON_FIELD_ERRORS] = e.messages + if errors: + self.clean_data = None self.__errors = errors + def clean(self): + """ + Hook for doing any extra form-wide cleaning after Field.clean() been + called on every field. + """ + return self.clean_data + class BoundField(object): "A Field plus data" def __init__(self, form, field, name): diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index 2241a54c07..5ec27653cf 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -2,30 +2,55 @@ HTML Widget classes """ -__all__ = ('Widget', 'TextInput', 'Textarea', 'CheckboxInput') +__all__ = ( + 'Widget', 'TextInput', 'PasswordInput', 'HiddenInput', 'FileInput', + 'Textarea', 'CheckboxInput', + 'Select', 'SelectMultiple', +) from django.utils.html import escape +from itertools import chain + +try: + set # Only available in Python 2.4+ +except NameError: + from sets import Set as set # Python 2.3 fallback # Converts a dictionary to a single string with key="value", XML-style. # Assumes keys do not need to be XML-escaped. flatatt = lambda attrs: ' '.join(['%s="%s"' % (k, escape(v)) for k, v in attrs.items()]) class Widget(object): + requires_data_list = False # Determines whether render()'s 'value' argument should be a list. def __init__(self, attrs=None): self.attrs = attrs or {} def render(self, name, value): raise NotImplementedError -class TextInput(Widget): +class Input(Widget): + "Base class for all <input> widgets (except type='checkbox', which is special)" + input_type = None # Subclasses must define this. def render(self, name, value, attrs=None): if value is None: value = '' - final_attrs = dict(self.attrs, type='text', name=name) + final_attrs = dict(self.attrs, type=self.input_type, name=name) if attrs: final_attrs.update(attrs) if value != '': final_attrs['value'] = value # Only add the 'value' attribute if a value is non-empty. return u'<input %s />' % flatatt(final_attrs) +class TextInput(Input): + input_type = 'text' + +class PasswordInput(Input): + input_type = 'password' + +class HiddenInput(Input): + input_type = 'hidden' + +class FileInput(Input): + input_type = 'file' + class Textarea(Widget): def render(self, name, value, attrs=None): if value is None: value = '' @@ -41,3 +66,48 @@ class CheckboxInput(Widget): final_attrs.update(attrs) if value: final_attrs['checked'] = 'checked' return u'<input %s />' % flatatt(final_attrs) + +class Select(Widget): + def __init__(self, attrs=None, choices=()): + # choices can be any iterable + self.attrs = attrs or {} + self.choices = choices + + def render(self, name, value, attrs=None, choices=()): + if value is None: value = '' + final_attrs = dict(self.attrs, name=name) + if attrs: + final_attrs.update(attrs) + output = [u'<select %s>' % flatatt(final_attrs)] + str_value = str(value) # Normalize to string. + for option_value, option_label in chain(self.choices, choices): + selected_html = (str(option_value) == str_value) and ' selected="selected"' or '' + output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(option_label))) + output.append(u'</select>') + return u'\n'.join(output) + +class SelectMultiple(Widget): + requires_data_list = True + def __init__(self, attrs=None, choices=()): + # choices can be any iterable + self.attrs = attrs or {} + self.choices = choices + + def render(self, name, value, attrs=None, choices=()): + if value is None: value = [] + final_attrs = dict(self.attrs, name=name) + if attrs: + final_attrs.update(attrs) + output = [u'<select multiple="multiple" %s>' % flatatt(final_attrs)] + str_values = set([str(v) for v in value]) # Normalize to strings. + for option_value, option_label in chain(self.choices, choices): + selected_html = (str(option_value) in str_values) and ' selected="selected"' or '' + output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(option_label))) + output.append(u'</select>') + return u'\n'.join(output) + +class RadioSelect(Widget): + pass + +class CheckboxSelectMultiple(Widget): + pass |
