Commits

Lynn Rees  committed 5dbb641

[svn]

  • Participants
  • Parent commits ce006d1
  • Branches wsgiform
  • Tags svn.23

Comments (0)

Files changed (1)

File branches/0.3/wsgiform/validators.py

 'vat':r'((DK|FI|HU|LU|MT|SI)(-)?\d{8})|((BE|EE|DE|EL|LT|PT)(-)?\d{9})|((PL|SK)(-)?\d{10})|((IT|LV)(-)?\d{11})|((LT|SE)(-)?\d{12})|(AT(-)?U\d{8})|(CY(-)?\d{8}[A-Z])|(CZ(-)?\d{8,10})|(FR(-)?[\dA-HJ-NP-Z]{2}\d{9})|(IE(-)?\d[A-Z\d]\d{5}[A-Z])|(NL(-)?\d{9}B\d{2})|(ES(-)?[A-Z\d]\d{7}[A-Z\d])',
 }
 
-patterns = dict((k, re.compile(_patterns[k])) for k in _patterns) 
+patterns = dict((k, re.compile(v)) for k, v in _patterns.iteritems()) 
 
 def _validate(key, data):
     '''Validates data by key.'''
     if patterns[key].match(data): return True
+
+# Character
     
 def isascii(data):
     '''Validates data is ASCII'''
-    return isinstance(str(data), str)
+    return isinstance(data, str)
 
 def isunicode(data):
     '''Validates data is unicode.'''
-    return isinstance(unicode(data), str)
+    return isinstance(data, unicode)
 
 def isnumber(data):
     '''Validates data is number.'''
     '''Validates data is under or equals a defined length.'''
     return len(data) <= length
 
-def iszipcode(data):
-    '''Validates data is a valid US ZIP code.'''
-    return _validate('zip', data)
+def isempty(data):
+    '''Validates data is empty.'''
+    return data.strip() == ''
 
-def isukpost(data):
-    '''Validates data is a valid UK postal code.'''
-    return _validate('ukpost', data)
-
-def iscnabbr(data):
-    '''Validates data is a valid Canadian provincial abbreviation.'''
-    return _validate('canada', data)
-
-def iscreditcardnum(data):
-    '''Validates data is a valid format for a credit card number.'''
-    return _validate('creditcard', data)
-
-def isdatetime(data):
-    '''Validates data is a valid format for mm/dd/yyyy hh:MM:ss.'''
-    return _validate('datetime', data)
+# User submissions
 
 def notmalice(data):
-    '''Validates data is free of some potentially malicious content.'''
+    '''Validates data is free of certain potentially malicious content.'''
     return _validate('demalice', data)
 
-def isemail(data):
-    '''Validates data is a valid email address.'''
-    return _validate('email', data)
+def isxml(data):
+    '''Validates data is well-formed XML.'''
+    try:
+        parseString(data)
+    except:
+        return False
+    return True
 
-def isedatetime(data):
-    '''Validates data is a valid format for yyyy/mm/dd hh:MM:ss.'''
-    return _validate('eurodatetime', data)
+def ishtml(data):
+    '''Validates data is not obviously broken HTML.'''
+    try:
+        parser = HTMLParser()
+        parser.feed(data)
+    except:
+        return False
+    return True
+
+# Network Addresses
 
 def isip4(data):
     '''Validates data is a valid IPv4 address.'''
     '''Validates data is a valid IPv6 address.'''
     return _validate('ip6', data)
 
+def ismacaddr(data):
+    '''Validates data is a valid MAC address.'''
+    return _validate('macaddr', data)
+
+def isemail(data):
+    '''Validates data is a valid email address.'''
+    return _validate('email', data)
+
 def isurl(data):
     '''Validates data is a valid URL.'''
     return _validate('url', data)
         urllib.urlopen(data)
     except:
         return False
-    return True        
+    return True
+
+# Username/password
+
+def isname(data):
+    '''Validates data seems like a valid personal name.'''
+    return _validate('namecheck', data)
+
+def ispassword(data):
+    '''Validates that data is a password with 1 upper 1 lower 1 num
+    1 and at least 8 characters.
+    '''
+    return _validate('password', data)
+
+# Postal codes
+
+def iszip(data):
+    '''Validates data is a valid US ZIP code.'''
+    return _validate('zip', data)
+
+def isukpost(data):
+    '''Validates data is a valid UK postal code.'''
+    return _validate('ukpost', data)
+
+def isnapost(data):
+    '''Validates data is a US ZIP or Canadian postal code.'''
+    return _validate('napost', data)
+
+# Territory names
+
+def isprovince(data):
+    '''Validates data is a valid Canadian provincial abbreviation.'''
+    return _validate('canada', data)
+
+def isstate(data):
+    '''Validates that data is a US state abbreviation.'''
+    return _validate('usstates', data)
+
+# Date time
+
+def isdatetime(data):
+    '''Validates data is a valid format for mm/dd/yyyy hh:MM:ss.'''
+    return _validate('datetime', data)
+
+def isedatetime(data):
+    '''Validates data is a valid format for yyyy/mm/dd hh:MM:ss.'''
+    return _validate('eurodatetime', data)
+
+# Numbers
+
+def iscreditcard(data):
+    '''Validates data is a valid format for a credit card number.'''
+    return _validate('creditcard', data)
 
 def isisbn(data):
     '''Validates data is ISBN.'''
     return _validate('isbn', data)
 
-def ismacaddr(data):
-    '''Validates data is a valid MAC address.'''
-    return _validate('macaddr', data)
+def isssn(data):
+    '''Validates that data is a social security number.'''
+    return _validate('ssn', data)
 
-def isname(data):
-    '''Validates data is seems like a valid personal name.'''
-    return _validate('namecheck', data)
+def isvat(data):
+    '''Validates that data is a EU VAT number.'''
+    return _validate('vat', data)
 
-def isnapost(data):
-    '''Validates data is a US ZIP or Canadian postal code.'''
-    return _validate('napost', data)
+def isusd(data):
+    '''Validates that data is a UK phone number.'''
+    return _validate('usd', data)
 
-def ispassword(data):
-    '''Validates that data is a password has 1 upper 1 lower 1 num
-    1 and at least 8 characters.
-    '''
-    return _validate('password', data)
+# Phone numbers
 
 def isphone(data):
     '''Validates that data is a phone number.'''
     return _validate('phone', data)
 
-def isssn(data):
-    '''Validates that data is a social security number.'''
-    return _validate('ssn', data)
-
 def isukphone(data):
     '''Validates that data is a UK phone number.'''
     return _validate('ukphone', data)
 
-def isusd(data):
-    '''Validates that data is a UK phone number.'''
-    return _validate('usd', data)
-
 def isusphone(data):
     '''Validates that data is a US phone number.'''
-    return _validate('usphone', data)
-
-def isusstateabbr(data):
-    '''Validates that data is a US state abbreviation.'''
-    return _validate('usstates', data)
-
-def isvat(data):
-    '''Validates that data is a EU VAT number.'''
-    return _validate('vat', data)
-
-def isxml(data):
-    '''Validates data is well-formed XML.'''
-    try:
-        parseString(data)
-    except:
-        return False
-    return True
-
-def ishtml(data):
-    '''Validates that data is not obviously broken HTML.'''
-    try:
-        parser = HTMLParser()
-        parser.feed(data)
-    except:
-        return False
-    return True
-
-def isempty(data):
-    '''Validates data is not empty.'''
-    return data.strip == ''
+    return _validate('usphone', data)