Ticket #16630: html5-input-types-take1.patch
File html5-input-types-take1.patch, 7.2 KB (added by , 13 years ago) |
---|
-
django/forms/fields.py
diff --git a/django/forms/fields.py b/django/forms/fields.py index 113a5aa..f1328a7 100644
a b from django.utils.ipv6 import clean_ipv6_address 24 24 from django.core.validators import EMPTY_VALUES 25 25 26 26 from util import ErrorList 27 from widgets import (TextInput, PasswordInput, HiddenInput,28 MultipleHiddenInput, ClearableFileInput, CheckboxInput, Select,27 from widgets import (TextInput, IntegerInput, EmailInput, URLInput, PasswordInput, 28 HiddenInput, MultipleHiddenInput, ClearableFileInput, CheckboxInput, Select, 29 29 NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput, 30 30 SplitDateTimeWidget, SplitHiddenDateTimeWidget, FILE_INPUT_CONTRADICTION) 31 31 … … class CharField(Field): 194 194 return smart_unicode(value) 195 195 196 196 def widget_attrs(self, widget): 197 if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)): 197 attrs = super(CharField, self).widget_attrs(widget) 198 if self.max_length is not None and isinstance(widget, TextInput): 198 199 # The HTML attribute is maxlength, not max_length. 199 return {'maxlength': str(self.max_length)} 200 attrs['maxlength'] = self.max_length 201 return attrs 200 202 201 203 class IntegerField(Field): 204 widget = IntegerInput 202 205 default_error_messages = { 203 206 'invalid': _(u'Enter a whole number.'), 204 207 'max_value': _(u'Ensure this value is less than or equal to %(limit_value)s.'), … … class IntegerField(Field): 230 233 raise ValidationError(self.error_messages['invalid']) 231 234 return value 232 235 236 def widget_attrs(self, widget): 237 attrs = super(IntegerField, self).widget_attrs(widget) 238 if self.min_value: 239 attrs['min'] = self.min_value 240 if self.max_value: 241 attrs['max'] = self.max_value 242 return attrs 243 233 244 class FloatField(IntegerField): 234 245 default_error_messages = { 235 246 'invalid': _(u'Enter a number.'), … … class FloatField(IntegerField): 251 262 raise ValidationError(self.error_messages['invalid']) 252 263 return value 253 264 254 class DecimalField(Field): 265 def widget_attrs(self, widget): 266 attrs = super(FloatField, self).widget_attrs(widget) 267 attrs['step'] = 'any' 268 return attrs 269 270 class DecimalField(IntegerField): 255 271 default_error_messages = { 256 272 'invalid': _(u'Enter a number.'), 257 'max_value': _(u'Ensure this value is less than or equal to %(limit_value)s.'),258 'min_value': _(u'Ensure this value is greater than or equal to %(limit_value)s.'),259 273 'max_digits': _('Ensure that there are no more than %s digits in total.'), 260 274 'max_decimal_places': _('Ensure that there are no more than %s decimal places.'), 261 275 'max_whole_digits': _('Ensure that there are no more than %s digits before the decimal point.') 262 276 } 263 277 264 278 def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs): 265 self.max_value, self.min_value = max_value, min_value266 279 self.max_digits, self.decimal_places = max_digits, decimal_places 267 Field.__init__(self, *args, **kwargs) 268 269 if max_value is not None: 270 self.validators.append(validators.MaxValueValidator(max_value)) 271 if min_value is not None: 272 self.validators.append(validators.MinValueValidator(min_value)) 280 super(DecimalField, self).__init__(max_value, min_value, *args, **kwargs) 273 281 274 282 def to_python(self, value): 275 283 """ … … class DecimalField(Field): 318 326 raise ValidationError(self.error_messages['max_whole_digits'] % (self.max_digits - self.decimal_places)) 319 327 return value 320 328 329 def widget_attrs(self, widget): 330 attrs = super(DecimalField, self).widget_attrs(widget) 331 if self.max_digits: 332 attrs['maxlength'] = self.max_digits + 1 # for the dot 333 if self.decimal_places: 334 attrs['step'] = '0.%s1' % ('0' * (self.decimal_places-1)) 335 return attrs 336 321 337 class BaseTemporalField(Field): 322 338 323 339 def __init__(self, input_formats=None, *args, **kwargs): … … class RegexField(CharField): 445 461 self.validators.append(validators.RegexValidator(regex=regex)) 446 462 447 463 class EmailField(CharField): 464 widget = EmailInput 448 465 default_error_messages = { 449 466 'invalid': _(u'Enter a valid e-mail address.'), 450 467 } … … class ImageField(FileField): 571 588 return f 572 589 573 590 class URLField(CharField): 591 widget = URLInput 574 592 default_error_messages = { 575 593 'invalid': _(u'Enter a valid URL.'), 576 594 'invalid_link': _(u'This URL appears to be a broken link.'), -
django/forms/widgets.py
diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 9b95c31..b9f405b 100644
a b from django.utils.safestring import mark_safe 17 17 from django.utils import datetime_safe, formats 18 18 19 19 __all__ = ( 20 'Media', 'MediaDefiningClass', 'Widget', 'TextInput', 'PasswordInput', 21 'HiddenInput', 'MultipleHiddenInput', 'ClearableFileInput', 22 'FileInput', 'DateInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput', 23 'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect', 24 'CheckboxSelectMultiple', 'MultiWidget', 25 'SplitDateTimeWidget', 20 'Media', 'MediaDefiningClass', 'Widget', 'TextInput', 'EmailInput', 'URLInput', 21 'IntegerInput', 'PasswordInput', 'HiddenInput', 'MultipleHiddenInput', 22 'ClearableFileInput', 'FileInput', 'DateInput', 'DateTimeInput', 'TimeInput', 23 'Textarea', 'CheckboxInput', 'Select', 'NullBooleanSelect', 'SelectMultiple', 24 'RadioSelect', 'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget', 26 25 ) 27 26 28 27 MEDIA_TYPES = ('css','js') … … class Input(Widget): 233 232 class TextInput(Input): 234 233 input_type = 'text' 235 234 236 class PasswordInput(Input): 235 class IntegerInput(TextInput): 236 input_type = 'number' 237 238 class EmailInput(TextInput): 239 input_type = 'email' 240 241 class URLInput(TextInput): 242 input_type = 'url' 243 244 class PasswordInput(TextInput): 237 245 input_type = 'password' 238 246 239 247 def __init__(self, attrs=None, render_value=False): … … class Textarea(Widget): 369 377 return mark_safe(u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), 370 378 conditional_escape(force_unicode(value)))) 371 379 372 class DateInput(Input): 373 input_type = 'text' 374 380 class DateInput(TextInput): 375 381 def __init__(self, attrs=None, format=None): 376 382 super(DateInput, self).__init__(attrs) 377 383 if format: … … class DateInput(Input): 400 406 pass 401 407 return super(DateInput, self)._has_changed(self._format_value(initial), data) 402 408 403 class DateTimeInput(Input): 404 input_type = 'text' 405 409 class DateTimeInput(TextInput): 406 410 def __init__(self, attrs=None, format=None): 407 411 super(DateTimeInput, self).__init__(attrs) 408 412 if format: … … class DateTimeInput(Input): 431 435 pass 432 436 return super(DateTimeInput, self)._has_changed(self._format_value(initial), data) 433 437 434 class TimeInput(Input): 435 input_type = 'text' 436 438 class TimeInput(TextInput): 437 439 def __init__(self, attrs=None, format=None): 438 440 super(TimeInput, self).__init__(attrs) 439 441 if format: