use field.vocabulary instead of field.choices, skip already related eids. Also consider vocabulary when no etype specified
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 27 Jan 2010 09:24:35 +0100
changeset 4385 820aa03f71ad
parent 4384 f7b22d552b13
child 4386 cf8842b69379
use field.vocabulary instead of field.choices, skip already related eids. Also consider vocabulary when no etype specified
web/box.py
--- a/web/box.py	Wed Jan 27 09:22:07 2010 +0100
+++ b/web/box.py	Wed Jan 27 09:24:35 2010 +0100
@@ -209,22 +209,23 @@
         return entity.related(self.rtype, get_role(self), entities=True)
 
     def unrelated_entities(self, entity):
-        """returns the list of unrelated entities
-
-        if etype is not defined on the Box's class, the default
-        behaviour is to use the entity's appropraite vocabulary function
+        """returns the list of unrelated entities, using the entity's
+        appropriate vocabulary function
         """
-        # use entity.unrelated if we've been asked for a particular etype
-        if hasattr(self, 'etype'):
-            return entity.unrelated(self.rtype, self.etype, get_role(self)).entities()
-        # in other cases, use vocabulary functions
+        skip = set(e.eid for e in entity.related(self.rtype, get_role(self),
+                                                 entities=True))
+        skip.add(None)
+        skip.add(INTERNAL_FIELD_VALUE)
+        filteretype = getattr(self, 'etype', None)
         entities = []
-        form = self._cw.vreg['forms'].select('edition', self._cw, rset=self.cw_rset,
+        form = self._cw.vreg['forms'].select('edition', self._cw,
+                                             rset=self.cw_rset,
                                              row=self.cw_row or 0)
         field = form.field_by_name(self.rtype, get_role(self), entity.e_schema)
-        for _, eid in field.choices(form):
-            if eid is not None and eid != INTERNAL_FIELD_VALUE:
-                rset = self._cw.eid_rset(eid)
-                entities.append(rset.get_entity(0, 0))
+        for _, eid in field.vocabulary(form):
+            if eid not in skip:
+                entity = self._cw.entity_from_eid(eid)
+                if filteretype is None or entity.__regid__ == filteretype:
+                    entities.append(entity)
         return entities