Added sort key when merging iterators
authorThierry Florac <thierry.florac@onf.fr>
Mon, 02 Jul 2018 18:32:22 +0200
changeset 786 fb45f422d0ce
parent 785 99772127f89b
child 787 93f9a8b859fc
Added sort key when merging iterators
src/pyams_content/shared/view/merge.py
--- a/src/pyams_content/shared/view/merge.py	Mon Jul 02 18:29:56 2018 +0200
+++ b/src/pyams_content/shared/view/merge.py	Mon Jul 02 18:32:22 2018 +0200
@@ -9,6 +9,9 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from zope.dublincore.interfaces import IZopeDublinCore
+
+from pyams_workflow.interfaces import IWorkflowPublicationInfo
 
 __docformat__ = 'restructuredtext'
 
@@ -93,6 +96,7 @@
     """Sorted merge mode base class"""
 
     sort_index = None
+    sort_key = None
 
     @classmethod
     def get_results(cls, views, context):
@@ -100,7 +104,7 @@
                                     sort_index=cls.sort_index,
                                     reverse=True)
                    for view in views)
-        yield from merge(*results)
+        yield from merge(*results, key=cls.sort_key)
 
 
 @utility_config(name='{0}.sort'.format(CREATION_DATE_ORDER), provides=IViewsMerger)
@@ -109,6 +113,7 @@
 
     label = _("Sort all results by creation date")
     sort_index = CREATION_DATE_ORDER
+    sort_key = lambda x: IZopeDublinCore(x).created
 
 
 @utility_config(name='{0}.sort'.format(UPDATE_DATE_ORDER), provides=IViewsMerger)
@@ -117,6 +122,7 @@
 
     label = _("Sort all results by last update date")
     sort_index = UPDATE_DATE_ORDER
+    sort_key = lambda x: IZopeDublinCore(x).modified
 
 
 @utility_config(name='{0}.sort'.format(PUBLICATION_DATE_ORDER), provides=IViewsMerger)
@@ -125,6 +131,7 @@
 
     label = _("Sort all results by current publication date")
     sort_index = PUBLICATION_DATE_ORDER
+    sort_key = lambda x: IWorkflowPublicationInfo(x).publication_date
 
 
 @utility_config(name='{0}.sort'.format(FIRSTPUBLICATION_DATE_ORDER), provides=IViewsMerger)
@@ -133,3 +140,4 @@
 
     label = _("Sort all results by first publication date")
     sort_index = FIRSTPUBLICATION_DATE_ORDER
+    sort_key = lambda x: IWorkflowPublicationInfo(x).first_publication_date