iro/model/decorators.py
changeset 302 3f4bdea2abbf
parent 294 0e75bd39767d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iro/model/decorators.py	Thu Sep 27 17:15:46 2012 +0200
@@ -0,0 +1,89 @@
+# Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net>
+# 
+# This file is part of Iro.
+# 
+# Permission is hereby granted, free of charge, to any person obtaining a copy of
+# this software and associated documentation files (the "Software"), to deal in
+# the Software without restriction, including without limitation the rights to use,
+# copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
+# #Software, and to permit persons to whom the Software is furnished to do so,
+# subject to the following conditions:
+# 
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+# 
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+"""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