12 from logilab.common.decorators import iclassmethod |
12 from logilab.common.decorators import iclassmethod |
13 from logilab.common.deprecation import deprecated |
13 from logilab.common.deprecation import deprecated |
14 |
14 |
15 from cubicweb.appobject import AppObject |
15 from cubicweb.appobject import AppObject |
16 from cubicweb.view import NOINDEX, NOFOLLOW |
16 from cubicweb.view import NOINDEX, NOFOLLOW |
17 from cubicweb.web import httpcache, formfields, controller |
17 from cubicweb.web import httpcache, formfields, controller, formwidgets as fwdgs |
18 |
|
19 |
18 |
20 class FormViewMixIn(object): |
19 class FormViewMixIn(object): |
21 """abstract form view mix-in""" |
20 """abstract form view mix-in""" |
22 category = 'form' |
21 category = 'form' |
23 http_cache_manager = httpcache.NoHTTPCacheManager |
22 http_cache_manager = httpcache.NoHTTPCacheManager |
137 fields = cls_or_self.fields |
136 fields = cls_or_self.fields |
138 return fields |
137 return fields |
139 |
138 |
140 @iclassmethod |
139 @iclassmethod |
141 def field_by_name(cls_or_self, name, role=None): |
140 def field_by_name(cls_or_self, name, role=None): |
142 """return field with the given name and role. |
141 """Return field with the given name and role. |
143 Raise FieldNotFound if the field can't be found. |
142 |
|
143 Raise :exc:`FieldNotFound` if the field can't be found. |
144 """ |
144 """ |
145 for field in cls_or_self._fieldsattr(): |
145 for field in cls_or_self._fieldsattr(): |
146 if field.name == name and field.role == role: |
146 if field.name == name and field.role == role: |
147 return field |
147 return field |
148 raise FieldNotFound(name, role) |
148 raise FieldNotFound(name, role) |
149 |
149 |
150 @iclassmethod |
150 @iclassmethod |
151 def fields_by_name(cls_or_self, name, role=None): |
151 def fields_by_name(cls_or_self, name, role=None): |
152 """return a list of fields with the given name and role""" |
152 """Return a list of fields with the given name and role.""" |
153 return [field for field in cls_or_self._fieldsattr() |
153 return [field for field in cls_or_self._fieldsattr() |
154 if field.name == name and field.role == role] |
154 if field.name == name and field.role == role] |
155 |
155 |
156 @iclassmethod |
156 @iclassmethod |
157 def remove_field(cls_or_self, field): |
157 def remove_field(cls_or_self, field): |
158 """remove a field from form class or instance""" |
158 """Remove the given field.""" |
159 cls_or_self._fieldsattr().remove(field) |
159 cls_or_self._fieldsattr().remove(field) |
160 |
160 |
161 @iclassmethod |
161 @iclassmethod |
162 def append_field(cls_or_self, field): |
162 def append_field(cls_or_self, field): |
163 """append a field to form class or instance""" |
163 """Append the given field.""" |
164 cls_or_self._fieldsattr().append(field) |
164 cls_or_self._fieldsattr().append(field) |
165 |
165 |
166 @iclassmethod |
166 @iclassmethod |
167 def insert_field_before(cls_or_self, new_field, name, role='subject'): |
167 def insert_field_before(cls_or_self, field, name, role=None): |
168 field = cls_or_self.field_by_name(name, role) |
168 """Insert the given field before the field of given name and role.""" |
|
169 bfield = cls_or_self.field_by_name(name, role) |
169 fields = cls_or_self._fieldsattr() |
170 fields = cls_or_self._fieldsattr() |
170 fields.insert(fields.index(field), new_field) |
171 fields.insert(fields.index(bfield), field) |
171 |
172 |
172 @iclassmethod |
173 @iclassmethod |
173 def insert_field_after(cls_or_self, new_field, name, role='subject'): |
174 def insert_field_after(cls_or_self, field, name, role=None): |
174 field = cls_or_self.field_by_name(name, role) |
175 """Insert the given field after the field of given name and role.""" |
|
176 afield = cls_or_self.field_by_name(name, role) |
175 fields = cls_or_self._fieldsattr() |
177 fields = cls_or_self._fieldsattr() |
176 fields.insert(fields.index(field)+1, new_field) |
178 fields.insert(fields.index(afield)+1, field) |
|
179 |
|
180 @iclassmethod |
|
181 def add_hidden(cls_or_self, name, value=None, **kwargs): |
|
182 """Append an hidden field to the form. `name`, `value` and extra keyword |
|
183 arguments will be given to the field constructor. The inserted field is |
|
184 returned. |
|
185 """ |
|
186 kwargs.setdefault('ignore_req_params', True) |
|
187 kwargs.setdefault('widget', fwdgs.HiddenInput) |
|
188 field = formfields.StringField(name=name, value=value, **kwargs) |
|
189 if 'id' in kwargs: |
|
190 # by default, hidden input don't set id attribute. If one is |
|
191 # explicitly specified, ensure it will be set |
|
192 field.widget.setdomid = True |
|
193 cls_or_self.append_field(field) |
|
194 return field |
177 |
195 |
178 def session_key(self): |
196 def session_key(self): |
179 """return the key that may be used to store / retreive data about a |
197 """return the key that may be used to store / retreive data about a |
180 previous post which failed because of a validation error |
198 previous post which failed because of a validation error |
181 """ |
199 """ |