web/formfields.py
changeset 5712 e136d392bd71
parent 5697 ec1ce7198ef4
child 5762 730d458ec1bf
equal deleted inserted replaced
5708:277c8a8eab21 5712:e136d392bd71
  1082         return eids
  1082         return eids
  1083 
  1083 
  1084 
  1084 
  1085 _AFF_KWARGS = uicfg.autoform_field_kwargs
  1085 _AFF_KWARGS = uicfg.autoform_field_kwargs
  1086 
  1086 
  1087 def guess_field(eschema, rschema, role='subject', skip_meta_attr=True, **kwargs):
  1087 def guess_field(eschema, rschema, role='subject', **kwargs):
  1088     """This function return the most adapted field to edit the given relation
  1088     """This function return the most adapted field to edit the given relation
  1089     (`rschema`) where the given entity type (`eschema`) is the subject or object
  1089     (`rschema`) where the given entity type (`eschema`) is the subject or object
  1090     (`role`).
  1090     (`role`).
  1091 
  1091 
  1092     The field is initialized according to information found in the schema,
  1092     The field is initialized according to information found in the schema,
  1093     though any value can be explicitly specified using `kwargs`.
  1093     though any value can be explicitly specified using `kwargs`.
  1094 
       
  1095     The `skip_meta_attr` flag is used to specify wether this function should
       
  1096     return a field for attributes considered as a meta-attributes
       
  1097     (e.g. describing an other attribute, such as the format or file name of a
       
  1098     file (`Bytes`) attribute).
       
  1099     """
  1094     """
  1100     fieldclass = None
  1095     fieldclass = None
  1101     rdef = eschema.rdef(rschema, role)
  1096     rdef = eschema.rdef(rschema, role)
  1102     if role == 'subject':
  1097     if role == 'subject':
  1103         targetschema = rdef.object
  1098         targetschema = rdef.object
  1115         kwargs.setdefault('label', (eschema.type, rschema.type + '_object'))
  1110         kwargs.setdefault('label', (eschema.type, rschema.type + '_object'))
  1116     else:
  1111     else:
  1117         kwargs.setdefault('label', (eschema.type, rschema.type))
  1112         kwargs.setdefault('label', (eschema.type, rschema.type))
  1118     kwargs.setdefault('help', rdef.description)
  1113     kwargs.setdefault('help', rdef.description)
  1119     if rschema.final:
  1114     if rschema.final:
  1120         if skip_meta_attr and rschema in eschema.meta_attributes():
       
  1121             return None
       
  1122         fieldclass = FIELDS[targetschema]
  1115         fieldclass = FIELDS[targetschema]
  1123         if fieldclass is StringField:
  1116         if fieldclass is StringField:
  1124             if eschema.has_metadata(rschema, 'format'):
  1117             if eschema.has_metadata(rschema, 'format'):
  1125                 # use RichTextField instead of StringField if the attribute has
  1118                 # use RichTextField instead of StringField if the attribute has
  1126                 # a "format" metadata. But getting information from constraints
  1119                 # a "format" metadata. But getting information from constraints
  1142             for metadata in KNOWN_METAATTRIBUTES:
  1135             for metadata in KNOWN_METAATTRIBUTES:
  1143                 metaschema = eschema.has_metadata(rschema, metadata)
  1136                 metaschema = eschema.has_metadata(rschema, metadata)
  1144                 if metaschema is not None:
  1137                 if metaschema is not None:
  1145                     metakwargs = _AFF_KWARGS.etype_get(eschema, metaschema, 'subject')
  1138                     metakwargs = _AFF_KWARGS.etype_get(eschema, metaschema, 'subject')
  1146                     kwargs['%s_field' % metadata] = guess_field(eschema, metaschema,
  1139                     kwargs['%s_field' % metadata] = guess_field(eschema, metaschema,
  1147                                                                 skip_meta_attr=False,
       
  1148                                                                 **metakwargs)
  1140                                                                 **metakwargs)
  1149         return fieldclass(**kwargs)
  1141         return fieldclass(**kwargs)
  1150     return RelationField.fromcardinality(card, **kwargs)
  1142     return RelationField.fromcardinality(card, **kwargs)
  1151 
  1143 
  1152 
  1144