Updated method to get target from reference in front-office
authorThierry Florac <thierry.florac@onf.fr>
Fri, 22 Jun 2018 12:48:08 +0200
changeset 48 4ef3339c13e4
parent 47 e7d34a774c7e
child 49 59f67c66ff78
Updated method to get target from reference in front-office
src/pyams_sequence/utility.py
--- a/src/pyams_sequence/utility.py	Fri Jun 22 12:46:15 2018 +0200
+++ b/src/pyams_sequence/utility.py	Fri Jun 22 12:48:08 2018 +0200
@@ -19,9 +19,11 @@
 from hypatia.interfaces import ICatalog
 from pyams_i18n.interfaces import II18n
 from pyams_sequence.interfaces import ISequentialIntIds, ISequentialIdTarget, ISequentialIdInfo
+from pyams_skin.layer import IPyAMSUserLayer
 
 try:
-    from pyams_workflow.interfaces import IWorkflowVersions, IWorkflowVersion, IWorkflowManagedContent
+    from pyams_workflow.interfaces import IWorkflowVersions, IWorkflowVersion, IWorkflowManagedContent, IWorkflow, \
+        IWorkflowPublicationInfo
 except ImportError:
     handle_workflow = False
 else:
@@ -34,6 +36,7 @@
 from hypatia.query import Eq, Any
 from pyams_catalog.query import CatalogResultSet
 from pyams_utils.registry import query_utility, get_utility
+from pyams_utils.request import check_request
 from pyramid.events import subscriber
 from zope.interface import implementer, Invalid
 from zope.intid import IntIds
@@ -52,6 +55,23 @@
         return None
 
 
+def get_visible_version(content):
+    """Check for visible version"""
+    if handle_workflow:
+        if IWorkflowVersion.providedBy(content) or IWorkflowManagedContent.providedBy(content):
+            workflow = IWorkflow(content)
+            versions = IWorkflowVersions(content).get_versions(workflow.published_states, sort=True)
+            if versions:
+                return versions[-1]
+        publication_info = IWorkflowPublicationInfo(content, None)
+        if publication_info is not None:
+            if publication_info.is_visible():
+                return content
+            else:
+                return None
+    return content
+
+
 def get_version_in_state(content, state):
     """Check for versions in given status"""
     if handle_workflow and (IWorkflowVersion.providedBy(content) or IWorkflowManagedContent.providedBy(content)):
@@ -97,7 +117,12 @@
             if results:
                 return results[0]
         else:
-            results = list(map(get_last_version, CatalogResultSet(CatalogQuery(catalog).query(params))))
+            request = check_request()
+            if IPyAMSUserLayer.providedBy(request):  # front-office request
+                getter = get_visible_version
+            else:
+                getter = get_last_version
+            results = list(map(getter, CatalogResultSet(CatalogQuery(catalog).query(params))))
             if results:
                 return results[0]
     else: