[autoform] when building inlined-form view for a relation, consider related entities intead of other views
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 04 Feb 2016 12:00:23 +0100
changeset 11100 5758ba784ebd
parent 11094 13c9cd75f097
child 11101 66fb68c25f83
[autoform] when building inlined-form view for a relation, consider related entities intead of other views by considering previous form-views, we may miss that there exists related entities but which are not editable. Closes #10495602
web/test/unittest_views_editforms.py
web/views/autoform.py
--- a/web/test/unittest_views_editforms.py	Fri Feb 05 12:26:28 2016 +0100
+++ b/web/test/unittest_views_editforms.py	Thu Feb 04 12:00:23 2016 +0100
@@ -15,7 +15,9 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
+
 from logilab.common.testlib import unittest_main, mock_object
+from logilab.common import tempattr
 
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.web.views import uicfg
@@ -181,6 +183,22 @@
                 autoform = self.vreg['forms'].select('edition', req, entity=req.user)
                 self.assertEqual(list(autoform.inlined_form_views()), [])
 
+    def test_inlined_form_views(self):
+        # when some relation has + cardinality, and some already linked entities which are not
+        # updatable, a link to optionally add a new sub-entity should be displayed, not a sub-form
+        # forcing creation of a sub-entity
+        from cubicweb.web.views import autoform
+        with self.admin_access.web_request() as req:
+            req.create_entity('EmailAddress', address=u'admin@cubicweb.org',
+                              reverse_use_email=req.user.eid)
+            use_email_schema = self.vreg.schema['CWUser'].rdef('use_email')
+            with tempattr(use_email_schema, 'cardinality', '+1'):
+                with self.temporary_permissions(EmailAddress={'update': ()}):
+                    form = self.vreg['forms'].select('edition', req, entity=req.user)
+                    formviews = list(form.inlined_form_views())
+                    self.assertEqual(len(formviews), 1, formviews)
+                    self.assertIsInstance(formviews[0], autoform.InlineAddNewLinkView)
+
     def test_check_inlined_rdef_permissions(self):
         # try to check permissions when creating an entity ('user' below is a
         # fresh entity without an eid)
--- a/web/views/autoform.py	Fri Feb 05 12:26:28 2016 +0100
+++ b/web/views/autoform.py	Thu Feb 04 12:00:23 2016 +0100
@@ -899,14 +899,13 @@
             ttype = tschema.type
             formviews = list(self.inline_edition_form_view(rschema, ttype, role))
             card = rschema.role_rdef(entity.e_schema, ttype, role).role_cardinality(role)
-            # there is no related entity and we need at least one: we need to
-            # display one explicit inline-creation view
-            if self.should_display_inline_creation_form(rschema, formviews, card):
+            related = entity.has_eid() and entity.related(rschema, role)
+            if self.should_display_inline_creation_form(rschema, related, card):
                 formviews += self.inline_creation_form_view(rschema, ttype, role)
             # we can create more than one related entity, we thus display a link
             # to add new related entities
             if self.must_display_add_new_relation_link(rschema, role, tschema,
-                                                       ttype, formviews, card):
+                                                       ttype, related, card):
                 addnewlink = self._cw.vreg['views'].select(
                     'inline-addnew-link', self._cw,
                     etype=ttype, rtype=rschema, role=role, card=card,