summaryrefslogtreecommitdiffstats
path: root/funcweb
diff options
context:
space:
mode:
authormakkalot <makkalot@gmail.com>2008-06-21 13:18:42 +0300
committermakkalot <makkalot@gmail.com>2008-06-21 13:18:42 +0300
commit4706ba8c79bfae2feb48fc7fd8b9bb7770077ae4 (patch)
treec60e9380d2c2c15c9f93b91ae5807db2c9ce260f /funcweb
parentde503d14a2239fd38f377ae7bf613e553aa2fd8f (diff)
downloadthird_party-func-4706ba8c79bfae2feb48fc7fd8b9bb7770077ae4.tar.gz
third_party-func-4706ba8c79bfae2feb48fc7fd8b9bb7770077ae4.tar.xz
third_party-func-4706ba8c79bfae2feb48fc7fd8b9bb7770077ae4.zip
adding custom validator for hashes
Diffstat (limited to 'funcweb')
-rw-r--r--funcweb/funcweb/tests/test_widget_validation.py39
-rw-r--r--funcweb/funcweb/widget_validation.py43
2 files changed, 78 insertions, 4 deletions
diff --git a/funcweb/funcweb/tests/test_widget_validation.py b/funcweb/funcweb/tests/test_widget_validation.py
index 6b33008..5cba250 100644
--- a/funcweb/funcweb/tests/test_widget_validation.py
+++ b/funcweb/funcweb/tests/test_widget_validation.py
@@ -1,7 +1,7 @@
import unittest
import turbogears
from turbogears import testutil
-from funcweb.widget_validation import WidgetSchemaFactory,MinionIntValidator,MinionFloatValidator,MinionListValidator
+from funcweb.widget_validation import WidgetSchemaFactory,MinionIntValidator,MinionFloatValidator,MinionListValidator,MinionHashValidator
from turbogears import validators
class TestWidgetValidator(unittest.TestCase):
@@ -173,10 +173,31 @@ class TestWidgetValidator(unittest.TestCase):
assert mv.to_python(['FEDORA','MACOSX']) == ['FEDORA','MACOSX']
self.assertRaises(validators.Invalid,mv.to_python,[])
self.assertRaises(validators.Invalid,mv.to_python,['hey_error'])
+ self.assertRaises(validators.Invalid,mv.to_python,{})
+ del mv
print "Happy testing !"
+
+ def test_minion_hash_validator(self):
+
+ #test default
+ mv = MinionHashValidator()
+ assert mv.to_python({'fedora':1,'debian':2,'others':3}) == {'fedora':1,'debian':2,'others':3}
+ del mv
+
+ #test with regex
+ mv = MinionHashValidator(regex_string='^[A-Z]+$',not_empty=True)
+ assert mv.to_python({'FEDORA':'FEDORA','MACOSX':'MACOSX'}) == {'FEDORA':'FEDORA','MACOSX':'MACOSX'}
+ self.assertRaises(validators.Invalid,mv.to_python,{})
+ self.assertRaises(validators.Invalid,mv.to_python,{'hey_error':12})
+ self.assertRaises(validators.Invalid,mv.to_python,"hwy")
+ del mv
+
+ print "Happy testing !"
+
+
def get_string_params(self):
return {
'string_default':{
@@ -262,5 +283,21 @@ class TestWidgetValidator(unittest.TestCase):
},
}
+ def get_hash_params(self):
+ return {
+ 'hash_default':{
+ 'type':'hash',
+ 'default':{'hey':12},
+ 'description':'default hash'
+ },
+ 'hash_regex':{
+ 'type':'hash',
+ 'default':{'hey':12},
+ 'optional':False,
+ 'description':'default regex hash',
+ 'validator':'^[A-Z]$'
+ },
+ }
+
diff --git a/funcweb/funcweb/widget_validation.py b/funcweb/funcweb/widget_validation.py
index 964a159..61d6254 100644
--- a/funcweb/funcweb/widget_validation.py
+++ b/funcweb/funcweb/widget_validation.py
@@ -254,15 +254,52 @@ class MinionListValidator(validators.FancyValidator):
def validate_python(self,value,state):
import re
if self.regex_string:
- compiled_regex = re.compile(self.regex_string)
+ try:
+ compiled_regex = re.compile(self.regex_string)
+ except Exception,e:
+ raise validators.Invalid('The passed regex_string is not a valid expression'%self.regex_string,value,state)
+
for list_value in value:
- if not re.match(compiled_regex,list_value):
+ if not re.match(compiled_regex,str(list_value)):
raise validators.Invalid('The %s doesnt match to the regex expression that was supplied'%list_value,value,state)
#there is no else for now :)
class MinionHashValidator(validators.FancyValidator):
- pass
+
+ regex_string = None
+
+ def _to_python(self,value,state):
+ """
+ Will check just the type here and return
+ value to be validated in validate_python
+ """
+ #will add more beautiful validation here after
+ #integrate the complex widgets for lists and dicts
+ if self.not_empty:
+ if len(value)==0:
+ raise validators.Invalid('Empty hash passed when not_empty is set',value,state)
+
+ #check the type firstly
+ tmp = {}
+ if type(tmp) != type(value):
+ raise validators.Invalid('The value passed to MinionHashValidator should be a dict object',value,state)
+
+ #print value
+ return value
+
+ def validate_python(self,value,state):
+ #print value
+ import re
+ if self.regex_string:
+ try:
+ compiled_regex = re.compile(self.regex_string)
+ except Exception,e:
+ raise validators.Invalid('The passed regex_string is not a valid expression'%self.regex_string,value,state)
+ for dict_value in value.itervalues():
+ if not re.match(compiled_regex,str(dict_value)):
+ raise validators.Invalid('The %s doesnt match to the regex expression that was supplied'%dict_value,value,state)
+
if __name__ == "__main__":