Added random merger
authorThierry Florac <thierry.florac@onf.fr>
Tue, 03 Jul 2018 14:19:24 +0200
changeset 791 29c778c100e0
parent 790 bc6e355911d1
child 792 6b47283058ed
Added random merger
src/pyams_content/shared/view/merge.py
--- a/src/pyams_content/shared/view/merge.py	Tue Jul 03 12:14:35 2018 +0200
+++ b/src/pyams_content/shared/view/merge.py	Tue Jul 03 14:19:24 2018 +0200
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_utils.list import random_iter
 
 __docformat__ = 'restructuredtext'
 
@@ -64,6 +65,20 @@
         yield from chain(*results)
 
 
+@utility_config(name='random', provides=IViewsMerger)
+class ViewsRandomMergeMode(object):
+    """Views random merge mode"""
+
+    label = _("Extract items randomly")
+
+    @classmethod
+    def get_results(cls, views, context):
+        results = (view.get_results(context) for view in views)
+        results = list(chain(*results))
+        shuffle(results)
+        yield from iter(results)
+
+
 @utility_config(name='zip', provides=IViewsMerger)
 class ViewsZipMergeMode(object):
     """Views zip merge mode"""