web/views/autoform.py
changeset 10519 0019b7888dd7
parent 10229 512ba8f37bd4
child 10522 1660a0fa4f43
child 10634 06a43f727601
--- a/web/views/autoform.py	Thu Jul 02 09:20:02 2015 +0200
+++ b/web/views/autoform.py	Wed Apr 08 09:27:26 2015 +0200
@@ -214,6 +214,12 @@
         return self.cw_rset.get_entity(self.cw_row, self.cw_col)
 
     @property
+    def petype(self):
+        assert isinstance(self.peid, int)
+        pentity = self._cw.entity_from_eid(self.peid)
+        return pentity.e_schema.type
+
+    @property
     @cached
     def form(self):
         entity = self._entity()
@@ -249,12 +255,25 @@
         creation form.
         """
         entity = self._entity()
-        if isinstance(self.peid, int):
-            pentity = self._cw.entity_from_eid(self.peid)
-            petype = pentity.e_schema.type
-            rdef = entity.e_schema.rdef(self.rtype, neg_role(self.role), petype)
-            card= rdef.role_cardinality(self.role)
-            if card == '1': # don't display remove link
+        rdef = entity.e_schema.rdef(self.rtype, neg_role(self.role), self.petype)
+        card = rdef.role_cardinality(self.role)
+        if card == '1': # don't display remove link
+            return None
+        # if cardinality is 1..n (+), dont display link to remove an inlined form for the first form
+        # allowing to edit the relation. To detect so:
+        #
+        # * if parent form (pform) is None, we're generated through an ajax call and so we know this
+        #   is not the first form
+        #
+        # * if parent form is not None, look for previous InlinedFormField in the parent's form
+        #   fields
+        if card == '+' and self.pform is not None:
+            # retrieve all field'views handling this relation and return None if we're the first of
+            # them
+            first_view = next(iter((f.view for f in self.pform.fields
+                                    if isinstance(f, InlinedFormField)
+                                    and f.view.rtype == self.rtype and f.view.role == self.role)))
+            if self == first_view:
                 return None
         return self.removejs and self.removejs % (
             self.peid, self.rtype, entity.eid)
@@ -314,7 +333,7 @@
     def removejs(self):
         entity = self._entity()
         rdef = entity.e_schema.rdef(self.rtype, neg_role(self.role), self.petype)
-        card= rdef.role_cardinality(self.role)
+        card = rdef.role_cardinality(self.role)
         # when one is adding an inline entity for a relation of a single card,
         # the 'add a new xxx' link disappears. If the user then cancel the addition,
         # we have to make this link appears back. This is done by giving add new link