|
1 # Copyright (c) 2012 netzguerilla.net <iro@netzguerilla.net> |
|
2 # |
|
3 # This file is part of Iro. |
|
4 # |
|
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6 # this software and associated documentation files (the "Software"), to deal in |
|
7 # the Software without restriction, including without limitation the rights to use, |
|
8 # copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
|
9 # #Software, and to permit persons to whom the Software is furnished to do so, |
|
10 # subject to the following conditions: |
|
11 # |
|
12 # The above copyright notice and this permission notice shall be included in |
|
13 # all copies or substantial portions of the Software. |
|
14 # |
|
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
|
16 # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
|
17 # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
20 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
21 |
|
22 """All decorators, that are created by this package. |
|
23 |
|
24 Imports: |
|
25 |
|
26 - :func:`.user.vUser` -- a validator for apikeys. |
|
27 - :func:`.pool.runInDBPool` -- runs a actual function in dbpool. |
|
28 """ |
|
29 import types |
|
30 |
|
31 from .user import vUser |
|
32 from .schema import Offer |
|
33 from .dbdefer import dbdefer |
|
34 from .pool import runInDBPool |
|
35 |
|
36 from ..error import ValidateException |
|
37 |
|
38 @dbdefer |
|
39 def vRoute(session, value, field, typ, allowString=True, allowList=True): |
|
40 """ a validator to test a valid route. use with :func:`iro.validate.validate`. |
|
41 |
|
42 :param session: a valid session object (is created by decorator :func:`iro.model.dbdefer.dbdefer`) |
|
43 :param value: the value to test |
|
44 :param string field: the field that is tested (only used to get a propper error message). |
|
45 :param string typ: a typ to test the route in |
|
46 :param boolean allowString: a single route is allowd. |
|
47 :param boolean allowList: a list of routes is allowed. |
|
48 :return: *value*, if value is a valid route for a given typ. |
|
49 :raises: :exc:`iro.error.ValidateException` |
|
50 """ |
|
51 str_ = False |
|
52 ret = [] |
|
53 |
|
54 if type(value) is types.StringType: |
|
55 if not allowString: |
|
56 raise ValidateException(field=field,msg='%s must be a list of routes.'%field) |
|
57 str_=True |
|
58 value=[value] |
|
59 elif not allowList: |
|
60 raise ValidateException(field=field,msg='%s must be a route - No list of routes.'%field) |
|
61 |
|
62 routes = [o.name for o in Offer.routes(session,typ)] |
|
63 providers = [o.provider for o in Offer.providers(session,typ)] |
|
64 for v in value: |
|
65 if v not in routes and v not in providers and v != "default": |
|
66 raise ValidateException(field=field,msg='Route %s is not valid.'%v) |
|
67 if v not in ret: |
|
68 ret.append(v) |
|
69 if str_: |
|
70 return ret[0] |
|
71 return ret |
|
72 |
|
73 @dbdefer |
|
74 def vTyp(value,field, session): |
|
75 """ a validator to test a valid typ. use with :func:`iro.validate.validate`. |
|
76 |
|
77 :param session: a valid session object (is created by decorator :func:`iro.model.dbdefer.dbdefer`) |
|
78 :param value: the value to test |
|
79 :param string field: the field that is tested (only used to get a propper error message). |
|
80 :return: *value*, if value is a valid typ. |
|
81 :raises: :exc:`iro.error.ValidateException` |
|
82 """ |
|
83 |
|
84 for typ in Offer.typs(session): |
|
85 if value == typ[0]: |
|
86 break |
|
87 else: |
|
88 raise ValidateException(field=field,msg='Typ %s is not valid.'%value) |
|
89 return value |