|
1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
1 """abstract form classes for CubicWeb web client |
18 """abstract form classes for CubicWeb web client |
2 |
19 |
3 :organization: Logilab |
|
4 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
|
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
|
7 """ |
20 """ |
8 __docformat__ = "restructuredtext en" |
21 __docformat__ = "restructuredtext en" |
9 |
22 |
10 from warnings import warn |
23 from warnings import warn |
11 |
24 |
12 from logilab.common.decorators import iclassmethod |
25 from logilab.common.decorators import iclassmethod |
13 from logilab.common.deprecation import deprecated |
26 from logilab.common.deprecation import deprecated |
14 |
27 |
15 from cubicweb.appobject import AppObject |
28 from cubicweb.appobject import AppObject |
16 from cubicweb.view import NOINDEX, NOFOLLOW |
29 from cubicweb.view import NOINDEX, NOFOLLOW |
17 from cubicweb.web import httpcache, formfields, controller |
30 from cubicweb.web import httpcache, formfields, controller, formwidgets as fwdgs |
18 |
|
19 |
31 |
20 class FormViewMixIn(object): |
32 class FormViewMixIn(object): |
21 """abstract form view mix-in""" |
33 """abstract form view mix-in""" |
22 category = 'form' |
34 category = 'form' |
23 http_cache_manager = httpcache.NoHTTPCacheManager |
35 http_cache_manager = httpcache.NoHTTPCacheManager |
135 fields = cls_or_self.fields |
147 fields = cls_or_self.fields |
136 return fields |
148 return fields |
137 |
149 |
138 @iclassmethod |
150 @iclassmethod |
139 def field_by_name(cls_or_self, name, role=None): |
151 def field_by_name(cls_or_self, name, role=None): |
140 """return field with the given name and role. |
152 """Return field with the given name and role. |
141 Raise FieldNotFound if the field can't be found. |
153 |
|
154 Raise :exc:`FieldNotFound` if the field can't be found. |
142 """ |
155 """ |
143 for field in cls_or_self._fieldsattr(): |
156 for field in cls_or_self._fieldsattr(): |
144 if field.name == name and field.role == role: |
157 if field.name == name and field.role == role: |
145 return field |
158 return field |
146 raise FieldNotFound(name, role) |
159 raise FieldNotFound(name, role) |
147 |
160 |
148 @iclassmethod |
161 @iclassmethod |
149 def fields_by_name(cls_or_self, name, role=None): |
162 def fields_by_name(cls_or_self, name, role=None): |
150 """return a list of fields with the given name and role""" |
163 """Return a list of fields with the given name and role.""" |
151 return [field for field in cls_or_self._fieldsattr() |
164 return [field for field in cls_or_self._fieldsattr() |
152 if field.name == name and field.role == role] |
165 if field.name == name and field.role == role] |
153 |
166 |
154 @iclassmethod |
167 @iclassmethod |
155 def remove_field(cls_or_self, field): |
168 def remove_field(cls_or_self, field): |
156 """remove a field from form class or instance""" |
169 """Remove the given field.""" |
157 cls_or_self._fieldsattr().remove(field) |
170 cls_or_self._fieldsattr().remove(field) |
158 |
171 |
159 @iclassmethod |
172 @iclassmethod |
160 def append_field(cls_or_self, field): |
173 def append_field(cls_or_self, field): |
161 """append a field to form class or instance""" |
174 """Append the given field.""" |
162 cls_or_self._fieldsattr().append(field) |
175 cls_or_self._fieldsattr().append(field) |
163 |
176 |
164 @iclassmethod |
177 @iclassmethod |
165 def insert_field_before(cls_or_self, new_field, name, role='subject'): |
178 def insert_field_before(cls_or_self, field, name, role=None): |
166 field = cls_or_self.field_by_name(name, role) |
179 """Insert the given field before the field of given name and role.""" |
|
180 bfield = cls_or_self.field_by_name(name, role) |
167 fields = cls_or_self._fieldsattr() |
181 fields = cls_or_self._fieldsattr() |
168 fields.insert(fields.index(field), new_field) |
182 fields.insert(fields.index(bfield), field) |
169 |
183 |
170 @iclassmethod |
184 @iclassmethod |
171 def insert_field_after(cls_or_self, new_field, name, role='subject'): |
185 def insert_field_after(cls_or_self, field, name, role=None): |
172 field = cls_or_self.field_by_name(name, role) |
186 """Insert the given field after the field of given name and role.""" |
|
187 afield = cls_or_self.field_by_name(name, role) |
173 fields = cls_or_self._fieldsattr() |
188 fields = cls_or_self._fieldsattr() |
174 fields.insert(fields.index(field)+1, new_field) |
189 fields.insert(fields.index(afield)+1, field) |
|
190 |
|
191 @iclassmethod |
|
192 def add_hidden(cls_or_self, name, value=None, **kwargs): |
|
193 """Append an hidden field to the form. `name`, `value` and extra keyword |
|
194 arguments will be given to the field constructor. The inserted field is |
|
195 returned. |
|
196 """ |
|
197 kwargs.setdefault('ignore_req_params', True) |
|
198 kwargs.setdefault('widget', fwdgs.HiddenInput) |
|
199 field = formfields.StringField(name=name, value=value, **kwargs) |
|
200 if 'id' in kwargs: |
|
201 # by default, hidden input don't set id attribute. If one is |
|
202 # explicitly specified, ensure it will be set |
|
203 field.widget.setdomid = True |
|
204 cls_or_self.append_field(field) |
|
205 return field |
175 |
206 |
176 def session_key(self): |
207 def session_key(self): |
177 """return the key that may be used to store / retreive data about a |
208 """return the key that may be used to store / retreive data about a |
178 previous post which failed because of a validation error |
209 previous post which failed because of a validation error |
179 """ |
210 """ |