web/views/autoform.py
branchstable
changeset 4629 1eeef3cbf528
parent 4626 c26b4df9fc90
child 4642 606364845c34
equal deleted inserted replaced
4628:2f23148d0870 4629:1eeef3cbf528
   167 class InlineEntityCreationFormView(InlineEntityEditionFormView):
   167 class InlineEntityCreationFormView(InlineEntityEditionFormView):
   168     """
   168     """
   169     :attr etype: the entity type being created in the inline form
   169     :attr etype: the entity type being created in the inline form
   170     """
   170     """
   171     __regid__ = 'inline-creation'
   171     __regid__ = 'inline-creation'
   172     __select__ = (match_kwargs('peid', 'rtype')
   172     __select__ = (match_kwargs('peid', 'petype', 'rtype')
   173                   & specified_etype_implements('Any'))
   173                   & specified_etype_implements('Any'))
       
   174     _select_attrs = InlineEntityEditionFormView._select_attrs + ('petype',)
   174 
   175 
   175     @property
   176     @property
   176     def removejs(self):
   177     def removejs(self):
   177         entity = self._entity()
   178         entity = self._entity()
   178         ttype = self._cw.entity_from_eid(self.peid).__regid__
   179         rdef = entity.e_schema.rdef(self.rtype, neg_role(self.role), self.petype)
   179         rdef = entity.e_schema.rdef(self.rtype, neg_role(self.role), ttype)
       
   180         card= rdef.role_cardinality(self.role)
   180         card= rdef.role_cardinality(self.role)
   181         # when one is adding an inline entity for a relation of a single card,
   181         # when one is adding an inline entity for a relation of a single card,
   182         # the 'add a new xxx' link disappears. If the user then cancel the addition,
   182         # the 'add a new xxx' link disappears. If the user then cancel the addition,
   183         # we have to make this link appears back. This is done by giving add new link
   183         # we have to make this link appears back. This is done by giving add new link
   184         # id to removeInlineForm.
   184         # id to removeInlineForm.
   207 class InlineAddNewLinkView(InlineEntityCreationFormView):
   207 class InlineAddNewLinkView(InlineEntityCreationFormView):
   208     """
   208     """
   209     :attr card: the cardinality of the relation according to role of `peid`
   209     :attr card: the cardinality of the relation according to role of `peid`
   210     """
   210     """
   211     __regid__ = 'inline-addnew-link'
   211     __regid__ = 'inline-addnew-link'
   212     __select__ = (match_kwargs('peid', 'rtype')
   212     __select__ = (match_kwargs('peid', 'petype', 'rtype')
   213                   & specified_etype_implements('Any'))
   213                   & specified_etype_implements('Any'))
   214 
   214 
   215     _select_attrs = InlineEntityCreationFormView._select_attrs + ('card',)
   215     _select_attrs = InlineEntityCreationFormView._select_attrs + ('card',)
   216     form = None # no actual form wrapped
   216     form = None # no actual form wrapped
   217 
   217 
   218     def call(self, i18nctx, **kwargs):
   218     def call(self, i18nctx, **kwargs):
   219         self._cw.set_varmaker()
   219         self._cw.set_varmaker()
   220         divid = "addNew%s%s%s:%s" % (self.etype, self.rtype, self.role, self.peid)
   220         divid = "addNew%s%s%s:%s" % (self.etype, self.rtype, self.role, self.peid)
   221         self.w(u'<div class="inlinedform" id="%s" cubicweb:limit="true">'
   221         self.w(u'<div class="inlinedform" id="%s" cubicweb:limit="true">'
   222           % divid)
   222           % divid)
   223         js = "addInlineCreationForm('%s', '%s', '%s', '%s', '%s')" % (
   223         js = "addInlineCreationForm('%s', '%s', '%s', '%s', '%s', '%s')" % (
   224             self.peid, self.etype, self.rtype, self.role, i18nctx)
   224             self.peid, self.petype, self.etype, self.rtype, self.role, i18nctx)
   225         if self.pform.should_hide_add_new_relation_link(self.rtype, self.card):
   225         if self.pform.should_hide_add_new_relation_link(self.rtype, self.card):
   226             js = "toggleVisibility('%s'); %s" % (divid, js)
   226             js = "toggleVisibility('%s'); %s" % (divid, js)
   227         __ = self._cw.pgettext
   227         __ = self._cw.pgettext
   228         self.w(u'<a class="addEntity" id="add%s:%slink" href="javascript: %s" >+ %s.</a>'
   228         self.w(u'<a class="addEntity" id="add%s:%slink" href="javascript: %s" >+ %s.</a>'
   229           % (self.rtype, self.peid, js, __(i18nctx, 'add a %s' % self.etype)))
   229           % (self.rtype, self.peid, js, __(i18nctx, 'add a %s' % self.etype)))
   708                 # we can create more than one related entity, we thus display a link
   708                 # we can create more than one related entity, we thus display a link
   709                 # to add new related entities
   709                 # to add new related entities
   710                 if self.should_display_add_new_relation_link(rschema, formviews, card):
   710                 if self.should_display_add_new_relation_link(rschema, formviews, card):
   711                     addnewlink = self._cw.vreg['views'].select(
   711                     addnewlink = self._cw.vreg['views'].select(
   712                         'inline-addnew-link', self._cw,
   712                         'inline-addnew-link', self._cw,
   713                         etype=ttype, rtype=rschema, role=role,
   713                         etype=ttype, rtype=rschema, role=role, card=card,
   714                         peid=self.edited_entity.eid, pform=self, card=card)
   714                         peid=self.edited_entity.eid,
       
   715                         petype=self.edited_entity.e_schema, pform=self)
   715                     formviews.append(addnewlink)
   716                     formviews.append(addnewlink)
   716                 allformviews += formviews
   717                 allformviews += formviews
   717         return allformviews
   718         return allformviews
   718 
   719 
   719     def should_inline_relation_form(self, rschema, targettype, role):
   720     def should_inline_relation_form(self, rschema, targettype, role):
   769         """yield inline form views to a newly related (hence created) entity
   770         """yield inline form views to a newly related (hence created) entity
   770         through the given relation
   771         through the given relation
   771         """
   772         """
   772         yield self._cw.vreg['views'].select('inline-creation', self._cw,
   773         yield self._cw.vreg['views'].select('inline-creation', self._cw,
   773                                             etype=ttype, rtype=rschema, role=role,
   774                                             etype=ttype, rtype=rschema, role=role,
   774                                             peid=self.edited_entity.eid, pform=self)
   775                                             peid=self.edited_entity.eid,
       
   776                                             petype=self.edited_entity.e_schema,
       
   777                                             pform=self)
   775 
   778 
   776 
   779 
   777 ## default form ui configuration ##############################################
   780 ## default form ui configuration ##############################################
   778 
   781 
   779 # use primary and not generated for eid since it has to be an hidden
   782 # use primary and not generated for eid since it has to be an hidden