Commits

Lynn Rees committed 6f7f418

[svn]

  • Participants
  • Parent commits 7e74258
  • Branches wsgiform
  • Tags svn.4

Comments (0)

Files changed (1)

File branches/0.2/wsgiform.py

 from xml.sax import saxutils
 
 _trans = string.maketrans('', '')
+_zip = re.compile(r'(^\d{5}$)|(^\d{5}-*\d{4}$)')
+_ip4 = re.compile('r^[1-2]{0,1}[1-9]{0,1}[$')
 
 def _hyperescape(data):
     '''Escapes punctuation with HTML entities except ., -, and _.'''
 def sterilize(environ, strict=False):
     '''Removes all form data characters except alphanumerics, ., -, and _.'''
     return _runfunc(extract(environ, strict), _sterilize)
+
+def istext(text):
+    return isinstance(text, basestring)
+
+def isint(num):
+    return isinstance(int(num), num)
+
+def islength(data, length):
+    return len(data) == length
+
+def iszipcode(data):
+    if _zip.match(data): return True
     
 
+
 class WsgiForm(object):
 
     '''Class that parsing form data into dictionaries, individual environ entries,
         self._strict = kw.get('strict', False)
         # Function to escape metachars
         self._func = self._funclist[(kw.get('func', 'escape'))]
+        # Function to handle form validation errors
+        self._errorhandler = kw.get('errapp', self._errapp)
         
     def __call__(self, env, start_return):
         '''Call function.'''
             env[self._key] = cgi.FieldStorage(fp=env['wsgi.input'], environ=env)
         else:
             qdict = self._func(env, self._strict)
-            if self._validators: self._validate(qdict)
+            if self._validators:
+                if not self._validate(qdict):
+                    return self._errorhandler(environ, start_return)                    
             # Make individual environ entries
             if self._style == 'environ':
                 for k, v in qdict.iteritems(): env[self._key % k] = v
         '''
         for key, value in qdict.iteritems():
             try:
-                self._validators[key](value)
+                if not self._validators[key](value): return False                    
             except KeyError: pass
+        return True
 
+    def _errorapp(self, environ, start_return):
+        start_return('200 OK', ('Content-type', 'text/plain'))
+        return ['Data in form was invalid.']
 
 def form(**kw):
     '''Decorator for form processing.'''