iro/validate.py
branchdevel
changeset 126 1ac2439a68b5
parent 125 19b3f383c9ce
child 129 d6704178a18f
equal deleted inserted replaced
125:19b3f383c9ce 126:1ac2439a68b5
       
     1 from twisted.internet import defer
       
     2 
     1 import re
     3 import re
     2 from decorator import decorator
     4 from decorator import decorator
       
     5 from inspect import getcallargs
     3 
     6 
     4 from twisted.internet import defer
     7 from .error import ValidateException, NotATelNumber, InvalidMail
     5 
     8 from .telnumber import Telnumber
     6 from inspect import getcallargs
       
     7 from .error import ValidateException
       
     8 
     9 
     9 def vBool(value, field):
    10 def vBool(value, field):
    10     '''Validator for boolean values'''
    11     '''Validator for boolean values'''
    11     t=[True, 1, "true", "True", "TRUE"]
    12     t=[True, 1, "true", "True", "TRUE"]
    12     f=[False, 0, "false", "False", "FALSE"]
    13     f=[False, 0, "false", "False", "FALSE"]
    27     if maxlength and len(value)>maxlength:
    28     if maxlength and len(value)>maxlength:
    28         raise ValidateException(field=field)
    29         raise ValidateException(field=field)
    29     return value
    30     return value
    30 
    31 
    31 def vTel(value,field):
    32 def vTel(value,field):
    32     return value
    33     '''Validator for Telefonnumbers'''
       
    34     ret = []
       
    35     for v in value:
       
    36         try:
       
    37             ret.append(Telnumber(v))
       
    38         except NotATelNumber, e:
       
    39             e.field=field
       
    40             raise e
       
    41     return ret
    33 
    42 
    34 def vEmail(value, field):
    43 def vEmail(value, field):
    35     return value
    44     '''validator for emailadresses (see wikipeda for strange mailadresses and RFC3696)
       
    45     valid:
       
    46     "very.(),:;<>[]\\".VERY.\\"very@\\\ \\"very\\".unusual"@strange.example.com
       
    47     ""@example.org
       
    48     "very.unusual.@.unusual.com"@example.com'
       
    49 
       
    50     not valid:
       
    51     Abc.@example.com
       
    52     Abc..123@example.com
       
    53     thisis."notallowed@example.com
       
    54     this\\ still\\"not\\allowed@example.com
       
    55     '''
       
    56     ret = []
       
    57     for v in value:
       
    58         parts= re.match(r'^(.*)@(.+?)$',v)
       
    59         if not parts:
       
    60             raise InvalidMail(v,field)
       
    61         local=parts.group(1)
       
    62         domain=parts.group(2)
       
    63         
       
    64         if not re.match(r'^(\[[0-9\.]{7,16}\]|\[[0-9a-f:]{3,}\]|([a-z0-9+\-%_]+\.)+[a-z]{2,6})$',domain.lower()):
       
    65             raise InvalidMail(v,field)
       
    66        
       
    67         if local == "":
       
    68             continue
       
    69         if local.startswith(".") or local.endswith("."):
       
    70             raise InvalidMail(v,field)
       
    71         unquote = True
       
    72         parts = local.split('"')
       
    73         c=0
       
    74         i=0
       
    75         for part in parts:
       
    76             if unquote and part != "":                     #unquoted is not allowd so much
       
    77                 if not re.match(r'^[^\\,\[\];\(\)@<>: ]+$',part) or ".." in part:
       
    78                     raise InvalidMail(v,field)
       
    79             if i == 0:
       
    80                 if unquote and part != "" and len(parts) > 1 and part[-1] != '.': #quoted parts must be seperated by a dot
       
    81                     raise InvalidMail(v,field)
       
    82                 unquote = not unquote
       
    83                 c+=1
       
    84             elif part == ''  or part[-1] != "\\":
       
    85                 if unquote  and part != "":         #quoted parts must be seperated by a dot
       
    86                     if part[0] != ".":
       
    87                         raise InvalidMail(v,field)
       
    88                     if i < len(parts)-1 and part[-1] != '.':
       
    89                         raise InvalidMail(v,field)
       
    90                 unquote = not unquote
       
    91                 c += 1
       
    92             i += 1
       
    93         if c%2 == 0 and c > 1:                        #no single quote allowed
       
    94             raise InvalidMail(v,field)
       
    95         ret.append(v)
       
    96     return ret
    36 
    97 
    37 def validate(kwd,func, need=True,*args,**kargs):
    98 def validate(kwd,func, need=True,*args,**kargs):
    38     '''validate decorator
    99     '''validate decorator
    39     use it like this:
   100     use it like this:
    40         @validate(kwd=userhash, func=vuserhash)
   101         @validate(kwd=userhash, func=vuserhash)