[web/autoform] fix regression introduced by #5758ba784ebd
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 17 Feb 2016 17:20:29 +0100
changeset 11145 d3edefdeddb4
parent 11141 147280056e58
child 11146 517e7cdd7b1b
[web/autoform] fix regression introduced by #5758ba784ebd The changes from cset 5758ba784ebd "[autoform] when building inlined-form view for a relation, consider related entities intead of other views" lead to an unexpected 'add new' link on creation of an entity with inlined form for a relation of cardinality 1. To avoid this, we should consider formviews and not existing relations when the entity is being created. Closes #10860258
web/test/unittest_views_editforms.py
web/views/autoform.py
--- a/web/test/unittest_views_editforms.py	Thu Feb 18 14:36:39 2016 +0100
+++ b/web/test/unittest_views_editforms.py	Wed Feb 17 17:20:29 2016 +0100
@@ -198,6 +198,13 @@
                     formviews = list(form.inlined_form_views())
                     self.assertEqual(len(formviews), 1, formviews)
                     self.assertIsInstance(formviews[0], autoform.InlineAddNewLinkView)
+            # though do not introduce regression on entity creation with 1 cardinality relation
+            with tempattr(use_email_schema, 'cardinality', '11'):
+                user = self.vreg['etypes'].etype_class('CWUser')(req)
+                form = self.vreg['forms'].select('edition', req, entity=user)
+                formviews = list(form.inlined_form_views())
+                self.assertEqual(len(formviews), 1, formviews)
+                self.assertIsInstance(formviews[0], autoform.InlineEntityCreationFormView)
 
     def test_check_inlined_rdef_permissions(self):
         # try to check permissions when creating an entity ('user' below is a
--- a/web/views/autoform.py	Thu Feb 18 14:36:39 2016 +0100
+++ b/web/views/autoform.py	Wed Feb 17 17:20:29 2016 +0100
@@ -899,13 +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)
-            related = entity.has_eid() and entity.related(rschema, role)
-            if self.should_display_inline_creation_form(rschema, related, card):
+            existing = entity.related(rschema, role) if entity.has_eid() else formviews
+            if self.should_display_inline_creation_form(rschema, existing, 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, related, card):
+                                                       ttype, existing, card):
                 addnewlink = self._cw.vreg['views'].select(
                     'inline-addnew-link', self._cw,
                     etype=ttype, rtype=rschema, role=role, card=card,
@@ -915,24 +915,24 @@
             allformviews += formviews
         return allformviews
 
-    def should_display_inline_creation_form(self, rschema, existant, card):
+    def should_display_inline_creation_form(self, rschema, existing, card):
         """return true if a creation form should be inlined
 
         by default true if there is no related entity and we need at least one
         """
-        return not existant and card in '1+'
+        return not existing and card in '1+'
 
-    def should_display_add_new_relation_link(self, rschema, existant, card):
+    def should_display_add_new_relation_link(self, rschema, existing, card):
         """return true if we should add a link to add a new creation form
         (through ajax call)
 
         by default true if there is no related entity or if the relation has
         multiple cardinality
         """
-        return not existant or card in '+*'
+        return not existing or card in '+*'
 
     def must_display_add_new_relation_link(self, rschema, role, tschema,
-                                           ttype, existant, card):
+                                           ttype, existing, card):
         """return true if we must add a link to add a new creation form
         (through ajax call)
 
@@ -941,7 +941,7 @@
         relation.
         """
         return (self.should_display_add_new_relation_link(
-                    rschema, existant, card) and
+                    rschema, existing, card) and
                 self.check_inlined_rdef_permissions(
                     rschema, role, tschema, ttype))