cubicweb/web/views/autoform.py
changeset 11858 b2ceb483e056
parent 11767 432f87a63057
child 11875 011730a4af73
equal deleted inserted replaced
11857:a166ddb503aa 11858:b2ceb483e056
   859 
   859 
   860     def editable_attributes(self, strict=False):
   860     def editable_attributes(self, strict=False):
   861         """return a list of (relation schema, role) to edit for the entity"""
   861         """return a list of (relation schema, role) to edit for the entity"""
   862         if self.display_fields is not None:
   862         if self.display_fields is not None:
   863             schema = self._cw.vreg.schema
   863             schema = self._cw.vreg.schema
   864             return [(schema[rtype], role) for rtype, role in self.display_fields]
   864             for rtype, role in self.display_fields:
       
   865                 yield (schema[rtype], role)
   865         if self.edited_entity.has_eid() and not self.edited_entity.cw_has_perm('update'):
   866         if self.edited_entity.has_eid() and not self.edited_entity.cw_has_perm('update'):
   866             return []
   867             return
   867         action = 'update' if self.edited_entity.has_eid() else 'add'
   868         action = 'update' if self.edited_entity.has_eid() else 'add'
   868         return [(rtype, role) for rtype, _, role in self._relations_by_section(
   869         for rtype, _, role in self._relations_by_section('attributes', action, strict):
   869             'attributes', action, strict)]
   870             yield (rtype, role)
   870 
   871 
   871     def editable_relations(self):
   872     def editable_relations(self):
   872         """return a sorted list of (relation's label, relation'schema, role) for
   873         """return a sorted list of (relation's label, relation'schema, role) for
   873         relations in the 'relations' section
   874         relations in the 'relations' section
   874         """
   875         """
   875         result = []
   876         return sorted(self.iter_editable_relations())
   876         for rschema, _, role in self._relations_by_section('relations',
   877 
   877                                                            strict=True):
   878     def iter_editable_relations(self):
   878             result.append( (rschema.display_name(self.edited_entity._cw, role,
   879         for rschema, _, role in self._relations_by_section('relations', strict=True):
   879                                                  self.edited_entity.cw_etype),
   880             yield (rschema.display_name(self.edited_entity._cw, role,
   880                             rschema, role) )
   881                                         self.edited_entity.cw_etype),
   881         return sorted(result)
   882                    rschema, role)
   882 
   883 
   883     def inlined_relations(self):
   884     def inlined_relations(self):
   884         """return a list of (relation schema, target schemas, role) matching
   885         """return a list of (relation schema, target schemas, role) matching
   885         given category(ies) and permission
   886         given category(ies) and permission
   886         """
   887         """
   887         return self._relations_by_section('inlined')
   888         return self._relations_by_section('inlined')
   888 
   889 
   889     # inlined forms control ####################################################
   890     # inlined forms control ####################################################
   890 
   891 
   891     def inlined_form_views(self):
   892     def inlined_form_views(self):
   892         """compute and return list of inlined form views (hosting the inlined
   893         """Yield inlined form views (hosting the inlined form object)
   893         form object)
   894         """
   894         """
       
   895         allformviews = []
       
   896         entity = self.edited_entity
   895         entity = self.edited_entity
   897         for rschema, ttypes, role in self.inlined_relations():
   896         for rschema, ttypes, role in self.inlined_relations():
   898             # show inline forms only if there's one possible target type
   897             # show inline forms only if there's one possible target type
   899             # for rschema
   898             # for rschema
   900             if len(ttypes) != 1:
   899             if len(ttypes) != 1:
   902                              'inlined form but there is multiple target types, '
   901                              'inlined form but there is multiple target types, '
   903                              'dunno what to do', rschema)
   902                              'dunno what to do', rschema)
   904                 continue
   903                 continue
   905             tschema = ttypes[0]
   904             tschema = ttypes[0]
   906             ttype = tschema.type
   905             ttype = tschema.type
   907             formviews = list(self.inline_edition_form_view(rschema, ttype, role))
   906             existing = bool(entity.related(rschema, role)) if entity.has_eid() else False
       
   907             for formview in self.inline_edition_form_view(rschema, ttype, role):
       
   908                 yield formview
       
   909                 existing = True
   908             card = rschema.role_rdef(entity.e_schema, ttype, role).role_cardinality(role)
   910             card = rschema.role_rdef(entity.e_schema, ttype, role).role_cardinality(role)
   909             existing = entity.related(rschema, role) if entity.has_eid() else formviews
       
   910             if self.should_display_inline_creation_form(rschema, existing, card):
   911             if self.should_display_inline_creation_form(rschema, existing, card):
   911                 formviews += self.inline_creation_form_view(rschema, ttype, role)
   912                 for formview in self.inline_creation_form_view(rschema, ttype, role):
       
   913                     yield formview
       
   914                     existing = True
   912             # we can create more than one related entity, we thus display a link
   915             # we can create more than one related entity, we thus display a link
   913             # to add new related entities
   916             # to add new related entities
   914             if self.must_display_add_new_relation_link(rschema, role, tschema,
   917             if self.must_display_add_new_relation_link(rschema, role, tschema,
   915                                                        ttype, existing, card):
   918                                                        ttype, existing, card):
   916                 addnewlink = self._cw.vreg['views'].select(
   919                 addnewlink = self._cw.vreg['views'].select(
   917                     'inline-addnew-link', self._cw,
   920                     'inline-addnew-link', self._cw,
   918                     etype=ttype, rtype=rschema, role=role, card=card,
   921                     etype=ttype, rtype=rschema, role=role, card=card,
   919                     peid=self.edited_entity.eid,
   922                     peid=self.edited_entity.eid,
   920                     petype=self.edited_entity.e_schema, pform=self)
   923                     petype=self.edited_entity.e_schema, pform=self)
   921                 formviews.append(addnewlink)
   924                 yield addnewlink
   922             allformviews += formviews
       
   923         return allformviews
       
   924 
   925 
   925     def should_display_inline_creation_form(self, rschema, existing, card):
   926     def should_display_inline_creation_form(self, rschema, existing, card):
   926         """return true if a creation form should be inlined
   927         """return true if a creation form should be inlined
   927 
   928 
   928         by default true if there is no related entity and we need at least one
   929         by default true if there is no related entity and we need at least one