581 def guess_field(eschema, rschema, role='subject', skip_meta_attr=True, **kwargs): |
581 def guess_field(eschema, rschema, role='subject', skip_meta_attr=True, **kwargs): |
582 """return the most adapated widget to edit the relation |
582 """return the most adapated widget to edit the relation |
583 'subjschema rschema objschema' according to information found in the schema |
583 'subjschema rschema objschema' according to information found in the schema |
584 """ |
584 """ |
585 fieldclass = None |
585 fieldclass = None |
586 card = eschema.cardinality(rschema, role) |
586 rdef = eschema.rdef(rschema, role) |
587 if role == 'subject': |
587 if role == 'subject': |
588 targetschema = rschema.objects(eschema)[0] |
588 targetschema = rdef.object |
589 help = rschema.rproperty(eschema, targetschema, 'description') |
|
590 if rschema.final: |
589 if rschema.final: |
591 if rschema.rproperty(eschema, targetschema, 'internationalizable'): |
590 if rdef.internationalizable: |
592 kwargs.setdefault('internationalizable', True) |
591 kwargs.setdefault('internationalizable', True) |
593 def get_default(form, es=eschema, rs=rschema): |
592 def get_default(form, es=eschema, rs=rschema): |
594 return es.default(rs) |
593 return es.default(rs) |
595 kwargs.setdefault('initial', get_default) |
594 kwargs.setdefault('initial', get_default) |
596 else: |
595 else: |
597 targetschema = rschema.subjects(eschema)[0] |
596 targetschema = rdef.subject |
598 help = rschema.rproperty(targetschema, eschema, 'description') |
597 kwargs['required'] = rdef.role_cardinality(role) in '1+' |
599 kwargs['required'] = card in '1+' |
|
600 kwargs['name'] = rschema.type |
598 kwargs['name'] = rschema.type |
601 if role == 'object': |
599 if role == 'object': |
602 kwargs.setdefault('label', (eschema.type, rschema.type + '_object')) |
600 kwargs.setdefault('label', (eschema.type, rschema.type + '_object')) |
603 else: |
601 else: |
604 kwargs.setdefault('label', (eschema.type, rschema.type)) |
602 kwargs.setdefault('label', (eschema.type, rschema.type)) |
605 kwargs['eidparam'] = True |
603 kwargs['eidparam'] = True |
606 kwargs.setdefault('help', help) |
604 kwargs.setdefault('help', rdef.description) |
607 if rschema.final: |
605 if rschema.final: |
608 if skip_meta_attr and rschema in eschema.meta_attributes(): |
606 if skip_meta_attr and rschema in eschema.meta_attributes(): |
609 return None |
607 return None |
610 fieldclass = FIELDS[targetschema] |
608 fieldclass = FIELDS[targetschema] |
611 if fieldclass is StringField: |
609 if fieldclass is StringField: |
615 return StringField(**kwargs) |
613 return StringField(**kwargs) |
616 if eschema.has_metadata(rschema, 'format'): |
614 if eschema.has_metadata(rschema, 'format'): |
617 # use RichTextField instead of StringField if the attribute has |
615 # use RichTextField instead of StringField if the attribute has |
618 # a "format" metadata. But getting information from constraints |
616 # a "format" metadata. But getting information from constraints |
619 # may be useful anyway... |
617 # may be useful anyway... |
620 constraints = rschema.rproperty(eschema, targetschema, 'constraints') |
618 for cstr in rdef.constraints: |
621 for cstr in constraints: |
|
622 if isinstance(cstr, StaticVocabularyConstraint): |
619 if isinstance(cstr, StaticVocabularyConstraint): |
623 raise Exception('rich text field with static vocabulary') |
620 raise Exception('rich text field with static vocabulary') |
624 return RichTextField(**kwargs) |
621 return RichTextField(**kwargs) |
625 constraints = rschema.rproperty(eschema, targetschema, 'constraints') |
|
626 # init StringField parameters according to constraints |
622 # init StringField parameters according to constraints |
627 for cstr in constraints: |
623 for cstr in rdef.constraints: |
628 if isinstance(cstr, StaticVocabularyConstraint): |
624 if isinstance(cstr, StaticVocabularyConstraint): |
629 kwargs.setdefault('choices', cstr.vocabulary) |
625 kwargs.setdefault('choices', cstr.vocabulary) |
630 break |
626 break |
631 for cstr in constraints: |
627 for cstr in rdef.constraints: |
632 if isinstance(cstr, SizeConstraint) and cstr.max is not None: |
628 if isinstance(cstr, SizeConstraint) and cstr.max is not None: |
633 kwargs['max_length'] = cstr.max |
629 kwargs['max_length'] = cstr.max |
634 return StringField(**kwargs) |
630 return StringField(**kwargs) |
635 if fieldclass is FileField: |
631 if fieldclass is FileField: |
636 for metadata in ('format', 'encoding', 'name'): |
632 for metadata in ('format', 'encoding', 'name'): |