39 lines
844 B
Python
39 lines
844 B
Python
# coding=UTF-8
|
|
from api.models.market import iter_markets
|
|
|
|
|
|
class ValidationError(Exception):
|
|
|
|
def __init__(self, message):
|
|
Exception.__init__(self)
|
|
self.message = message
|
|
|
|
|
|
def validate_keys_exists(obj, *keys):
|
|
for key in keys:
|
|
if not key in obj:
|
|
raise ValidationError('No {} is specified'.format(key))
|
|
return True
|
|
|
|
|
|
def is_number(s):
|
|
try:
|
|
float(s)
|
|
return True
|
|
except ValueError:
|
|
return False
|
|
|
|
|
|
def validate_number(*values):
|
|
for val in values:
|
|
if not is_number(val):
|
|
raise ValidationError('{} is not a number'.format(val))
|
|
return True
|
|
|
|
|
|
def validate_territory(value):
|
|
for market in iter_markets():
|
|
if market.territory == value:
|
|
return True
|
|
raise ValidationError('{} is not a valid territory'.format(value))
|