1
2 u"""
3 :Copyright:
4
5 Copyright 2007 - 2013
6 Andr\xe9 Malo or his licensors, as applicable
7
8 :License:
9
10 Licensed under the Apache License, Version 2.0 (the "License");
11 you may not use this file except in compliance with the License.
12 You may obtain a copy of the License at
13
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
21
22 =====================
23 HTML forms reloaded
24 =====================
25
26 Form helper classes.
27 """
28 __author__ = u"Andr\xe9 Malo"
29 __docformat__ = "restructuredtext en"
30 __all__ = [
31 'DictParameterAdapter', 'ListDictParameterAdapter',
32 'MultiDictParameterAdapter', 'NullParameterAdapter',
33 ]
34
35 from tdi.tools.htmlform._interfaces import ParameterAdapterInterface
36
37
39 """
40 HTMLForm parameter adapter from a simple dict
41
42 :IVariables:
43 `param` : ``dict``
44 Parameters
45 """
46 __implements__ = [ParameterAdapterInterface]
47
49 """
50 Initialization
51
52 :Parameters:
53 `param` : ``dict``
54 Parameters
55 """
56 self.param = param
57
59 """ :See: ``tdi.tools.htmlform.ParameterAdapterInterface`` """
60 return self.param.get(name, default)
61
63 """ :See: ``tdi.tools.htmlform.ParameterAdapterInterface`` """
64 if name in self.param:
65 return [self.param[name]]
66 return []
67
68
70 """
71 HTMLForm parameter adapter from a dict of sequences
72
73 :IVariables:
74 `param` : dict of sequences
75 Parameters
76 """
77 __implements__ = [ParameterAdapterInterface]
78
80 """
81 Initialization
82
83 :Parameters:
84 `param` : dict of sequences
85 Parameters. Empty sequences act as if the key was not present.
86 Otherwise ``getfirst`` will return the first element and
87 ``getlist`` will return a shallow copy of the sequence as a
88 ``list``.
89 """
90 self.param = param
91
93 """ :See: ``tdi.tools.htmlform.ParameterAdapterInterface`` """
94 try:
95 result = self.param[name]
96 except KeyError:
97 pass
98 else:
99 if result:
100 return result[0]
101 return default
102
104 """ :See: ``tdi.tools.htmlform.ParameterAdapterInterface`` """
105 try:
106 result = self.param[name]
107 except KeyError:
108 pass
109 else:
110 return list(result)
111 return []
112
113
115 """
116 HTMLForm parameter adapter from a multidict (like paste provides)
117
118 :IVariables:
119 `param` : multidict
120 Parameters
121 """
122 __implements__ = [ParameterAdapterInterface]
123
125 """
126 Initialization
127
128 :Parameters:
129 `param` : multidict
130 Parameters. The object is expected to provide a getall() method
131 """
132 self.param = param
133
134 - def getfirst(self, name, default=None):
135 """ :See: ``tdi.tools.htmlform.ParameterAdapterInterface`` """
136 try:
137 return self.param.getall(name)[0]
138 except IndexError:
139 return default
140
142 """ :See: ``tdi.tools.htmlform.ParameterAdapterInterface`` """
143 return self.param.getall(name)
144
145
147 """ This adapter just returns nothing """
148 __implements__ = [ParameterAdapterInterface]
149
151 """ :See: `ParameterAdapterInterface.getlist` """
152
153 return []
154
155 - def getfirst(self, name, default=None):
156 """ :See: `ParameterAdapterInterface.getfirst` """
157
158 return default
159