1 """some base form classes for CubicWeb web client |
1 """some base form classes for CubicWeb web client |
2 |
2 |
3 :organization: Logilab |
3 :organization: Logilab |
4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
4 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
7 """ |
7 """ |
8 __docformat__ = "restructuredtext en" |
8 __docformat__ = "restructuredtext en" |
9 |
9 |
187 | (one_line_rset() & non_final_entity())) |
187 | (one_line_rset() & non_final_entity())) |
188 |
188 |
189 internal_fields = FieldsForm.internal_fields + ('__type', 'eid', '__maineid') |
189 internal_fields = FieldsForm.internal_fields + ('__type', 'eid', '__maineid') |
190 domid = 'entityForm' |
190 domid = 'entityForm' |
191 |
191 |
192 @iclassmethod |
192 def __init__(self, req, rset=None, row=None, col=None, *args, **kwargs): |
193 def field_by_name(cls_or_self, name, role=None, eschema=None): |
193 # entity was either explicitly specified or we have a one line rset |
194 """return field with the given name and role. If field is not explicitly |
194 if 'entity' in kwargs: |
195 defined for the form but `eclass` is specified, guess_field will be |
195 self.edited_entity = kwargs.pop('entity') |
196 called. |
196 else: |
197 """ |
197 self.edited_entity = rset.complete_entity(row or 0, col or 0) |
198 try: |
198 self.edited_entity.complete() |
199 return super(EntityFieldsForm, cls_or_self).field_by_name(name, role) |
|
200 except form.FieldNotFound: |
|
201 if eschema is None or role is None or not name in eschema.schema: |
|
202 raise |
|
203 rschema = eschema.schema.rschema(name) |
|
204 # XXX use a sample target type. Document this. |
|
205 tschemas = rschema.targets(eschema, role) |
|
206 fieldcls = _AFF.etype_get(eschema, rschema, role, tschemas[0]) |
|
207 kwargs = _AFF_KWARGS.etype_get(eschema, rschema, role, tschemas[0]) |
|
208 if kwargs is None: |
|
209 kwargs = {} |
|
210 if fieldcls: |
|
211 if not isinstance(fieldcls, type): |
|
212 return fieldcls # already and instance |
|
213 return fieldcls(name=name, role=role, eidparam=True, **kwargs) |
|
214 field = guess_field(eschema, rschema, role, eidparam=True, **kwargs) |
|
215 if field is None: |
|
216 raise |
|
217 return field |
|
218 |
|
219 def __init__(self, *args, **kwargs): |
|
220 self.edited_entity = kwargs.pop('entity', None) |
|
221 msg = kwargs.pop('submitmsg', None) |
199 msg = kwargs.pop('submitmsg', None) |
222 super(EntityFieldsForm, self).__init__(*args, **kwargs) |
200 super(EntityFieldsForm, self).__init__(req, rset, row, col, *args, **kwargs) |
223 if self.edited_entity is None: |
|
224 self.edited_entity = self.cw_rset.complete_entity(self.cw_row or 0, self.cw_col or 0) |
|
225 self.add_hidden('__type', self.edited_entity.__regid__, eidparam=True) |
201 self.add_hidden('__type', self.edited_entity.__regid__, eidparam=True) |
226 self.add_hidden('eid', self.edited_entity.eid) |
202 self.add_hidden('eid', self.edited_entity.eid) |
227 if kwargs.get('mainform', True): # mainform default to true in parent |
203 if kwargs.get('mainform', True): # mainform default to true in parent |
228 self.add_hidden(u'__maineid', self.edited_entity.eid) |
204 self.add_hidden(u'__maineid', self.edited_entity.eid) |
229 # If we need to directly attach the new object to another one |
205 # If we need to directly attach the new object to another one |