Ticket #6302: pass_initial_through_with_tests_2.diff
| File pass_initial_through_with_tests_2.diff, 41.2 kB (added by brosner, 9 months ago) |
|---|
-
a/django/contrib/localflavor/ar/forms.py
old new 32 32 super(ARPostalCodeField, self).__init__(r'^\d{4}$|^[A-HJ-NP-Za-hj-np-z]\d{4}\D{3}$', 33 33 min_length=4, max_length=8, *args, **kwargs) 34 34 35 def clean(self, value ):36 value = super(ARPostalCodeField, self).clean(value )35 def clean(self, value, initial=None): 36 value = super(ARPostalCodeField, self).clean(value, initial) 37 37 if value in EMPTY_VALUES: 38 38 return u'' 39 39 if len(value) not in (4, 8): … … 55 55 super(ARDNIField, self).__init__(max_length=10, min_length=7, *args, 56 56 **kwargs) 57 57 58 def clean(self, value ):58 def clean(self, value, initial=None): 59 59 """ 60 60 Value can be a string either in the [X]X.XXX.XXX or [X]XXXXXXX formats. 61 61 """ 62 value = super(ARDNIField, self).clean(value )62 value = super(ARDNIField, self).clean(value, initial) 63 63 if value in EMPTY_VALUES: 64 64 return u'' 65 65 if not value.isdigit(): … … 85 85 super(ARCUITField, self).__init__(r'^\d{2}-?\d{8}-?\d$', 86 86 *args, **kwargs) 87 87 88 def clean(self, value ):88 def clean(self, value, initial=None): 89 89 """ 90 90 Value can be either a string in the format XX-XXXXXXXX-X or an 91 91 11-digit number. 92 92 """ 93 value = super(ARCUITField, self).clean(value )93 value = super(ARCUITField, self).clean(value, initial) 94 94 if value in EMPTY_VALUES: 95 95 return u'' 96 96 value, cd = self._canon(value) -
a/django/contrib/localflavor/au/forms.py
old new 26 26 'invalid': u'Phone numbers must contain 10 digits.', 27 27 } 28 28 29 def clean(self, value ):29 def clean(self, value, initial=None): 30 30 """ 31 31 Validate a phone number. Strips parentheses, whitespace and hyphens. 32 32 """ 33 super(AUPhoneNumberField, self).clean(value )33 super(AUPhoneNumberField, self).clean(value, initial) 34 34 if value in EMPTY_VALUES: 35 35 return u'' 36 36 value = re.sub('(\(|\)|\s+|-)', '', smart_unicode(value)) -
a/django/contrib/localflavor/br/forms.py
old new 30 30 'invalid': _('Phone numbers must be in XX-XXXX-XXXX format.'), 31 31 } 32 32 33 def clean(self, value ):34 super(BRPhoneNumberField, self).clean(value )33 def clean(self, value, initial=None): 34 super(BRPhoneNumberField, self).clean(value, initial) 35 35 if value in EMPTY_VALUES: 36 36 return u'' 37 37 value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) … … 65 65 from br_states import STATE_CHOICES 66 66 self.widget.choices = STATE_CHOICES 67 67 68 def clean(self, value ):69 value = super(BRStateChoiceField, self).clean(value )68 def clean(self, value, initial=None): 69 value = super(BRStateChoiceField, self).clean(value, initial) 70 70 if value in EMPTY_VALUES: 71 71 value = u'' 72 72 value = smart_unicode(value) … … 99 99 def __init__(self, *args, **kwargs): 100 100 super(BRCPFField, self).__init__(max_length=14, min_length=11, *args, **kwargs) 101 101 102 def clean(self, value ):102 def clean(self, value, initial=None): 103 103 """ 104 104 Value can be either a string in the format XXX.XXX.XXX-XX or an 105 105 11-digit number. 106 106 """ 107 value = super(BRCPFField, self).clean(value )107 value = super(BRCPFField, self).clean(value, initial) 108 108 if value in EMPTY_VALUES: 109 109 return u'' 110 110 orig_value = value[:] … … 136 136 'max_digits': _("This field requires at least 14 digits"), 137 137 } 138 138 139 def clean(self, value ):139 def clean(self, value, initial=None): 140 140 """ 141 141 Value can be either a string in the format XX.XXX.XXX/XXXX-XX or a 142 142 group of 14 characters. 143 143 """ 144 value = super(BRCNPJField, self).clean(value )144 value = super(BRCNPJField, self).clean(value, initial) 145 145 if value in EMPTY_VALUES: 146 146 return u'' 147 147 orig_value = value[:] -
a/django/contrib/localflavor/ca/forms.py
old new 27 27 'invalid': u'Phone numbers must be in XXX-XXX-XXXX format.', 28 28 } 29 29 30 def clean(self, value ):30 def clean(self, value, initial=None): 31 31 """Validate a phone number. 32 32 """ 33 super(CAPhoneNumberField, self).clean(value )33 super(CAPhoneNumberField, self).clean(value, initial) 34 34 if value in EMPTY_VALUES: 35 35 return u'' 36 36 value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) … … 49 49 'invalid': u'Enter a Canadian province or territory.', 50 50 } 51 51 52 def clean(self, value ):52 def clean(self, value, initial=None): 53 53 from ca_provinces import PROVINCES_NORMALIZED 54 super(CAProvinceField, self).clean(value )54 super(CAProvinceField, self).clean(value, initial) 55 55 if value in EMPTY_VALUES: 56 56 return u'' 57 57 try: … … 88 88 'invalid': ugettext('Enter a valid Canadian Social Insurance number in XXX-XXX-XXX format.'), 89 89 } 90 90 91 def clean(self, value ):92 super(CASocialInsuranceNumberField, self).clean(value )91 def clean(self, value, initial=None): 92 super(CASocialInsuranceNumberField, self).clean(value, initial) 93 93 if value in EMPTY_VALUES: 94 94 return u'' 95 95 -
a/django/contrib/localflavor/ch/forms.py
old new 31 31 'invalid': 'Phone numbers must be in 0XX XXX XX XX format.', 32 32 } 33 33 34 def clean(self, value ):35 super(CHPhoneNumberField, self).clean(value )34 def clean(self, value, initial=None): 35 super(CHPhoneNumberField, self).clean(value, initial) 36 36 if value in EMPTY_VALUES: 37 37 return u'' 38 38 value = re.sub('(\.|\s|/|-)', '', smart_unicode(value)) … … 95 95 96 96 return str(calculated_checksum)[-1] == given_checksum 97 97 98 def clean(self, value ):99 super(CHIdentityCardNumberField, self).clean(value )98 def clean(self, value, initial=None): 99 super(CHIdentityCardNumberField, self).clean(value, initial) 100 100 if value in EMPTY_VALUES: 101 101 return u'' 102 102 -
a/django/contrib/localflavor/cl/forms.py
old new 41 41 # the real world. 42 42 super(CLRutField, self).__init__(r'^[\d\.]{1,11}-?[\dkK]$', *args, **kwargs) 43 43 44 def clean(self, value ):44 def clean(self, value, initial=None): 45 45 """ 46 46 Check and clean the Chilean RUT. 47 47 """ 48 super(CLRutField, self).clean(value )48 super(CLRutField, self).clean(value, initial) 49 49 if value in EMPTY_VALUES: 50 50 return u'' 51 51 rut, verificador = self._canonify(value) -
a/django/contrib/localflavor/de/forms.py
old new 60 60 61 61 return str(calculated_checksum)[-1] == given_checksum 62 62 63 def clean(self, value ):64 super(DEIdentityCardNumberField, self).clean(value )63 def clean(self, value, initial=None): 64 super(DEIdentityCardNumberField, self).clean(value, initial) 65 65 if value in EMPTY_VALUES: 66 66 return u'' 67 67 match = re.match(id_re, value) -
a/django/contrib/localflavor/es/forms.py
old new 81 81 error_message=self.default_error_messages['invalid%s' % (self.only_nif and '_only_nif' or '')], 82 82 *args, **kwargs) 83 83 84 def clean(self, value ):85 super(ESIdentityCardNumberField, self).clean(value )84 def clean(self, value, initial=None): 85 super(ESIdentityCardNumberField, self).clean(value, initial) 86 86 if value in EMPTY_VALUES: 87 87 return u'' 88 88 nif_get_checksum = lambda d: self.nif_control[int(d)%23] … … 147 147 super(ESCCCField, self).__init__(r'^\d{4}[ -]?\d{4}[ -]?\d{2}[ -]?\d{10}$', 148 148 max_length=None, min_length=None, *args, **kwargs) 149 149 150 def clean(self, value ):151 super(ESCCCField, self).clean(value )150 def clean(self, value, initial=None): 151 super(ESCCCField, self).clean(value, initial) 152 152 if value in EMPTY_VALUES: 153 153 return u'' 154 154 control_str = [1, 2, 4, 8, 5, 10, 9, 7, 3, 6] -
a/django/contrib/localflavor/fi/forms.py
old new 28 28 'invalid': ugettext('Enter a valid Finnish social security number.'), 29 29 } 30 30 31 def clean(self, value ):32 super(FISocialSecurityNumber, self).clean(value )31 def clean(self, value, initial=None): 32 super(FISocialSecurityNumber, self).clean(value, initial) 33 33 if value in EMPTY_VALUES: 34 34 return u'' 35 35 -
a/django/contrib/localflavor/fr/forms.py
old new 30 30 'invalid': u'Phone numbers must be in 0X XX XX XX XX format.', 31 31 } 32 32 33 def clean(self, value ):34 super(FRPhoneNumberField, self).clean(value )33 def clean(self, value, initial=None): 34 super(FRPhoneNumberField, self).clean(value, initial) 35 35 if value in EMPTY_VALUES: 36 36 return u'' 37 37 value = re.sub('(\.|\s)', '', smart_unicode(value)) -
a/django/contrib/localflavor/in_/forms.py
old new 28 28 'invalid': u'Enter a Indian state or territory.', 29 29 } 30 30 31 def clean(self, value ):31 def clean(self, value, initial=None): 32 32 from in_states import STATES_NORMALIZED 33 super(INStateField, self).clean(value )33 super(INStateField, self).clean(value, initial) 34 34 if value in EMPTY_VALUES: 35 35 return u'' 36 36 try: -
a/django/contrib/localflavor/is_/forms.py
old new 22 22 kwargs['min_length'],kwargs['max_length'] = 10,11 23 23 super(ISIdNumberField, self).__init__(r'^\d{6}(-| )?\d{4}$', *args, **kwargs) 24 24 25 def clean(self, value ):26 value = super(ISIdNumberField, self).clean(value )25 def clean(self, value, initial=None): 26 value = super(ISIdNumberField, self).clean(value, initial) 27 27 28 28 if value in EMPTY_VALUES: 29 29 return u'' … … 64 64 kwargs['min_length'], kwargs['max_length'] = 7,8 65 65 super(ISPhoneNumberField, self).__init__(r'^\d{3}(-| )?\d{4}$', *args, **kwargs) 66 66 67 def clean(self, value ):68 value = super(ISPhoneNumberField, self).clean(value )67 def clean(self, value, initial=None): 68 value = super(ISPhoneNumberField, self).clean(value, initial) 69 69 70 70 if value in EMPTY_VALUES: 71 71 return u'' -
a/django/contrib/localflavor/it/forms.py
old new 47 47 super(ITSocialSecurityNumberField, self).__init__(r'^\w{3}\s*\w{3}\s*\w{5}\s*\w{5}$', 48 48 max_length=None, min_length=None, *args, **kwargs) 49 49 50 def clean(self, value ):51 value = super(ITSocialSecurityNumberField, self).clean(value )50 def clean(self, value, initial=None): 51 value = super(ITSocialSecurityNumberField, self).clean(value, initial) 52 52 if value == u'': 53 53 return value 54 54 value = re.sub('\s', u'', value).upper() … … 68 68 'invalid': ugettext(u'Enter a valid VAT number.'), 69 69 } 70 70 71 def clean(self, value ):72 value = super(ITVatNumberField, self).clean(value )71 def clean(self, value, initial=None): 72 value = super(ITVatNumberField, self).clean(value, initial) 73 73 if value == u'': 74 74 return value 75 75 try: -
a/django/contrib/localflavor/jp/forms.py
old new 23 23 super(JPPostalCodeField, self).__init__(r'^\d{3}-\d{4}$|^\d{7}$', 24 24 max_length=None, min_length=None, *args, **kwargs) 25 25 26 def clean(self, value ):26 def clean(self, value, initial=None): 27 27 """ 28 28 Validates the input and returns a string that contains only numbers. 29 29 Returns an empty string for empty values. 30 30 """ 31 v = super(JPPostalCodeField, self).clean(value )31 v = super(JPPostalCodeField, self).clean(value, initial) 32 32 return v.replace('-', '') 33 33 34 34 class JPPrefectureSelect(Select): -
a/django/contrib/localflavor/nl/forms.py
old new 21 21 'invalid': _('Enter a valid postal code'), 22 22 } 23 23 24 def clean(self, value ):25 super(NLZipCodeField, self).clean(value )24 def clean(self, value, initial=None): 25 super(NLZipCodeField, self).clean(value, initial) 26 26 if value in EMPTY_VALUES: 27 27 return u'' 28 28 … … 52 52 'invalid': _('Enter a valid phone number'), 53 53 } 54 54 55 def clean(self, value ):56 super(NLPhoneNumberField, self).clean(value )55 def clean(self, value, initial=None): 56 super(NLPhoneNumberField, self).clean(value, initial) 57 57 if value in EMPTY_VALUES: 58 58 return u'' 59 59 … … 78 78 'invalid': _('Enter a valid SoFi number'), 79 79 } 80 80 81 def clean(self, value ):82 super(NLSoFiNumberField, self).clean(value )81 def clean(self, value, initial=None): 82 super(NLSoFiNumberField, self).clean(value, initial) 83 83 if value in EMPTY_VALUES: 84 84 return u'' 85 85 -
a/django/contrib/localflavor/no/forms.py
old new 33 33 'invalid': ugettext(u'Enter a valid Norwegian social security number.'), 34 34 } 35 35 36 def clean(self, value ):37 super(NOSocialSecurityNumber, self).clean(value )36 def clean(self, value, initial=None): 37 super(NOSocialSecurityNumber, self).clean(value, initial) 38 38 if value in EMPTY_VALUES: 39 39 return u'' 40 40 -
a/django/contrib/localflavor/pe/forms.py
old new 28 28 super(PEDNIField, self).__init__(max_length=8, min_length=8, *args, 29 29 **kwargs) 30 30 31 def clean(self, value ):31 def clean(self, value, initial=None): 32 32 """ 33 33 Value must be a string in the XXXXXXXX formats. 34 34 """ 35 value = super(PEDNIField, self).clean(value )35 value = super(PEDNIField, self).clean(value, initial) 36 36 if value in EMPTY_VALUES: 37 37 return u'' 38 38 if not value.isdigit(): … … 56 56 super(PERUCField, self).__init__(max_length=11, min_length=11, *args, 57 57 **kwargs) 58 58 59 def clean(self, value ):59 def clean(self, value, initial=None): 60 60 """ 61 61 Value must be an 11-digit number. 62 62 """ 63 value = super(PERUCField, self).clean(value )63 value = super(PERUCField, self).clean(value, initial) 64 64 if value in EMPTY_VALUES: 65 65 return u'' 66 66 if not value.isdigit(): -
a/django/contrib/localflavor/pl/forms.py
old new 44 44 super(PLNationalIdentificationNumberField, self).__init__(r'^\d{11}$', 45 45 max_length=None, min_length=None, *args, **kwargs) 46 46 47 def clean(self, value):48 super(PLNationalIdentificationNumberField, self).clean(value )47 def clean(self, value, initial=None): 48 super(PLNationalIdentificationNumberField, self).clean(value, initial) 49 49 if not self.has_valid_checksum(value): 50 50 raise ValidationError(self.error_messages['checksum']) 51 51 return u'%s' % value … … 77 77 super(PLTaxNumberField, self).__init__(r'^\d{3}-\d{3}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{3}-\d{3}$', 78 78 max_length=None, min_length=None, *args, **kwargs) 79 79 80 def clean(self, value):81 super(PLTaxNumberField, self).clean(value )80 def clean(self, value, initial=None): 81 super(PLTaxNumberField, self).clean(value, initial) 82 82 value = re.sub("[-]", "", value) 83 83 if not self.has_valid_checksum(value): 84 84 raise ValidationError(self.error_messages['checksum']) … … 117 117 super(PLNationalBusinessRegisterField, self).__init__(r'^\d{7,9}$', 118 118 max_length=None, min_length=None, *args, **kwargs) 119 119 120 def clean(self, value):121 super(PLNationalBusinessRegisterField, self).clean(value )120 def clean(self, value, initial=None): 121 super(PLNationalBusinessRegisterField, self).clean(value, initial) 122 122 if not self.has_valid_checksum(value): 123 123 raise ValidationError(self.error_messages['checksum']) 124 124 return u'%s' % value -
a/django/contrib/localflavor/sk/forms.py
old new 34 34 super(SKPostalCodeField, self).__init__(r'^\d{5}$|^\d{3} \d{2}$', 35 35 max_length=None, min_length=None, *args, **kwargs) 36 36 37 def clean(self, value ):37 def clean(self, value, initial=None): 38 38 """ 39 39 Validates the input and returns a string that contains only numbers. 40 40 Returns an empty string for empty values. 41 41 """ 42 v = super(SKPostalCodeField, self).clean(value )42 v = super(SKPostalCodeField, self).clean(value, initial) 43 43 return v.replace(' ', '') -
a/django/contrib/localflavor/uk/forms.py
old new 25 25 postcode_regex = re.compile(r'^(GIR 0AA|%s %s)$' % (outcode_pattern, incode_pattern)) 26 26 space_regex = re.compile(r' *(%s)$' % incode_pattern) 27 27 28 def clean(self, value ):29 value = super(UKPostcodeField, self).clean(value )28 def clean(self, value, initial=None): 29 value = super(UKPostcodeField, self).clean(value, initial) 30 30 if value == u'': 31 31 return value 32 32 postcode = value.upper().strip() -
a/django/contrib/localflavor/us/forms.py
old new 25 25 'invalid': u'Phone numbers must be in XXX-XXX-XXXX format.', 26 26 } 27 27 28 def clean(self, value ):29 super(USPhoneNumberField, self).clean(value )28 def clean(self, value, initial=None): 29 super(USPhoneNumberField, self).clean(value, initial) 30 30 if value in EMPTY_VALUES: 31 31 return u'' 32 32 value = re.sub('(\(|\)|\s+)', '', smart_unicode(value)) … … 54 54 'invalid': ugettext('Enter a valid U.S. Social Security number in XXX-XX-XXXX format.'), 55 55 } 56 56 57 def clean(self, value ):58 super(USSocialSecurityNumberField, self).clean(value )57 def clean(self, value, initial=None): 58 super(USSocialSecurityNumberField, self).clean(value, initial) 59 59 if value in EMPTY_VALUES: 60 60 return u'' 61 61 match = re.match(ssn_re, value) … … 87 87 'invalid': u'Enter a U.S. state or territory.', 88 88 } 89 89 90 def clean(self, value ):90 def clean(self, value, initial=None): 91 91 from us_states import STATES_NORMALIZED 92 super(USStateField, self).clean(value )92 super(USStateField, self).clean(value, initial) 93 93 if value in EMPTY_VALUES: 94 94 return u'' 95 95 try: -
a/django/contrib/localflavor/za/forms.py
old new 20 20 'invalid': _(u'Enter a valid South African ID number'), 21 21 } 22 22 23 def clean(self, value ):23 def clean(self, value, initial=None): 24 24 # strip spaces and dashes 25 25 value = value.strip().replace(' ', '').replace('-', '') 26 26 27 super(ZAIDField, self).clean(value )27 super(ZAIDField, self).clean(value, initial) 28 28 29 29 if value in EMPTY_VALUES: 30 30 return u'' -
a/django/db/models/fields/__init__.py
old new 797 797 return os.path.normpath(f) 798 798 799 799 def save_form_data(self, instance, data): 800 if data: 800 from django.newforms.fields import UploadedFile 801 if data and isinstance(data, UploadedFile): 801 802 getattr(instance, "save_%s_file" % self.name)(data.filename, data.content, save=False) 802 803 803 804 def formfield(self, **kwargs): 804 805 defaults = {'form_class': forms.FileField} 805 # If a file has been provided previously, then the form doesn't require806 # that a new file is provided this time.807 if 'initial' in kwargs:808 defaults['required'] = False809 806 defaults.update(kwargs) 810 807 return super(FileField, self).formfield(**defaults) 811 808 -
a/django/newforms/fields.py
old new 93 93 messages.update(error_messages or {}) 94 94 self.error_messages = messages 95 95 96 def clean(self, value ):96 def clean(self, value, initial=None): 97 97 """ 98 98 Validates the given value and returns its "cleaned" value as an 99 99 appropriate Python object. … … 128 128 self.max_length, self.min_length = max_length, min_length 129 129 super(CharField, self).__init__(*args, **kwargs) 130 130 131 def clean(self, value ):131 def clean(self, value, initial=None): 132 132 "Validates max_length and min_length. Returns a Unicode object." 133 super(CharField, self).clean(value )133 super(CharField, self).clean(value, initial) 134 134 if value in EMPTY_VALUES: 135 135 return u'' 136 136 value = smart_unicode(value) … … 157 157 self.max_value, self.min_value = max_value, min_value 158 158 super(IntegerField, self).__init__(*args, **kwargs) 159 159 160 def clean(self, value ):160 def clean(self, value, initial=None): 161 161 """ 162 162 Validates that int() can be called on the input. Returns the result 163 163 of int(). Returns None for empty values. 164 164 """ 165 super(IntegerField, self).clean(value )165 super(IntegerField, self).clean(value, initial) 166 166 if value in EMPTY_VALUES: 167 167 return None 168 168 try: … … 186 186 self.max_value, self.min_value = max_value, min_value 187 187 Field.__init__(self, *args, **kwargs) 188 188 189 def clean(self, value ):189 def clean(self, value, initial=None): 190 190 """ 191 191 Validates that float() can be called on the input. Returns a float. 192 192 Returns None for empty values. 193 193 """ 194 super(FloatField, self).clean(value )194 super(FloatField, self).clean(value, initial) 195 195 if not self.required and value in EMPTY_VALUES: 196 196 return None 197 197 try: … … 219 219 self.max_digits, self.decimal_places = max_digits, decimal_places 220 220 Field.__init__(self, *args, **kwargs) 221 221 222 def clean(self, value ):222 def clean(self, value, initial=None): 223 223 """ 224 224 Validates that the input is a decimal number. Returns a Decimal 225 225 instance. Returns None for empty values. Ensures that there are no more 226 226 than max_digits in the number, and no more than decimal_places digits 227 227 after the decimal point. 228 228 """ 229 super(DecimalField, self).clean(value )229 super(DecimalField, self).clean(value, initial) 230 230 if not self.required and value in EMPTY_VALUES: 231 231 return None 232 232 value = smart_str(value).strip() … … 266 266 super(DateField, self).__init__(*args, **kwargs) 267 267 self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS 268 268 269 def clean(self, value ):269 def clean(self, value, initial=None): 270 270 """ 271 271 Validates that the input can be converted to a date. Returns a Python 272 272 datetime.date object. 273 273 """ 274 super(DateField, self).clean(value )274 super(DateField, self).clean(value, initial) 275 275 if value in EMPTY_VALUES: 276 276 return None 277 277 if isinstance(value, datetime.datetime): … … 299 299 super(TimeField, self).__init__(*args, **kwargs) 300 300 self.input_formats = input_formats or DEFAULT_TIME_INPUT_FORMATS 301 301 302 def clean(self, value ):302 def clean(self, value, initial=None): 303 303 """ 304 304 Validates that the input can be converted to a time. Returns a Python 305 305 datetime.time object. 306 306 """ 307 super(TimeField, self).clean(value )307 super(TimeField, self).clean(value, initial) 308 308 if value in EMPTY_VALUES: 309 309 return None 310 310 if isinstance(value, datetime.time): … … 338 338 super(DateTimeField, self).__init__(*args, **kwargs) 339 339 self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS 340 340 341 def clean(self, value ):341 def clean(self, value, initial=None): 342 342 """ 343 343 Validates that the input can be converted to a datetime. Returns a 344 344 Python datetime.datetime object. 345 345 """ 346 super(DateTimeField, self).clean(value )346 super(DateTimeField, self).clean(value, initial) 347 347 if value in EMPTY_VALUES: 348 348 return None 349 349 if isinstance(value, datetime.datetime): … … 380 380 regex = re.compile(regex) 381 381 self.regex = regex 382 382 383 def clean(self, value ):383 def clean(self, value, initial=None): 384 384 """ 385 385 Validates that the input matches the regular expression. Returns a 386 386 Unicode object. 387 387 """ 388 value = super(RegexField, self).clean(value )388 value = super(RegexField, self).clean(value, initial) 389 389 if value == u'': 390 390 return value 391 391 if not self.regex.search(value): … … 437 437 def __init__(self, *args, **kwargs): 438 438 super(FileField, self).__init__(*args, **kwargs) 439 439 440 def clean(self, data ):441 super(FileField, self).clean( data)440 def clean(self, data, initial=None): 441 super(FileField, self).clean(initial or data) 442 442 if not self.required and data in EMPTY_VALUES: 443 443 return None 444 elif not data and initial: 445 return initial 444 446 try: 445 447 f = UploadedFile(data['filename'], data['content']) 446 448 except TypeError: … … 456 458 'invalid_image': _(u"Upload a valid image. The file you uploaded was either not an image or a corrupted image."), 457 459 } 458 460 459 def clean(self, data ):461 def clean(self, data, initial): 460 462 """ 461 463 Checks that the file-upload field data contains a valid image (GIF, JPG, 462 464 PNG, possibly others -- whatever the Python Imaging Library supports). 463 465 """ 464 f = super(ImageField, self).clean(data )466 f = super(ImageField, self).clean(data, initial) 465 467 if f is None: 466 468 return None 467 469 from PIL import Image … … 500 502 self.verify_exists = verify_exists 501 503 self.user_agent = validator_user_agent 502 504 503 def clean(self, value ):505 def clean(self, value, initial=None): 504 506 # If no URL scheme given, assume http:// 505 507 if value and '://' not in value: 506 508 value = u'http://%s' % value 507 value = super(URLField, self).clean(value )509 value = super(URLField, self).clean(value, initial) 508 510 if value == u'': 509 511 return value 510 512 if self.verify_exists: … … 529 531 class BooleanField(Field): 530 532 widget = CheckboxInput 531 533 532 def clean(self, value ):534 def clean(self, value, initial=None): 533 535 """Returns a Python boolean object.""" 534 super(BooleanField, self).clean(value )536 super(BooleanField, self).clean(value, initial) 535 537 # Explicitly check for the string 'False', which is what a hidden field 536 538 # will submit for False. Because bool("True") == True, we don't need to 537 539 # handle that explicitly. … … 546 548 """ 547 549 widget = NullBooleanSelect 548 550 549 def clean(self, value ):551 def clean(self, value, initial=None): 550 552 return {True: True, False: False}.get(value, None) 551 553 552 554 class ChoiceField(Field): … … 572 574 573 575 choices = property(_get_choices, _set_choices) 574 576 575 def clean(self, value ):577 def clean(self, value, initial=None): 576 578 """ 577 579 Validates that the input is in self.choices. 578 580 """ 579 value = super(ChoiceField, self).clean(value )581 value = super(ChoiceField, self).clean(value, initial) 580 582 if value in EMPTY_VALUES: 581 583 value = u'' 582 584 value = smart_unicode(value) … … 595 597 'invalid_list': _(u'Enter a list of values.'), 596 598 } 597 599 598 def clean(self, value ):600 def clean(self, value, initial=None): 599 601 """ 600 602 Validates that the input is a list or tuple. 601 603 """ … … 626 628 f.required = False 627 629 self.fields = fields 628 630 629 def clean(self, value ):631 def clean(self, value, initial=None): 630 632 """ 631 633 Validates the given value against all of self.fields, which is a 632 634 list of Field instances. 633 635 """ 634 super(ComboField, self).clean(value )636 super(ComboField, self).clean(value, initial) 635 637 for field in self.fields: 636 value = field.clean(value )638 value = field.clean(value, initial) 637 639 return value 638 640 639 641 class MultiValueField(Field): … … 666 668 f.required = False 667 669 self.fields = fields 668 670 669 def clean(self, value ):671 def clean(self, value, initial=None): 670 672 """ 671 673 Validates every value in the given list. A value is validated against 672 674 the corresponding Field in self.fields. -
a/django/newforms/forms.py
old new 181 181 # Each widget type knows how to retrieve its own data, because some 182 182 # widgets split data over several HTML fields. 183 183 value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) 184 initial = field.widget.value_from_initialdict(self.initial, name) 184 185 try: 185 value = field.clean(value )186 value = field.clean(value, initial) 186 187 self.cleaned_data[name] = value 187 188 if hasattr(self, 'clean_%s' % name): 188 189 value = getattr(self, 'clean_%s' % name)() -
a/django/newforms/models.py
old new 360 360 361 361 choices = property(_get_choices, _set_choices) 362 362 363 def clean(self, value ):363 def clean(self, value, initial=None): 364 364 Field.clean(self, value) 365 365 if value in EMPTY_VALUES: 366 366 return None … … 386 386 cache_choices, required, widget, label, initial, help_text, 387 387 *args, **kwargs) 388 388 389 def clean(self, value ):389 def clean(self, value, initial=None): 390 390 if self.required and not value: 391 391 raise ValidationError(self.error_messages['required']) 392 392 elif not self.required and not value: -
a/django/newforms/widgets.py
old new 77 77 return id_ 78 78 id_for_label = classmethod(id_for_label) 79 79 80 def value_from_initialdict(self, initial, name): 81 """ 82 Given a dictionary of initial data return the value for this widget. 83 Returns None if it's not provided. 84 """ 85 if name in initial: 86 return initial[name] 87 return None 88 80 89 class Input(Widget): 81 90 """ 82 91 Base class for all <input> widgets (except type='checkbox' and -
a/docs/newforms.txt
old new 1594 1594 from django import newforms as forms 1595 1595 1596 1596 class MultiEmailField(forms.Field): 1597 def clean(self, value ):1597 def clean(self, value, initial=None): 1598 1598 if not value: 1599 1599 raise forms.ValidationError('Enter at least one e-mail address.') 1600 1600 emails = value.split(',') -
a/tests/modeltests/model_forms/models.py
old new 7 7 most of these tests should be rewritten. 8 8 """ 9 9 10 import os 11 import tempfile 12 10 13 from django.db import models 11 14 12 15 ARTICLE_STATUS = ( … … 55 58 def __unicode__(self): 56 59 return self.phone 57 60 61 class TextFile(models.Model): 62 description = models.CharField(max_length=20) 63 file = models.FileField(upload_to=tempfile.gettempdir()) 64 65 def __unicode__(self): 66 return self.description 67 58 68 __test__ = {'API_TESTS': """ 59 69 >>> from django import newforms as forms 60 70 >>> from django.newforms.models import ModelForm … … 701 711 True 702 712 >>> f.cleaned_data 703 713 {'phone': u'312-555-1212', 'description': u'Assistance'} 714 715 # FileField ################################################################### 716 717 >>> class TextFileForm(ModelForm): 718 ... class Meta: 719 ... model = TextFile 720 721 Test conditions when files is either not given or empty. 722 723 >>> f = TextFileForm(data={'description': u'Assistance'}) 724 >>> f.is_valid() 725 False 726 >>> f = TextFileForm(data={'description': u'Assistance'}, files={}) 727 >>> f.is_valid() 728 False 729 730 Upload a file and ensure it all works as expected. 731 732 >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test1.txt', 'content': 'hello world'}}) 733 >>> f.is_valid() 734 True 735 >>> type(f.cleaned_data['file']) 736 <class 'django.newforms.fields.UploadedFile'> 737 >>> instance = f.save() 738 >>> instance.file 739 u'.../test1.txt' 740 741 Edit an instance that already has the file defined in the model. This will not 742 save the file again, but leave it exactly as it is. 743 744 >>> f = TextFileForm(data={'description': u'Assistance'}, instance=instance) 745 >>> f.is_valid() 746 True 747 >>> f.cleaned_data['file'] 748 u'.../test1.txt' 749 >>> instance = f.save() 750 >>> instance.file 751 u'.../test1.txt' 752 753 Delete the current file since this is not done by Django. 754 755 >>> os.unlink(instance.get_file_filename()) 756 757 Override the file by uploading a new one. 758 759 >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test2.txt', 'content': 'hello world'}}, instance=instance) 760 >>> f.is_valid() 761 True 762 >>> instance = f.save() 763 >>> instance.file 764 u'.../test2.txt' 765 766 >>> instance.delete() 767 768 Test the non-required FileField 769 770 >>> f = TextFileForm(data={'description': u'Assistance'}) 771 >>> f.fields['file'].required = False 772 >>> f.is_valid() 773 True 774 >>> instance = f.save() 775 >>> instance.file 776 '' 777 778 >>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test3.txt', 'content': 'hello world'}}, instance=instance) 779 >>> f.is_valid() 780 True 781 >>> instance = f.save() 782 >>> instance.file 783 u'.../test3.txt'
