1010
1111import urllib2
1212from django .conf import settings
13- from django .utils .translation import ugettext , ugettext_lazy , ungettext
13+ from django .utils .translation import ugettext as _ , ugettext_lazy , ungettext
1414from django .utils .functional import Promise , lazy
1515import re
1616
@@ -61,30 +61,30 @@ def __str__(self):
6161
6262def isAlphaNumeric (field_data , all_data ):
6363 if not alnum_re .search (field_data ):
64- raise ValidationError , ugettext ("This value must contain only letters, numbers and underscores." )
64+ raise ValidationError , _ ("This value must contain only letters, numbers and underscores." )
6565
6666def isAlphaNumericURL (field_data , all_data ):
6767 if not alnumurl_re .search (field_data ):
68- raise ValidationError , ugettext ("This value must contain only letters, numbers, underscores, dashes or slashes." )
68+ raise ValidationError , _ ("This value must contain only letters, numbers, underscores, dashes or slashes." )
6969
7070def isSlug (field_data , all_data ):
7171 if not slug_re .search (field_data ):
72- raise ValidationError , ugettext ("This value must contain only letters, numbers, underscores or hyphens." )
72+ raise ValidationError , _ ("This value must contain only letters, numbers, underscores or hyphens." )
7373
7474def isLowerCase (field_data , all_data ):
7575 if field_data .lower () != field_data :
76- raise ValidationError , ugettext ("Uppercase letters are not allowed here." )
76+ raise ValidationError , _ ("Uppercase letters are not allowed here." )
7777
7878def isUpperCase (field_data , all_data ):
7979 if field_data .upper () != field_data :
80- raise ValidationError , ugettext ("Lowercase letters are not allowed here." )
80+ raise ValidationError , _ ("Lowercase letters are not allowed here." )
8181
8282def isCommaSeparatedIntegerList (field_data , all_data ):
8383 for supposed_int in field_data .split (',' ):
8484 try :
8585 int (supposed_int )
8686 except ValueError :
87- raise ValidationError , ugettext ("Enter only digits separated by commas." )
87+ raise ValidationError , _ ("Enter only digits separated by commas." )
8888
8989def isCommaSeparatedEmailList (field_data , all_data ):
9090 """
@@ -96,32 +96,32 @@ def isCommaSeparatedEmailList(field_data, all_data):
9696 try :
9797 isValidEmail (supposed_email .strip (), '' )
9898 except ValidationError :
99- raise ValidationError , ugettext ("Enter valid e-mail addresses separated by commas." )
99+ raise ValidationError , _ ("Enter valid e-mail addresses separated by commas." )
100100
101101def isValidIPAddress4 (field_data , all_data ):
102102 if not ip4_re .search (field_data ):
103- raise ValidationError , ugettext ("Please enter a valid IP address." )
103+ raise ValidationError , _ ("Please enter a valid IP address." )
104104
105105def isNotEmpty (field_data , all_data ):
106106 if field_data .strip () == '' :
107- raise ValidationError , ugettext ("Empty values are not allowed here." )
107+ raise ValidationError , _ ("Empty values are not allowed here." )
108108
109109def isOnlyDigits (field_data , all_data ):
110110 if not field_data .isdigit ():
111- raise ValidationError , ugettext ("Non-numeric characters aren't allowed here." )
111+ raise ValidationError , _ ("Non-numeric characters aren't allowed here." )
112112
113113def isNotOnlyDigits (field_data , all_data ):
114114 if field_data .isdigit ():
115- raise ValidationError , ugettext ("This value can't be comprised solely of digits." )
115+ raise ValidationError , _ ("This value can't be comprised solely of digits." )
116116
117117def isInteger (field_data , all_data ):
118118 # This differs from isOnlyDigits because this accepts the negative sign
119119 if not integer_re .search (field_data ):
120- raise ValidationError , ugettext ("Enter a whole number." )
120+ raise ValidationError , _ ("Enter a whole number." )
121121
122122def isOnlyLetters (field_data , all_data ):
123123 if not field_data .isalpha ():
124- raise ValidationError , ugettext ("Only alphabetical characters are allowed here." )
124+ raise ValidationError , _ ("Only alphabetical characters are allowed here." )
125125
126126def _isValidDate (date_string ):
127127 """
@@ -136,30 +136,30 @@ def _isValidDate(date_string):
136136 # This check is needed because strftime is used when saving the date
137137 # value to the database, and strftime requires that the year be >=1900.
138138 if year < 1900 :
139- raise ValidationError , ugettext ('Year must be 1900 or later.' )
139+ raise ValidationError , _ ('Year must be 1900 or later.' )
140140 try :
141141 date (year , month , day )
142142 except ValueError , e :
143- msg = ugettext ('Invalid date: %s' ) % ugettext (str (e ))
143+ msg = _ ('Invalid date: %s' ) % _ (str (e ))
144144 raise ValidationError , msg
145145
146146def isValidANSIDate (field_data , all_data ):
147147 if not ansi_date_re .search (field_data ):
148- raise ValidationError , ugettext ('Enter a valid date in YYYY-MM-DD format.' )
148+ raise ValidationError , _ ('Enter a valid date in YYYY-MM-DD format.' )
149149 _isValidDate (field_data )
150150
151151def isValidANSITime (field_data , all_data ):
152152 if not ansi_time_re .search (field_data ):
153- raise ValidationError , ugettext ('Enter a valid time in HH:MM format.' )
153+ raise ValidationError , _ ('Enter a valid time in HH:MM format.' )
154154
155155def isValidANSIDatetime (field_data , all_data ):
156156 if not ansi_datetime_re .search (field_data ):
157- raise ValidationError , ugettext ('Enter a valid date/time in YYYY-MM-DD HH:MM format.' )
157+ raise ValidationError , _ ('Enter a valid date/time in YYYY-MM-DD HH:MM format.' )
158158 _isValidDate (field_data .split ()[0 ])
159159
160160def isValidEmail (field_data , all_data ):
161161 if not email_re .search (field_data ):
162- raise ValidationError , ugettext ('Enter a valid e-mail address.' )
162+ raise ValidationError , _ ('Enter a valid e-mail address.' )
163163
164164def isValidImage (field_data , all_data ):
165165 """
@@ -171,34 +171,34 @@ def isValidImage(field_data, all_data):
171171 try :
172172 content = field_data ['content' ]
173173 except TypeError :
174- raise ValidationError , ugettext ("No file was submitted. Check the encoding type on the form." )
174+ raise ValidationError , _ ("No file was submitted. Check the encoding type on the form." )
175175 try :
176176 Image .open (StringIO (content ))
177177 except IOError : # Python Imaging Library doesn't recognize it as an image
178- raise ValidationError , ugettext ("Upload a valid image. The file you uploaded was either not an image or a corrupted image." )
178+ raise ValidationError , _ ("Upload a valid image. The file you uploaded was either not an image or a corrupted image." )
179179
180180def isValidImageURL (field_data , all_data ):
181181 uc = URLMimeTypeCheck (('image/jpeg' , 'image/gif' , 'image/png' ))
182182 try :
183183 uc (field_data , all_data )
184184 except URLMimeTypeCheck .InvalidContentType :
185- raise ValidationError , ugettext ("The URL %s does not point to a valid image." ) % field_data
185+ raise ValidationError , _ ("The URL %s does not point to a valid image." ) % field_data
186186
187187def isValidPhone (field_data , all_data ):
188188 if not phone_re .search (field_data ):
189- raise ValidationError , ugettext ('Phone numbers must be in XXX-XXX-XXXX format. "%s" is invalid.' ) % field_data
189+ raise ValidationError , _ ('Phone numbers must be in XXX-XXX-XXXX format. "%s" is invalid.' ) % field_data
190190
191191def isValidQuicktimeVideoURL (field_data , all_data ):
192192 "Checks that the given URL is a video that can be played by QuickTime (qt, mpeg)"
193193 uc = URLMimeTypeCheck (('video/quicktime' , 'video/mpeg' ,))
194194 try :
195195 uc (field_data , all_data )
196196 except URLMimeTypeCheck .InvalidContentType :
197- raise ValidationError , ugettext ("The URL %s does not point to a valid QuickTime video." ) % field_data
197+ raise ValidationError , _ ("The URL %s does not point to a valid QuickTime video." ) % field_data
198198
199199def isValidURL (field_data , all_data ):
200200 if not url_re .search (field_data ):
201- raise ValidationError , ugettext ("A valid URL is required." )
201+ raise ValidationError , _ ("A valid URL is required." )
202202
203203def isValidHTML (field_data , all_data ):
204204 import urllib , urllib2
@@ -212,14 +212,14 @@ def isValidHTML(field_data, all_data):
212212 return
213213 from xml .dom .minidom import parseString
214214 error_messages = [e .firstChild .wholeText for e in parseString (u .read ()).getElementsByTagName ('messages' )[0 ].getElementsByTagName ('msg' )]
215- raise ValidationError , ugettext ("Valid HTML is required. Specific errors are:\n %s" ) % "\n " .join (error_messages )
215+ raise ValidationError , _ ("Valid HTML is required. Specific errors are:\n %s" ) % "\n " .join (error_messages )
216216
217217def isWellFormedXml (field_data , all_data ):
218218 from xml .dom .minidom import parseString
219219 try :
220220 parseString (field_data )
221221 except Exception , e : # Naked except because we're not sure what will be thrown
222- raise ValidationError , ugettext ("Badly formed XML: %s" ) % str (e )
222+ raise ValidationError , _ ("Badly formed XML: %s" ) % str (e )
223223
224224def isWellFormedXmlFragment (field_data , all_data ):
225225 isWellFormedXml ('<root>%s</root>' % field_data , all_data )
@@ -249,7 +249,7 @@ def isValidUSState(field_data, all_data):
249249 "Checks that the given string is a valid two-letter U.S. state abbreviation"
250250 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' ]
251251 if field_data .upper () not in states :
252- raise ValidationError , ugettext ("Enter a valid U.S. state abbreviation." )
252+ raise ValidationError , _ ("Enter a valid U.S. state abbreviation." )
253253
254254def hasNoProfanities (field_data , all_data ):
255255 """
@@ -364,11 +364,11 @@ def __init__(self, lower=None, upper=None, error_message=''):
364364 self .lower , self .upper = lower , upper
365365 if not error_message :
366366 if lower and upper :
367- self .error_message = ugettext ("This value must be between %(lower)s and %(upper)s." ) % {'lower' : lower , 'upper' : upper }
367+ self .error_message = _ ("This value must be between %(lower)s and %(upper)s." ) % {'lower' : lower , 'upper' : upper }
368368 elif lower :
369- self .error_message = ugettext ("This value must be at least %s." ) % lower
369+ self .error_message = _ ("This value must be at least %s." ) % lower
370370 elif upper :
371- self .error_message = ugettext ("This value must be no more than %s." ) % upper
371+ self .error_message = _ ("This value must be no more than %s." ) % upper
372372 else :
373373 self .error_message = error_message
374374
@@ -404,7 +404,7 @@ def __call__(self, field_data, all_data):
404404 from math import log
405405 val = log (int (field_data )) / log (self .power_of )
406406 if val != int (val ):
407- raise ValidationError , ugettext ("This value must be a power of %s." ) % self .power_of
407+ raise ValidationError , _ ("This value must be a power of %s." ) % self .power_of
408408
409409class IsValidFloat (object ):
410410 def __init__ (self , max_digits , decimal_places ):
@@ -415,7 +415,7 @@ def __call__(self, field_data, all_data):
415415 try :
416416 float (data )
417417 except ValueError :
418- raise ValidationError , ugettext ("Please enter a valid decimal number." )
418+ raise ValidationError , _ ("Please enter a valid decimal number." )
419419 # Negative floats require more space to input.
420420 max_allowed_length = data .startswith ('-' ) and (self .max_digits + 2 ) or (self .max_digits + 1 )
421421 if len (data ) > max_allowed_length :
@@ -504,10 +504,10 @@ def __call__(self, field_data, all_data):
504504 try :
505505 info = urllib2 .urlopen (field_data ).info ()
506506 except (urllib2 .HTTPError , urllib2 .URLError ):
507- raise URLMimeTypeCheck .CouldNotRetrieve , ugettext ("Could not retrieve anything from %s." ) % field_data
507+ raise URLMimeTypeCheck .CouldNotRetrieve , _ ("Could not retrieve anything from %s." ) % field_data
508508 content_type = info ['content-type' ]
509509 if content_type not in self .mime_type_list :
510- raise URLMimeTypeCheck .InvalidContentType , ugettext ("The URL %(url)s returned the invalid Content-Type header '%(contenttype)s'." ) % {
510+ raise URLMimeTypeCheck .InvalidContentType , _ ("The URL %(url)s returned the invalid Content-Type header '%(contenttype)s'." ) % {
511511 'url' : field_data , 'contenttype' : content_type }
512512
513513class RelaxNGCompact (object ):
0 commit comments