iro/validate.py
branchdevel
changeset 115 323d06431100
parent 114 1ed072cc6793
child 118 e16c0250c974
equal deleted inserted replaced
114:1ed072cc6793 115:323d06431100
     1 import re
     1 import re
     2 from functools import wraps
     2 from decorator import decorator
     3 
     3 
     4 from inspect import getcallargs
     4 from inspect import getcallargs
     5 from .error import ValidateException
     5 from .error import ValidateException
     6 
     6 
     7 def boolean(value, field):
     7 def vBool(value, field):
     8     t=[True,"true",1]
     8     t=[True, 1, "true", "True", "TRUE"]
     9     f=[False,"false",0]
     9     f=[False, 0, "false", "False", "FALSE"]
    10     if value in t:
    10     if value in t:
    11         return True
    11         return True
    12     elif value in f:
    12     elif value in f:
    13         return False
    13         return False
    14     else:
    14     else:
    15         raise ValidateException(field=field, msg='%s is not boolean' % field)
    15         raise ValidateException(field=field, msg='%s is not boolean' % field)
    16 
    16 
    17 
    17 
    18 def validateHash(value,field,minlength=None,maxlength=None):
    18 def vHash(value,field,minlength=None,maxlength=None):
    19     if not re.match(r'^[a-f0-9]*$', value.lower()):
    19     if not re.match(r'^[a-f0-9]*$', value.lower()):
    20         raise ValidateException(field=field)
    20         raise ValidateException(field=field)
    21     if minlength and len(value)<minlength:
    21     if minlength and len(value)<minlength:
    22         raise ValidateException(field=field)
    22         raise ValidateException(field=field)
    23     if maxlength and len(value)>maxlength:
    23     if maxlength and len(value)>maxlength:
    24         raise ValidateException(field=field)
    24         raise ValidateException(field=field)
    25     return value
    25     return value
    26 
    26 
    27 def vuserhash(value,field):
    27 def vTel(value,field):
    28     '''vailidate function for userhash'''
    28     return value
    29     return validateHash(value,field,minlength=15,maxlength=15)
    29 
       
    30 def vEmail(value, field):
       
    31     return value
    30 
    32 
    31 def validate(kwd,func, need=True,*args,**kargs):
    33 def validate(kwd,func, need=True,*args,**kargs):
    32     '''validate decorator
    34     '''validate decorator
    33     use it like this:
    35     use it like this:
    34         @validate(kwd=userhash, func=vuserhash)
    36         @validate(kwd=userhash, func=vuserhash)
    35         f(userhash)
    37         f(userhash)
    36     that will validate usrhash with the function vuserhash.
    38     that will validate usrhash with the function vuserhash.
    37     Every validate function should raise an Exception, if the the value is not valid'''
    39     Every validate function should raise an Exception, if the the value is not valid'''
    38     def v(f):
    40     @decorator
    39         @wraps(f)
    41     def v(f,*a,**k):
    40         def new_f(*a,**k):
    42         kp=getcallargs(f,*a,**k)
    41             kp=getcallargs(new_f.original,*a,**k)
       
    42             try:
       
    43                 if need or kp[kwd] is not None:
       
    44                     kp[kwd] = func(kp[kwd],kwd,*args,**kargs)
       
    45                 else:
       
    46                     kp[kwd] = None
       
    47             except KeyError:
       
    48                 if need:
       
    49                     raise ValidateException(field=kwd,msg="%s is nessasary"%kwd)
       
    50             return f(**kp)
       
    51         try:
    43         try:
    52             new_f.original=f.original
    44             if need or kp[kwd] is not None:
    53         except AttributeError:
    45                 kp[kwd] = func(kp[kwd],kwd,*args,**kargs)
    54             new_f.original=f
    46             else:
    55         return new_f
    47                 kp[kwd] = None
       
    48         except KeyError:
       
    49             if need:
       
    50                 raise ValidateException(field=kwd,msg="%s is nessasary"%kwd)
       
    51         return f(**kp)
    56     return v
    52     return v
    57  
    53