79 from cubicweb import Binary, tags, uilib |
79 from cubicweb import Binary, tags, uilib |
80 from cubicweb.utils import support_args |
80 from cubicweb.utils import support_args |
81 from cubicweb.web import INTERNAL_FIELD_VALUE, ProcessFormError, eid_param, \ |
81 from cubicweb.web import INTERNAL_FIELD_VALUE, ProcessFormError, eid_param, \ |
82 formwidgets as fw |
82 formwidgets as fw |
83 from cubicweb.web.views import uicfg |
83 from cubicweb.web.views import uicfg |
84 |
|
85 |
84 |
86 class UnmodifiedField(Exception): |
85 class UnmodifiedField(Exception): |
87 """raise this when a field has not actually been edited and you want to skip |
86 """raise this when a field has not actually been edited and you want to skip |
88 it |
87 it |
89 """ |
88 """ |
464 except AttributeError: |
463 except AttributeError: |
465 # fields with eidparam=True but not corresponding to an actual |
464 # fields with eidparam=True but not corresponding to an actual |
466 # attribute or relation |
465 # attribute or relation |
467 return True |
466 return True |
468 # if it's a non final relation, we need the eids |
467 # if it's a non final relation, we need the eids |
469 # XXX underlying regression: getattr(ent, 'foo') used to return |
|
470 # a tuple, now we get a list |
|
471 if isinstance(previous_value, (list, tuple)): |
468 if isinstance(previous_value, (list, tuple)): |
472 # widget should return a set of untyped eids |
469 # widget should return a set of untyped eids |
473 previous_value = set(e.eid for e in previous_value) |
470 previous_value = set(e.eid for e in previous_value) |
474 try: |
471 try: |
475 new_value = self.process_form_value(form) |
472 new_value = self.process_form_value(form) |
1162 return eids |
1159 return eids |
1163 |
1160 |
1164 |
1161 |
1165 _AFF_KWARGS = uicfg.autoform_field_kwargs |
1162 _AFF_KWARGS = uicfg.autoform_field_kwargs |
1166 |
1163 |
1167 def guess_field(eschema, rschema, role='subject', **kwargs): |
1164 def guess_field(eschema, rschema, role='subject', req=None, **kwargs): |
1168 """This function return the most adapted field to edit the given relation |
1165 """This function return the most adapted field to edit the given relation |
1169 (`rschema`) where the given entity type (`eschema`) is the subject or object |
1166 (`rschema`) where the given entity type (`eschema`) is the subject or object |
1170 (`role`). |
1167 (`role`). |
1171 |
1168 |
1172 The field is initialized according to information found in the schema, |
1169 The field is initialized according to information found in the schema, |
1210 for cstr in rdef.constraints: |
1207 for cstr in rdef.constraints: |
1211 if isinstance(cstr, SizeConstraint) and cstr.max is not None: |
1208 if isinstance(cstr, SizeConstraint) and cstr.max is not None: |
1212 kwargs['max_length'] = cstr.max |
1209 kwargs['max_length'] = cstr.max |
1213 return StringField(**kwargs) |
1210 return StringField(**kwargs) |
1214 if fieldclass is FileField: |
1211 if fieldclass is FileField: |
|
1212 if req: |
|
1213 aff_kwargs = req.vreg['uicfg'].select('autoform_field_kwargs', req) |
|
1214 else: |
|
1215 aff_kwargs = _AFF_KWARGS |
1215 for metadata in KNOWN_METAATTRIBUTES: |
1216 for metadata in KNOWN_METAATTRIBUTES: |
1216 metaschema = eschema.has_metadata(rschema, metadata) |
1217 metaschema = eschema.has_metadata(rschema, metadata) |
1217 if metaschema is not None: |
1218 if metaschema is not None: |
1218 metakwargs = _AFF_KWARGS.etype_get(eschema, metaschema, 'subject') |
1219 metakwargs = aff_kwargs.etype_get(eschema, metaschema, 'subject') |
1219 kwargs['%s_field' % metadata] = guess_field(eschema, metaschema, |
1220 kwargs['%s_field' % metadata] = guess_field(eschema, metaschema, |
1220 **metakwargs) |
1221 req=req, **metakwargs) |
1221 return fieldclass(**kwargs) |
1222 return fieldclass(**kwargs) |
1222 return RelationField.fromcardinality(card, **kwargs) |
1223 return RelationField.fromcardinality(card, **kwargs) |
1223 |
1224 |
1224 |
1225 |
1225 FIELDS = { |
1226 FIELDS = { |