Changeset 901
- Timestamp:
- 10/17/05 08:16:13 (3 years ago)
- Files:
-
- django/branches/i18n/django/core/validators.py (modified) (27 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/i18n/django/core/validators.py
r726 r901 25 25 26 26 from django.conf.settings import JING_PATH 27 from django.utils.translation import gettext_lazy 27 28 28 29 class ValidationError(Exception): … … 58 59 def isAlphaNumericURL(field_data, all_data): 59 60 if not alnumurl_re.search(field_data): 60 raise ValidationError, "This value must contain only letters, numbers, underscores and slashes."61 raise ValidationError, _("This value must contain only letters, numbers, underscores and slashes.") 61 62 62 63 def isLowerCase(field_data, all_data): 63 64 if field_data.lower() != field_data: 64 raise ValidationError, "Uppercase letters are not allowed here."65 raise ValidationError, _("Uppercase letters are not allowed here.") 65 66 66 67 def isUpperCase(field_data, all_data): 67 68 if field_data.upper() != field_data: 68 raise ValidationError, "Lowercase letters are not allowed here."69 raise ValidationError, _("Lowercase letters are not allowed here.") 69 70 70 71 def isCommaSeparatedIntegerList(field_data, all_data): … … 73 74 int(supposed_int) 74 75 except ValueError: 75 raise ValidationError, "Enter only digits separated by commas."76 raise ValidationError, _("Enter only digits separated by commas.") 76 77 77 78 def isCommaSeparatedEmailList(field_data, all_data): … … 85 86 isValidEmail(supposed_email.strip(), '') 86 87 except ValidationError: 87 raise ValidationError, "Enter valid e-mail addresses separated by commas."88 raise ValidationError, _("Enter valid e-mail addresses separated by commas.") 88 89 89 90 def isValidIPAddress4(field_data, all_data): … … 92 93 if len(valid_parts) == 4: 93 94 return 94 raise validators.ValidationError, "Please enter a valid IP address."95 raise validators.ValidationError, _("Please enter a valid IP address.") 95 96 96 97 def isNotEmpty(field_data, all_data): 97 98 if field_data.strip() == '': 98 raise ValidationError, "Empty values are not allowed here."99 raise ValidationError, _("Empty values are not allowed here.") 99 100 100 101 def isOnlyDigits(field_data, all_data): 101 102 if not field_data.isdigit(): 102 raise ValidationError, "Non-numeric characters aren't allowed here."103 raise ValidationError, _("Non-numeric characters aren't allowed here.") 103 104 104 105 def isNotOnlyDigits(field_data, all_data): 105 106 if field_data.isdigit(): 106 raise ValidationError, "This value can't be comprised solely of digits."107 raise ValidationError, _("This value can't be comprised solely of digits.") 107 108 108 109 def isInteger(field_data, all_data): 109 110 # This differs from isOnlyDigits because this accepts the negative sign 110 111 if not integer_re.search(field_data): 111 raise ValidationError, "Enter a whole number."112 raise ValidationError, _("Enter a whole number.") 112 113 113 114 def isOnlyLetters(field_data, all_data): 114 115 if not field_data.isalpha(): 115 raise ValidationError, "Only alphabetical characters are allowed here."116 raise ValidationError, _("Only alphabetical characters are allowed here.") 116 117 117 118 def isValidANSIDate(field_data, all_data): 118 119 if not ansi_date_re.search(field_data): 119 raise ValidationError, 'Enter a valid date in YYYY-MM-DD format.'120 raise ValidationError, _('Enter a valid date in YYYY-MM-DD format.') 120 121 121 122 def isValidANSITime(field_data, all_data): 122 123 if not ansi_time_re.search(field_data): 123 raise ValidationError, 'Enter a valid time in HH:MM format.'124 raise ValidationError, _('Enter a valid time in HH:MM format.') 124 125 125 126 def isValidANSIDatetime(field_data, all_data): 126 127 if not ansi_datetime_re.search(field_data): 127 raise ValidationError, 'Enter a valid date/time in YYYY-MM-DD HH:MM format.'128 raise ValidationError, _('Enter a valid date/time in YYYY-MM-DD HH:MM format.') 128 129 129 130 def isValidEmail(field_data, all_data): 130 131 if not email_re.search(field_data): 131 raise ValidationError, 'Enter a valid e-mail address.'132 raise ValidationError, _('Enter a valid e-mail address.') 132 133 133 134 def isValidImage(field_data, all_data): … … 141 142 Image.open(StringIO(field_data['content'])) 142 143 except IOError: # Python Imaging Library doesn't recognize it as an image 143 raise ValidationError, "Upload a valid image. The file you uploaded was either not an image or a corrupted image."144 raise ValidationError, _("Upload a valid image. The file you uploaded was either not an image or a corrupted image.") 144 145 145 146 def isValidImageURL(field_data, all_data): … … 148 149 uc(field_data, all_data) 149 150 except URLMimeTypeCheck.InvalidContentType: 150 raise ValidationError, "The URL %s does not point to a valid image."% field_data151 raise ValidationError, _("The URL %s does not point to a valid image.") % field_data 151 152 152 153 def isValidPhone(field_data, all_data): 153 154 if not phone_re.search(field_data): 154 raise ValidationError, 'Phone numbers must be in XXX-XXX-XXXX format. "%s" is invalid.'% field_data155 raise ValidationError, _('Phone numbers must be in XXX-XXX-XXXX format. "%s" is invalid.') % field_data 155 156 156 157 def isValidQuicktimeVideoURL(field_data, all_data): … … 160 161 uc(field_data, all_data) 161 162 except URLMimeTypeCheck.InvalidContentType: 162 raise ValidationError, "The URL %s does not point to a valid QuickTime video."% field_data163 raise ValidationError, _("The URL %s does not point to a valid QuickTime video.") % field_data 163 164 164 165 def isValidURL(field_data, all_data): 165 166 if not url_re.search(field_data): 166 raise ValidationError, "A valid URL is required."167 raise ValidationError, _("A valid URL is required.") 167 168 168 169 def isValidHTML(field_data, all_data): … … 178 179 from xml.dom.minidom import parseString 179 180 error_messages = [e.firstChild.wholeText for e in parseString(u.read()).getElementsByTagName('messages')[0].getElementsByTagName('msg')] 180 raise ValidationError, "Valid HTML is required. Specific errors are:\n%s"% "\n".join(error_messages)181 raise ValidationError, _("Valid HTML is required. Specific errors are:\n%s") % "\n".join(error_messages) 181 182 182 183 def isWellFormedXml(field_data, all_data): … … 185 186 parseString(field_data) 186 187 except Exception, e: # Naked except because we're not sure what will be thrown 187 raise ValidationError, "Badly formed XML: %s"% str(e)188 raise ValidationError, _("Badly formed XML: %s") % str(e) 188 189 189 190 def isWellFormedXmlFragment(field_data, all_data): … … 195 196 u = urllib2.urlopen(field_data) 196 197 except ValueError: 197 raise ValidationError, "Invalid URL: %s"% field_data198 raise ValidationError, _("Invalid URL: %s") % field_data 198 199 except: # urllib2.HTTPError, urllib2.URLError, httplib.InvalidURL, etc. 199 raise ValidationError, "The URL %s is a broken link."% field_data200 raise ValidationError, _("The URL %s is a broken link.") % field_data 200 201 201 202 def isValidUSState(field_data, all_data): … … 203 204 states = ['AA', 'AE', 'AK', 'AL', 'AP', 'AR', 'AS', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'FM', 'GA', 'GU', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MH', 'MI', 'MN', 'MO', 'MP', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'PR', 'PW', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VI', 'VT', 'WA', 'WI', 'WV', 'WY'] 204 205 if field_data.upper() not in states: 205 raise ValidationError, "Enter a valid U.S. state abbreviation."206 raise ValidationError, _("Enter a valid U.S. state abbreviation.") 206 207 207 208 def hasNoProfanities(field_data, all_data): … … 218 219 from django.utils.text import get_text_list 219 220 plural = len(words_seen) > 1 220 raise ValidationError, "Watch your mouth! The word%s %s %s not allowed here."% \221 raise ValidationError, _("Watch your mouth! The word%s %s %s not allowed here.") % \ 221 222 (plural and 's' or '', 222 223 get_text_list(['"%s%s%s"' % (i[0], '-'*(len(i)-2), i[-1]) for i in words_seen], 'and'), … … 226 227 def __init__(self, other_field_name, error_message=None): 227 228 self.other = other_field_name 228 self.error_message = error_message or "This field must match the '%s' field."% self.other229 self.error_message = error_message or gettext_lazy("This field must match the '%s' field.") % self.other 229 230 self.always_test = True 230 231 … … 245 246 246 247 class RequiredIfOtherFieldNotGiven: 247 def __init__(self, other_field_name, error_message= "Please enter something for at least one field."):248 def __init__(self, other_field_name, error_message=gettext_lazy("Please enter something for at least one field.")): 248 249 self.other, self.error_message = other_field_name, error_message 249 250 self.always_test = True … … 254 255 255 256 class RequiredIfOtherFieldsGiven: 256 def __init__(self, other_field_names, error_message= "Please enter both fields or leave them both empty."):257 def __init__(self, other_field_names, error_message=gettext_lazy("Please enter both fields or leave them both empty.")): 257 258 self.other, self.error_message = other_field_names, error_message 258 259 self.always_test = True … … 265 266 class RequiredIfOtherFieldGiven(RequiredIfOtherFieldsGiven): 266 267 "Like RequiredIfOtherFieldsGiven, but takes a single field name instead of a list." 267 def __init__(self, other_field_name, error_message= "Please enter both fields or leave them both empty."):268 def __init__(self, other_field_name, error_message=gettext_lazy("Please enter both fields or leave them both empty.")): 268 269 RequiredIfOtherFieldsGiven.__init__(self, [other_field_name], error_message) 269 270 … … 272 273 self.other_field = other_field 273 274 self.other_value = other_value 274 self.error_message = error_message or "This field must be given if %s is %s"% (other_field, other_value)275 self.error_message = error_message or gettext_lazy("This field must be given if %s is %s") % (other_field, other_value) 275 276 self.always_test = True 276 277 … … 283 284 self.other_field = other_field 284 285 self.other_value = other_value 285 self.error_message = error_message or "This field must be given if %s is not %s"% (other_field, other_value)286 self.error_message = error_message or gettext_lazy("This field must be given if %s is not %s") % (other_field, other_value) 286 287 self.always_test = True 287 288 … … 301 302 def __init__(self, field_name, prefix, error_message): 302 303 self.field_name, self.prefix = field_name, prefix 303 self.error_message = error_message or "Duplicate values are not allowed."304 self.error_message = error_message or gettext_lazy("Duplicate values are not allowed.") 304 305 305 306 def __call__(self, field_data, all_data): … … 324 325 val = log(int(field_data)) / log(self.power_of) 325 326 if val != int(val): 326 raise ValidationError, "This value must be a power of %s."% self.power_of327 raise ValidationError, _("This value must be a power of %s.") % self.power_of 327 328 328 329 class IsValidFloat: … … 335 336 float(data) 336 337 except ValueError: 337 raise ValidationError, "Please enter a valid decimal number."338 raise ValidationError, _("Please enter a valid decimal number.") 338 339 if len(data) > (self.max_digits + 1): 339 raise ValidationError, "Please enter a valid decimal number with at most %s total digit%s."% \340 raise ValidationError, _("Please enter a valid decimal number with at most %s total digit%s.") % \ 340 341 (self.max_digits, self.max_digits > 1 and 's' or '') 341 342 if '.' in data and len(data.split('.')[1]) > self.decimal_places: 342 raise ValidationError, "Please enter a valid decimal number with at most %s decimal place%s."% \343 raise ValidationError, _("Please enter a valid decimal number with at most %s decimal place%s.") % \ 343 344 (self.decimal_places, self.decimal_places > 1 and 's' or '') 344 345 … … 350 351 def __init__(self, min_size=None, max_size=None, min_error_message=None, max_error_message=None): 351 352 self.min_size, self.max_size = min_size, max_size 352 self.min_error_message = min_error_message or "Make sure your uploaded file is at least %s bytes big."% min_size353 self.max_error_message = max_error_message or "Make sure your uploaded file is at most %s bytes big."% min_size353 self.min_error_message = min_error_message or gettext_lazy("Make sure your uploaded file is at least %s bytes big.") % min_size 354 self.max_error_message = max_error_message or gettext_lazy("Make sure your uploaded file is at most %s bytes big.") % min_size 354 355 355 356 def __call__(self, field_data, all_data): … … 364 365 should be in string format, not already compiled. 365 366 """ 366 def __init__(self, regexp, error_message= "The format for this field is wrong."):367 def __init__(self, regexp, error_message=gettext_lazy("The format for this field is wrong.")): 367 368 self.regexp = re.compile(regexp) 368 369 self.error_message = error_message … … 379 380 specify one on instantiation. 380 381 """ 381 def __init__(self, validator_list=[], error_message= "This field is invalid."):382 def __init__(self, validator_list=[], error_message=gettext_lazy("This field is invalid.")): 382 383 self.validator_list = validator_list 383 384 self.error_message = error_message … … 414 415 info = urllib2.urlopen(field_data).info() 415 416 except (urllib2.HTTPError, urllib2.URLError): 416 raise URLMimeTypeCheck.CouldNotRetrieve, "Could not retrieve anything from %s."% field_data417 raise URLMimeTypeCheck.CouldNotRetrieve, _("Could not retrieve anything from %s.") % field_data 417 418 content_type = info['content-type'] 418 419 if content_type not in self.mime_type_list: 419 raise URLMimeTypeCheck.InvalidContentType, "The URL %s returned the invalid Content-Type header '%s'."% (field_data, content_type)420 raise URLMimeTypeCheck.InvalidContentType, _("The URL %s returned the invalid Content-Type header '%s'.") % (field_data, content_type) 420 421 421 422 class RelaxNGCompact: … … 449 450 m = re.search(r'Expected "(.*?)" to terminate element starting on line (\d+)', message) 450 451 if m: 451 display_errors.append( 'Please close the unclosed %s tag from line %s. (Line starts with "%s".)'% \452 display_errors.append(_('Please close the unclosed %s tag from line %s. (Line starts with "%s".)') % \ 452 453 (m.group(1).replace('/', ''), m.group(2), lines[int(m.group(2)) - 1][:30])) 453 454 continue 454 455 if message.strip() == 'text not allowed here': 455 display_errors.append( 'Some text starting on line %s is not allowed in that context. (Line starts with "%s".)'% \456 display_errors.append(_('Some text starting on line %s is not allowed in that context. (Line starts with "%s".)') % \ 456 457 (line, lines[int(line) - 1][:30])) 457 458 continue 458 459 m = re.search(r'\s*attribute "(.*?)" not allowed at this point; ignored', message) 459 460 if m: 460 display_errors.append( '"%s" on line %s is an invalid attribute. (Line starts with "%s".)'% \461 display_errors.append(_('"%s" on line %s is an invalid attribute. (Line starts with "%s".)') % \ 461 462 (m.group(1), line, lines[int(line) - 1][:30])) 462 463 continue 463 464 m = re.search(r'\s*unknown element "(.*?)"', message) 464 465 if m: 465 display_errors.append( '"<%s>" on line %s is an invalid tag. (Line starts with "%s".)'% \466 display_errors.append(_('"<%s>" on line %s is an invalid tag. (Line starts with "%s".)') % \ 466 467 (m.group(1), line, lines[int(line) - 1][:30])) 467 468 continue 468 469 if message.strip() == 'required attributes missing': 469 display_errors.append( 'A tag on line %s is missing one or more required attributes. (Line starts with "%s".)'% \470 display_errors.append(_('A tag on line %s is missing one or more required attributes. (Line starts with "%s".)') % \ 470 471 (line, lines[int(line) - 1][:30])) 471 472 continue 472 473 m = re.search(r'\s*bad value for attribute "(.*?)"', message) 473 474 if m: 474 display_errors.append( 'The "%s" attribute on line %s has an invalid value. (Line starts with "%s".)'% \475 display_errors.append(_('The "%s" attribute on line %s has an invalid value. (Line starts with "%s".)') % \ 475 476 (m.group(1), line, lines[int(line) - 1][:30])) 476 477 continue
