890 # display one explicit inline-creation view |
890 # display one explicit inline-creation view |
891 if self.should_display_inline_creation_form(rschema, formviews, card): |
891 if self.should_display_inline_creation_form(rschema, formviews, card): |
892 formviews += self.inline_creation_form_view(rschema, ttype, role) |
892 formviews += self.inline_creation_form_view(rschema, ttype, role) |
893 # we can create more than one related entity, we thus display a link |
893 # we can create more than one related entity, we thus display a link |
894 # to add new related entities |
894 # to add new related entities |
895 if self.should_display_add_new_relation_link(rschema, formviews, card): |
895 if self.must_display_add_new_relation_link(rschema, role, tschema, |
896 rdef = entity.e_schema.rdef(rschema, role, ttype) |
896 ttype, formviews, card): |
897 if entity.has_eid(): |
897 addnewlink = self._cw.vreg['views'].select( |
898 if role == 'subject': |
898 'inline-addnew-link', self._cw, |
899 rdefkwargs = {'fromeid': entity.eid} |
899 etype=ttype, rtype=rschema, role=role, card=card, |
900 else: |
900 peid=self.edited_entity.eid, |
901 rdefkwargs = {'toeid': entity.eid} |
901 petype=self.edited_entity.e_schema, pform=self) |
902 else: |
902 formviews.append(addnewlink) |
903 rdefkwargs = {} |
|
904 if (tschema.has_perm(self._cw, 'add') |
|
905 and rdef.has_perm(self._cw, 'add', **rdefkwargs)): |
|
906 addnewlink = self._cw.vreg['views'].select( |
|
907 'inline-addnew-link', self._cw, |
|
908 etype=ttype, rtype=rschema, role=role, card=card, |
|
909 peid=self.edited_entity.eid, |
|
910 petype=self.edited_entity.e_schema, pform=self) |
|
911 formviews.append(addnewlink) |
|
912 allformviews += formviews |
903 allformviews += formviews |
913 return allformviews |
904 return allformviews |
914 |
905 |
915 def should_display_inline_creation_form(self, rschema, existant, card): |
906 def should_display_inline_creation_form(self, rschema, existant, card): |
916 """return true if a creation form should be inlined |
907 """return true if a creation form should be inlined |
925 |
916 |
926 by default true if there is no related entity or if the relation has |
917 by default true if there is no related entity or if the relation has |
927 multiple cardinality |
918 multiple cardinality |
928 """ |
919 """ |
929 return not existant or card in '+*' |
920 return not existant or card in '+*' |
|
921 |
|
922 def must_display_add_new_relation_link(self, rschema, role, tschema, |
|
923 ttype, existant, card): |
|
924 """return true if we must add a link to add a new creation form |
|
925 (through ajax call) |
|
926 |
|
927 by default true if there is no related entity or if the relation has |
|
928 multiple cardinality and it is permitted to add the inlined object and |
|
929 relation. |
|
930 """ |
|
931 return (self.should_display_add_new_relation_link( |
|
932 rschema, existant, card) and |
|
933 self.check_inlined_rdef_permissions( |
|
934 rschema, role, tschema, ttype)) |
|
935 |
|
936 def check_inlined_rdef_permissions(self, rschema, role, tschema, ttype): |
|
937 """return true if permissions are granted on the inlined object and |
|
938 relation""" |
|
939 entity = self.edited_entity |
|
940 rdef = entity.e_schema.rdef(rschema, role, ttype) |
|
941 if entity.has_eid(): |
|
942 if role == 'subject': |
|
943 rdefkwargs = {'fromeid': entity.eid} |
|
944 else: |
|
945 rdefkwargs = {'toeid': entity.eid} |
|
946 else: |
|
947 rdefkwargs = {} |
|
948 return (tschema.has_perm(self._cw, 'add') |
|
949 and rdef.has_perm(self._cw, 'add', **rdefkwargs)) |
|
950 |
930 |
951 |
931 def should_hide_add_new_relation_link(self, rschema, card): |
952 def should_hide_add_new_relation_link(self, rschema, card): |
932 """return true if once an inlined creation form is added, the 'add new' |
953 """return true if once an inlined creation form is added, the 'add new' |
933 link should be hidden |
954 link should be hidden |
934 |
955 |