Updated CatalogResultSet class to be able to feed it with a list of internal IDs as well a Query object, and added "prepend" and "append" methods to be able to add a list of items to the beginning or the end of the initial results list
authorThierry Florac <thierry.florac@onf.fr>
Tue, 27 Jun 2017 13:41:57 +0200
changeset 21 464671eede98
parent 20 9da606c609cb
child 22 8c121a145dbe
Updated CatalogResultSet class to be able to feed it with a list of internal IDs as well a Query object, and added "prepend" and "append" methods to be able to add a list of items to the beginning or the end of the initial results list
src/pyams_catalog/query.py
--- a/src/pyams_catalog/query.py	Tue Jun 06 17:05:31 2017 +0200
+++ b/src/pyams_catalog/query.py	Tue Jun 27 13:41:57 2017 +0200
@@ -29,20 +29,38 @@
     def __init__(self, query):
         self.query = query
         self.intids = query_utility(IIntIds)
+        self.first = []
+        self.last = []
 
     def __iter__(self):
+        for item in self.first:
+            yield item
         intids = self.intids
-        if intids is None:
-            raise StopIteration
-        query = self.query
-        if isinstance(query, Query):
-            query = query.execute()
-        if isinstance(query, tuple):
-            query = query[1]
-        for oid in query:
-            target = intids.queryObject(oid)
-            if target is not None:
-                yield target
+        if intids is not None:
+            query = self.query
+            if isinstance(query, Query):
+                query = query.execute()
+            if isinstance(query, tuple):
+                query = query[1]
+            for oid in query:
+                if isinstance(oid, int):
+                    target = intids.queryObject(oid)
+                    if target is not None:
+                        yield target
+                else:
+                    yield oid
+        for item in self.last:
+            yield item
+
+    def prepend(self, items):
+        if isinstance(items, CatalogResultSet):
+            items = list(items)
+        self.first.extend(items)
+
+    def append(self, items):
+        if isinstance(items, CatalogResultSet):
+            items = list(items)
+        self.last.extend(items)
 
 
 def or_(source, added):