"""All decorators, that are created by this package.
Imports:
- :func:`.user.vUser` -- a validator for apikeys.
- :func:`.pool.runInDBPool` -- runs a actual function in dbpool.
"""
import types
from .user import vUser
from .schema import Offer
from .dbdefer import dbdefer
from .pool import runInDBPool
from ..error import ValidateException
@dbdefer
def vRoute(session, value, field, typ, allowString=True, allowList=True):
""" a validator to test a valid route. use with :func:`iro.validate.validate`.
:param session: a valid session object (is created by decorator :func:`iro.model.dbdefer.dbdefer`)
:param value: the value to test
:param string field: the field that is tested (only used to get a propper error message).
:param string typ: a typ to test the route in
:param boolean allowString: a single route is allowd.
:param boolean allowList: a list of routes is allowed.
:return: *value*, if value is a valid route for a given typ.
:raises: :exc:`iro.error.ValidateException`
"""
str_ = False
ret = []
if type(value) is types.StringType:
if not allowString:
raise ValidateException(field=field,msg='%s must be a list of routes.'%field)
str_=True
value=[value]
elif not allowList:
raise ValidateException(field=field,msg='%s must be a route - No list of routes.'%field)
routes = [o.name for o in Offer.routes(session,typ)]
providers = [o.provider for o in Offer.providers(session,typ)]
for v in value:
if v not in routes and v not in providers and v != "default":
raise ValidateException(field=field,msg='Route %s is not valid.'%v)
if v not in ret:
ret.append(v)
if str_:
return ret[0]
return ret
@dbdefer
def vTyp(value,field, session):
""" a validator to test a valid typ. use with :func:`iro.validate.validate`.
:param session: a valid session object (is created by decorator :func:`iro.model.dbdefer.dbdefer`)
:param value: the value to test
:param string field: the field that is tested (only used to get a propper error message).
:return: *value*, if value is a valid typ.
:raises: :exc:`iro.error.ValidateException`
"""
for typ in Offer.typs(session):
if value == typ[0]:
break
else:
raise ValidateException(field=field,msg='Typ %s is not valid.'%value)
return value