diff options
Diffstat (limited to 'django/newforms')
| -rw-r--r-- | django/newforms/__init__.py | 28 | ||||
| -rw-r--r-- | django/newforms/fields.py | 295 | ||||
| -rw-r--r-- | django/newforms/forms.py | 169 | ||||
| -rw-r--r-- | django/newforms/util.py | 55 | ||||
| -rw-r--r-- | django/newforms/widgets.py | 113 |
5 files changed, 660 insertions, 0 deletions
diff --git a/django/newforms/__init__.py b/django/newforms/__init__.py new file mode 100644 index 0000000000..2a472d7b39 --- /dev/null +++ b/django/newforms/__init__.py @@ -0,0 +1,28 @@ +""" +Django validation and HTML form handling. + +TODO: + Default value for field + Field labels + Nestable Forms + FatalValidationError -- short-circuits all other validators on a form + ValidationWarning + "This form field requires foo.js" and form.js_includes() +""" + +from util import ValidationError +from widgets import * +from fields import * +from forms import Form + +########################## +# DATABASE API SHORTCUTS # +########################## + +def form_for_model(model): + "Returns a Form instance for the given Django model class." + raise NotImplementedError + +def form_for_fields(field_list): + "Returns a Form instance for the given list of Django database field instances." + raise NotImplementedError diff --git a/django/newforms/fields.py b/django/newforms/fields.py new file mode 100644 index 0000000000..54089cb3c3 --- /dev/null +++ b/django/newforms/fields.py @@ -0,0 +1,295 @@ +""" +Field classes +""" + +from util import ValidationError, DEFAULT_ENCODING +from widgets import TextInput, CheckboxInput, Select, SelectMultiple +import datetime +import re +import time + +__all__ = ( + 'Field', 'CharField', 'IntegerField', + 'DEFAULT_DATE_INPUT_FORMATS', 'DateField', + 'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField', + '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. + + def __init__(self, required=True, widget=None): + self.required = required + widget = widget or self.widget + if isinstance(widget, type): + widget = widget() + self.widget = widget + + def clean(self, value): + """ + Validates the given value and returns its "cleaned" value as an + appropriate Python object. + + Raises ValidationError for any errors. + """ + if self.required and value in EMPTY_VALUES: + raise ValidationError(u'This field is required.') + return value + +class CharField(Field): + def __init__(self, max_length=None, min_length=None, required=True, widget=None): + Field.__init__(self, required, widget) + self.max_length, self.min_length = max_length, min_length + + def clean(self, value): + "Validates max_length and min_length. Returns a Unicode object." + 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) + if self.max_length is not None and len(value) > self.max_length: + raise ValidationError(u'Ensure this value has at most %d characters.' % self.max_length) + if self.min_length is not None and len(value) < self.min_length: + raise ValidationError(u'Ensure this value has at least %d characters.' % self.min_length) + return value + +class IntegerField(Field): + def clean(self, value): + """ + Validates that int() can be called on the input. Returns the result + of int(). + """ + super(IntegerField, self).clean(value) + try: + return int(value) + except (ValueError, TypeError): + raise ValidationError(u'Enter a whole number.') + +DEFAULT_DATE_INPUT_FORMATS = ( + '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06' + '%b %d %Y', '%b %d, %Y', # 'Oct 25 2006', 'Oct 25, 2006' + '%d %b %Y', '%d %b, %Y', # '25 Oct 2006', '25 Oct, 2006' + '%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006' + '%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006' +) + +class DateField(Field): + def __init__(self, input_formats=None, required=True, widget=None): + Field.__init__(self, required, widget) + self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS + + def clean(self, value): + """ + Validates that the input can be converted to a date. Returns a Python + datetime.date object. + """ + Field.clean(self, value) + if value in EMPTY_VALUES: + return None + if isinstance(value, datetime.datetime): + return value.date() + if isinstance(value, datetime.date): + return value + for format in self.input_formats: + try: + return datetime.date(*time.strptime(value, format)[:3]) + except ValueError: + continue + raise ValidationError(u'Enter a valid date.') + +DEFAULT_DATETIME_INPUT_FORMATS = ( + '%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59' + '%Y-%m-%d %H:%M', # '2006-10-25 14:30' + '%Y-%m-%d', # '2006-10-25' + '%m/%d/%Y %H:%M:%S', # '10/25/2006 14:30:59' + '%m/%d/%Y %H:%M', # '10/25/2006 14:30' + '%m/%d/%Y', # '10/25/2006' + '%m/%d/%y %H:%M:%S', # '10/25/06 14:30:59' + '%m/%d/%y %H:%M', # '10/25/06 14:30' + '%m/%d/%y', # '10/25/06' +) + +class DateTimeField(Field): + def __init__(self, input_formats=None, required=True, widget=None): + Field.__init__(self, required, widget) + self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS + + def clean(self, value): + """ + Validates that the input can be converted to a datetime. Returns a + Python datetime.datetime object. + """ + Field.clean(self, value) + if value in EMPTY_VALUES: + return None + if isinstance(value, datetime.datetime): + return value + if isinstance(value, datetime.date): + return datetime.datetime(value.year, value.month, value.day) + for format in self.input_formats: + try: + return datetime.datetime(*time.strptime(value, format)[:6]) + except ValueError: + continue + raise ValidationError(u'Enter a valid date/time.') + +class RegexField(Field): + def __init__(self, regex, error_message=None, required=True, widget=None): + """ + regex can be either a string or a compiled regular expression object. + error_message is an optional error message to use, if + 'Enter a valid value' is too generic for you. + """ + Field.__init__(self, required, widget) + if isinstance(regex, basestring): + regex = re.compile(regex) + self.regex = regex + self.error_message = error_message or u'Enter a valid value.' + + def clean(self, value): + """ + Validates that the input matches the regular expression. Returns a + Unicode object. + """ + 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) + if not self.regex.search(value): + raise ValidationError(self.error_message) + return value + +email_re = re.compile( + r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom + r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string + r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain + +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) + +try: + from django.conf import settings + URL_VALIDATOR_USER_AGENT = settings.URL_VALIDATOR_USER_AGENT +except ImportError: + # It's OK if Django settings aren't configured. + URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)' + +class URLField(RegexField): + def __init__(self, required=True, verify_exists=False, widget=None, + validator_user_agent=URL_VALIDATOR_USER_AGENT): + RegexField.__init__(self, url_re, u'Enter a valid URL.', required, widget) + self.verify_exists = verify_exists + self.user_agent = validator_user_agent + + def clean(self, value): + value = RegexField.clean(self, value) + if self.verify_exists: + import urllib2 + from django.conf import settings + headers = { + "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", + "Accept-Language": "en-us,en;q=0.5", + "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7", + "Connection": "close", + "User-Agent": self.user_agent, + } + try: + req = urllib2.Request(field_data, None, headers) + u = urllib2.urlopen(req) + 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 clean(self, value): + "Returns a Python boolean object." + 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 new file mode 100644 index 0000000000..e490d0d5f9 --- /dev/null +++ b/django/newforms/forms.py @@ -0,0 +1,169 @@ +""" +Form classes +""" + +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): + attrs['fields'] = dict([(name, attrs.pop(name)) for name, obj in attrs.items() if isinstance(obj, Field)]) + return type.__new__(cls, name, bases, attrs) + +class Form(object): + "A collection of Fields, plus their associated data." + __metaclass__ = DeclarativeFieldsMetaclass + + def __init__(self, data=None): # TODO: prefix stuff + self.data = data or {} + 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 __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.full_clean() + return self.clean_data + + def errors(self): + "Returns an ErrorDict for self.data" + if self.__errors is None: + self.full_clean() + return self.__errors + + def is_valid(self): + """ + Returns True if the form has no errors. Otherwise, False. This exists + solely for convenience, so client code can use positive logic rather + than confusing negative logic ("if not form.errors()"). + """ + return not bool(self.errors()) + + 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 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.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 + 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): + self._form = form + self._field = field + self._name = name + + def __str__(self): + "Renders this field as an HTML widget." + # Use the 'widget' attribute on the field to determine which type + # of HTML widget to use. + return self.as_widget(self._field.widget) + + def _errors(self): + """ + Returns an ErrorList for this field. Returns an empty ErrorList + if there are none. + """ + try: + return self._form.errors()[self._name] + except KeyError: + return ErrorList() + errors = property(_errors) + + def as_widget(self, widget, attrs=None): + return widget.render(self._name, self._form.data.get(self._name, None), attrs=attrs) + + def as_text(self, attrs=None): + """ + Returns a string of HTML for representing this as an <input type="text">. + """ + return self.as_widget(TextInput(), attrs) + + def as_textarea(self, attrs=None): + "Returns a string of HTML for representing this as a <textarea>." + return self.as_widget(Textarea(), attrs) diff --git a/django/newforms/util.py b/django/newforms/util.py new file mode 100644 index 0000000000..3887010c85 --- /dev/null +++ b/django/newforms/util.py @@ -0,0 +1,55 @@ +# Default encoding for input byte strings. +DEFAULT_ENCODING = 'utf-8' # TODO: First look at django.conf.settings, then fall back to this. + +def smart_unicode(s): + if not isinstance(s, unicode): + s = unicode(s, DEFAULT_ENCODING) + return s + +class ErrorDict(dict): + """ + A collection of errors that knows how to display itself in various formats. + + The dictionary keys are the field names, and the values are the errors. + """ + def __str__(self): + return self.as_ul() + + def as_ul(self): + if not self: return u'' + return u'<ul class="errorlist">%s</ul>' % ''.join([u'<li>%s%s</li>' % (k, v) for k, v in self.items()]) + + def as_text(self): + return u'\n'.join([u'* %s\n%s' % (k, u'\n'.join([u' * %s' % i for i in v])) for k, v in self.items()]) + +class ErrorList(list): + """ + A collection of errors that knows how to display itself in various formats. + """ + def __str__(self): + return self.as_ul() + + def as_ul(self): + if not self: return u'' + return u'<ul class="errorlist">%s</ul>' % ''.join([u'<li>%s</li>' % e for e in self]) + + def as_text(self): + if not self: return u'' + return u'\n'.join([u'* %s' % e for e in self]) + +class ValidationError(Exception): + def __init__(self, message): + "ValidationError can be passed a string or a list." + if isinstance(message, list): + self.messages = ErrorList([smart_unicode(msg) for msg in message]) + else: + assert isinstance(message, basestring), ("%s should be a basestring" % repr(message)) + message = smart_unicode(message) + self.messages = ErrorList([message]) + + def __str__(self): + # This is needed because, without a __str__(), printing an exception + # instance would result in this: + # AttributeError: ValidationError instance has no attribute 'args' + # See http://www.python.org/doc/current/tut/node10.html#handling + return repr(self.messages) diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py new file mode 100644 index 0000000000..5ec27653cf --- /dev/null +++ b/django/newforms/widgets.py @@ -0,0 +1,113 @@ +""" +HTML Widget classes +""" + +__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 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=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 = '' + final_attrs = dict(self.attrs, name=name) + if attrs: + final_attrs.update(attrs) + return u'<textarea %s>%s</textarea>' % (flatatt(final_attrs), escape(value)) + +class CheckboxInput(Widget): + def render(self, name, value, attrs=None): + final_attrs = dict(self.attrs, type='checkbox', name=name) + if attrs: + 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 |
