def __init__(self, **kargs):
super(EntityBase, self).__init__(kargs)
self.validate()
try:
raise getattr(self, 'error')
except AttributeError:
pass
except Invalid as e:
raise MultipleInvalid([e])
python类Invalid()的实例源码
def test_invalid_ceps(self):
for i in self.invalid_cases:
self.assertRaises(Invalid, CEP, i)
def test_invalid_prices(self):
for i in self.invalid_cases:
self.assertRaises(Invalid, Price, i)
def test_invalid_cnpjs(self):
for i in self.invalid_cases:
self.assertRaises(Invalid, CNPJ, i)
def test_invalid_emails(self):
for i in self.invalid_cases:
self.assertRaises(Invalid, Email, i)
def test_invalid_dates(self):
for i in self.invalid_cases:
self.assertRaises(Invalid, Date, i)
def CNPJ(value):
if not validate_cnpj(value):
raise Invalid("Invalid CNPJ")
return clean_id(value)
def CPF(value):
if not validate_cpf(value):
raise Invalid("Invalid CPF")
return clean_id(value)
def CEP(value):
try:
format_cep(value)
except ValueError as e:
raise Invalid(e)
return clean_id(value)
def Price(value):
if not (type(value) == int or type(value) == float):
raise Invalid("This price is not and integer or a float.")
value = float(value)
return "%.2f" % value
def __init__(self, **kargs):
super(EntityBase, self).__init__(kargs)
self.validate()
try:
raise getattr(self, 'error')
except AttributeError:
pass
except Invalid as e:
raise MultipleInvalid([e])
def test_invalid_ceps(self):
for i in self.invalid_cases:
self.assertRaises(Invalid, CEP, i)
def test_invalid_prices(self):
for i in self.invalid_cases:
self.assertRaises(Invalid, Price, i)
def test_invalid_cnpjs(self):
for i in self.invalid_cases:
self.assertRaises(Invalid, CNPJ, i)
def test_invalid_cnpjs(self):
for i in self.invalid_cases:
self.assertRaises(Invalid, CPF, i)
def test_invalid_dates(self):
for i in self.invalid_cases:
self.assertRaises(Invalid, Date, i)
def invalid(kind, item):
return v.Invalid('Invalid {}: {}'.format(kind, item))
def check(*callback_tuples):
"""
Voluptuous wrapper function to raise our APIException
Args:
callback_tuples: a callback_tuple should contain (status, msg, callbacks)
Returns:
Returns a function callback for the Schema
"""
def v(value):
"""
Trys to validate the value with the given callbacks.
Args:
value: the item to validate
Raises:
APIException with the given error code and msg.
Returns:
The value if the validation callbacks are satisfied.
"""
for msg, callbacks in callback_tuples:
for callback in callbacks:
try:
result = callback(value)
if not result and type(result) == bool:
raise Invalid()
except Exception:
raise WebException(msg)
return value
return v